-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
168 lines (149 loc) · 6.93 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Data Streaming</title>
</head>
<body>
<h1>Data Streaming</h1>
<textarea id="dataInput" rows="4" cols="50" placeholder="Enter data (one item per line)"></textarea><br>
<div id="lineCount">Total lines: 0</div>
<input type="text" id="combineLineInput" placeholder="Combine Line (e.g. 101-104)"><br>
<select id="separatorSelect">
<option value="">No separator</option>
<option value="\n">New line (\n)</option>
<option value="\r">Carriage return (\r)</option>
<option value="\t">Tab (\t)</option>
<option value="\v">Vertical tab (\v)</option>
<option value="\f">Form feed (\f)</option>
<option value="\u2028">Line separator (\u2028)</option>
<option value="\u2029">Paragraph separator (\u2029)</option>
<option value="\u200B">Zero width space (\u200B)</option>
<option value="\uFEFF">Zero width no-break space (\uFEFF)</option>
<option value="\u200D">Zero width joiner (\u200D)</option>
<option value="\u00AD">Soft hyphen (\u00AD)</option>
<option value="custom">Custom...</option>
</select>
<input type="text" id="customSeparatorInput" placeholder="Enter custom separator" style="display: none;"><br>
<button id="submitData">Submit Data</button>
<button id="startStream">Start Streaming</button>
<div id="result"></div>
<script>
const dataInput = document.getElementById('dataInput');
const lineCountDiv = document.getElementById('lineCount');
const combineLineInput = document.getElementById('combineLineInput');
const separatorSelect = document.getElementById('separatorSelect');
const customSeparatorInput = document.getElementById('customSeparatorInput');
// Load saved data from localStorage
window.addEventListener('load', () => {
const savedData = localStorage.getItem('userData');
const savedCombineLine = localStorage.getItem('userCombineLine');
const savedSeparator = localStorage.getItem('userSeparator');
if (savedData) {
dataInput.value = savedData;
updateLineCount();
}
if (savedCombineLine) {
combineLineInput.value = savedCombineLine;
}
if (savedSeparator) {
if (Array.from(separatorSelect.options).some(option => option.value === savedSeparator)) {
separatorSelect.value = savedSeparator;
} else {
separatorSelect.value = 'custom';
customSeparatorInput.value = savedSeparator;
customSeparatorInput.style.display = 'inline';
}
}
});
function updateLineCount() {
const data = dataInput.value;
const lines = data.split('\n').filter(line => line.trim() !== '');
const lineCount = lines.length;
lineCountDiv.textContent = `Total lines: ${lineCount}`;
// Save data to localStorage
localStorage.setItem('userData', data);
}
function saveCombineLine() {
localStorage.setItem('userCombineLine', combineLineInput.value);
}
function saveSeparator() {
const separator = separatorSelect.value === 'custom' ? customSeparatorInput.value : separatorSelect.value;
localStorage.setItem('userSeparator', separator);
}
dataInput.addEventListener('input', updateLineCount);
combineLineInput.addEventListener('input', saveCombineLine);
separatorSelect.addEventListener('change', () => {
if (separatorSelect.value === 'custom') {
customSeparatorInput.style.display = 'inline';
} else {
customSeparatorInput.style.display = 'none';
}
saveSeparator();
});
customSeparatorInput.addEventListener('input', saveSeparator);
document.getElementById('submitData').addEventListener('click', async () => {
try {
const data = dataInput.value;
const combineLine = combineLineInput.value;
const separator = separatorSelect.value === 'custom' ? customSeparatorInput.value : separatorSelect.value;
const response = await fetch('/submit', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ data, combineLine, separator })
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const result = await response.text();
alert(result);
} catch (error) {
console.error('Error submitting data:', error);
alert('Error submitting data. Check console for details.');
}
});
document.getElementById('startStream').addEventListener('click', () => {
const resultDiv = document.getElementById('result');
resultDiv.innerHTML = ''; // Clear previous results
console.log('Starting stream...');
fetch('/stream').then(response => {
const reader = response.body.getReader();
const decoder = new TextDecoder();
let buffer = '';
function processText(text) {
const lines = (buffer + text).split('\n');
buffer = lines.pop(); // Keep the last incomplete chunk in the buffer
lines.forEach(line => {
if (line.trim() !== '') {
console.log('Received data:', line);
const p = document.createElement('p');
p.textContent = line;
resultDiv.appendChild(p);
if (line.trim() === '[DONE]') {
console.log('Received [DONE], closing stream');
reader.cancel();
}
}
});
}
function pump() {
return reader.read().then(({ done, value }) => {
if (done) {
if (buffer) processText(buffer);
return;
}
processText(decoder.decode(value, { stream: true }));
return pump();
});
}
return pump();
}).catch(error => {
console.error('Fetch error:', error);
});
});
</script>
</body>
</html>