@shaykec/bridge 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,361 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>ClaudeTeach — Timed Choice</title>
7
+ <style>
8
+ * { margin: 0; padding: 0; box-sizing: border-box; }
9
+ body {
10
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
11
+ background: #0d1117;
12
+ color: #c9d1d9;
13
+ display: flex;
14
+ flex-direction: column;
15
+ align-items: center;
16
+ justify-content: center;
17
+ min-height: 100vh;
18
+ padding: 2rem;
19
+ }
20
+ .timer-bar {
21
+ width: 100%;
22
+ max-width: 500px;
23
+ height: 6px;
24
+ background: #21262d;
25
+ border-radius: 3px;
26
+ margin-bottom: 1.5rem;
27
+ overflow: hidden;
28
+ }
29
+ .timer-fill {
30
+ height: 100%;
31
+ background: #58a6ff;
32
+ border-radius: 3px;
33
+ transition: width 0.1s linear;
34
+ width: 100%;
35
+ }
36
+ .timer-fill.warning { background: #d29922; }
37
+ .timer-fill.danger { background: #f85149; }
38
+ .timer-text {
39
+ font-size: 0.85rem;
40
+ color: #8b949e;
41
+ margin-bottom: 1rem;
42
+ }
43
+ h2 {
44
+ color: #58a6ff;
45
+ margin-bottom: 0.5rem;
46
+ font-size: 1.2rem;
47
+ }
48
+ .question {
49
+ margin-bottom: 1.5rem;
50
+ font-size: 1.1rem;
51
+ text-align: center;
52
+ max-width: 600px;
53
+ line-height: 1.5;
54
+ }
55
+ .choices {
56
+ display: flex;
57
+ flex-direction: column;
58
+ gap: 0.5rem;
59
+ width: 100%;
60
+ max-width: 500px;
61
+ margin-bottom: 1.5rem;
62
+ }
63
+ .choice {
64
+ background: #161b22;
65
+ border: 2px solid #30363d;
66
+ border-radius: 8px;
67
+ padding: 0.8rem 1rem;
68
+ cursor: pointer;
69
+ display: flex;
70
+ align-items: center;
71
+ gap: 0.75rem;
72
+ transition: border-color 0.15s, background 0.15s;
73
+ font-size: 0.95rem;
74
+ }
75
+ .choice:hover {
76
+ border-color: #58a6ff;
77
+ background: #1c2333;
78
+ }
79
+ .choice.selected {
80
+ border-color: #58a6ff;
81
+ background: #1c2333;
82
+ }
83
+ .choice.correct {
84
+ border-color: #238636;
85
+ background: rgba(35, 134, 54, 0.15);
86
+ }
87
+ .choice.incorrect {
88
+ border-color: #f85149;
89
+ background: rgba(248, 81, 73, 0.15);
90
+ }
91
+ .choice.disabled {
92
+ cursor: not-allowed;
93
+ opacity: 0.7;
94
+ }
95
+ .choice-letter {
96
+ background: #30363d;
97
+ color: #8b949e;
98
+ width: 28px;
99
+ height: 28px;
100
+ border-radius: 50%;
101
+ display: flex;
102
+ align-items: center;
103
+ justify-content: center;
104
+ font-size: 0.8rem;
105
+ font-weight: 600;
106
+ flex-shrink: 0;
107
+ }
108
+ .choice.selected .choice-letter {
109
+ background: #58a6ff;
110
+ color: #0d1117;
111
+ }
112
+ .btn {
113
+ background: #238636;
114
+ color: #fff;
115
+ border: none;
116
+ padding: 0.7rem 2rem;
117
+ border-radius: 6px;
118
+ font-size: 1rem;
119
+ cursor: pointer;
120
+ font-weight: 500;
121
+ transition: background 0.15s;
122
+ }
123
+ .btn:hover { background: #2ea043; }
124
+ .btn:disabled {
125
+ background: #21262d;
126
+ color: #484f58;
127
+ cursor: not-allowed;
128
+ }
129
+ .result {
130
+ margin-top: 1.5rem;
131
+ padding: 1rem 1.5rem;
132
+ border-radius: 8px;
133
+ font-weight: 500;
134
+ text-align: center;
135
+ display: none;
136
+ }
137
+ .result.correct {
138
+ display: block;
139
+ background: rgba(35, 134, 54, 0.15);
140
+ border: 1px solid #238636;
141
+ color: #3fb950;
142
+ }
143
+ .result.incorrect {
144
+ display: block;
145
+ background: rgba(248, 81, 73, 0.15);
146
+ border: 1px solid #f85149;
147
+ color: #f85149;
148
+ }
149
+ .result.timeout {
150
+ display: block;
151
+ background: rgba(210, 153, 34, 0.15);
152
+ border: 1px solid #d29922;
153
+ color: #d29922;
154
+ }
155
+ </style>
156
+ </head>
157
+ <body>
158
+ <div class="timer-bar"><div class="timer-fill" id="timerFill"></div></div>
159
+ <div class="timer-text" id="timerText">30s remaining</div>
160
+ <h2>Quick Quiz</h2>
161
+ <div class="question" id="question">Loading...</div>
162
+ <div class="choices" id="choices"></div>
163
+ <button class="btn" id="submitBtn" onclick="submitAnswer()" disabled>Submit</button>
164
+ <div class="result" id="result"></div>
165
+
166
+ <div aria-live="polite" style="position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0,0,0,0)" id="announcer"></div>
167
+
168
+ <script>
169
+ /* --- Bridge connection (WebSocket + REST fallback) --- */
170
+ var wsBridge = null;
171
+ function connectBridge() {
172
+ try {
173
+ wsBridge = new WebSocket('ws://' + location.host + '/ws');
174
+ wsBridge.onopen = function() {
175
+ wsBridge.send(JSON.stringify({
176
+ v: 1, type: 'sys:connect',
177
+ payload: { clientType: 'template' },
178
+ source: 'template', timestamp: Date.now()
179
+ }));
180
+ };
181
+ wsBridge.onerror = function() { wsBridge = null; };
182
+ wsBridge.onclose = function() { wsBridge = null; };
183
+ } catch(e) { wsBridge = null; }
184
+ }
185
+ connectBridge();
186
+
187
+ function sendResultToBridge(payload) {
188
+ var msg = {
189
+ v: 1, type: 'event:quiz-answer',
190
+ payload: payload,
191
+ source: 'template', timestamp: Date.now()
192
+ };
193
+ if (wsBridge && wsBridge.readyState === 1) { wsBridge.send(JSON.stringify(msg)); }
194
+ try {
195
+ fetch('/api/event', {
196
+ method: 'POST',
197
+ headers: { 'Content-Type': 'application/json' },
198
+ body: JSON.stringify(msg)
199
+ }).catch(function(){});
200
+ } catch(e) {}
201
+ if (window.parent !== window) { window.parent.postMessage(msg, '*'); }
202
+ }
203
+
204
+ let quizData = {
205
+ question: 'Which command creates a new branch?',
206
+ choices: ['git branch feature', 'git checkout feature', 'git merge feature', 'git push feature'],
207
+ correctIndex: 0,
208
+ timeLimit: 30,
209
+ };
210
+
211
+ let selectedIndex = null;
212
+ let startTime = null;
213
+ let timerInterval = null;
214
+ let answered = false;
215
+
216
+ function init(data) {
217
+ if (data) quizData = { ...quizData, ...data };
218
+ document.getElementById('question').textContent = quizData.question;
219
+ renderChoices();
220
+ startTimer();
221
+ }
222
+
223
+ function renderChoices() {
224
+ const container = document.getElementById('choices');
225
+ container.innerHTML = '';
226
+ const letters = 'ABCDEFGHIJ';
227
+
228
+ quizData.choices.forEach((choice, i) => {
229
+ const div = document.createElement('div');
230
+ div.className = 'choice';
231
+ div.dataset.index = i;
232
+ div.tabIndex = 0;
233
+ div.setAttribute('role', 'radio');
234
+ div.setAttribute('aria-checked', 'false');
235
+ div.setAttribute('aria-label', letters[i] + ': ' + choice);
236
+ div.innerHTML = `
237
+ <span class="choice-letter">${letters[i]}</span>
238
+ <span>${choice}</span>
239
+ `;
240
+ div.addEventListener('click', () => selectChoice(i));
241
+ div.addEventListener('keydown', (e) => {
242
+ if (e.key === 'Enter' || e.key === ' ') { e.preventDefault(); selectChoice(i); }
243
+ if (e.key === 'ArrowDown' && i < quizData.choices.length - 1) {
244
+ e.preventDefault();
245
+ container.children[i + 1].focus();
246
+ }
247
+ if (e.key === 'ArrowUp' && i > 0) {
248
+ e.preventDefault();
249
+ container.children[i - 1].focus();
250
+ }
251
+ });
252
+ container.appendChild(div);
253
+ });
254
+ }
255
+
256
+ function selectChoice(index) {
257
+ if (answered) return;
258
+ selectedIndex = index;
259
+ document.querySelectorAll('.choice').forEach((el, i) => {
260
+ el.classList.toggle('selected', i === index);
261
+ });
262
+ document.getElementById('submitBtn').disabled = false;
263
+ }
264
+
265
+ function startTimer() {
266
+ startTime = Date.now();
267
+ const duration = (quizData.timeLimit || 30) * 1000;
268
+
269
+ timerInterval = setInterval(() => {
270
+ const elapsed = Date.now() - startTime;
271
+ const remaining = Math.max(0, duration - elapsed);
272
+ const pct = (remaining / duration) * 100;
273
+
274
+ const fill = document.getElementById('timerFill');
275
+ fill.style.width = pct + '%';
276
+ fill.className = 'timer-fill' +
277
+ (pct < 20 ? ' danger' : pct < 50 ? ' warning' : '');
278
+
279
+ document.getElementById('timerText').textContent =
280
+ Math.ceil(remaining / 1000) + 's remaining';
281
+
282
+ if (remaining <= 0) {
283
+ clearInterval(timerInterval);
284
+ timeOut();
285
+ }
286
+ }, 100);
287
+ }
288
+
289
+ function timeOut() {
290
+ if (answered) return;
291
+ answered = true;
292
+ disableChoices();
293
+
294
+ const resultEl = document.getElementById('result');
295
+ resultEl.className = 'result timeout';
296
+ resultEl.textContent = "Time's up! The correct answer was: " +
297
+ quizData.choices[quizData.correctIndex];
298
+
299
+ highlightCorrect();
300
+ sendResult(false, Date.now() - startTime);
301
+ }
302
+
303
+ function submitAnswer() {
304
+ if (answered || selectedIndex === null) return;
305
+ answered = true;
306
+ clearInterval(timerInterval);
307
+ disableChoices();
308
+
309
+ const correct = selectedIndex === quizData.correctIndex;
310
+ const timeMs = Date.now() - startTime;
311
+
312
+ const resultEl = document.getElementById('result');
313
+ resultEl.className = 'result ' + (correct ? 'correct' : 'incorrect');
314
+ resultEl.textContent = correct
315
+ ? `Correct! (${(timeMs / 1000).toFixed(1)}s)`
316
+ : 'Incorrect. The right answer was: ' + quizData.choices[quizData.correctIndex];
317
+
318
+ highlightCorrect();
319
+ if (!correct) {
320
+ document.querySelectorAll('.choice')[selectedIndex].classList.add('incorrect');
321
+ }
322
+
323
+ sendResult(correct, timeMs);
324
+ }
325
+
326
+ function disableChoices() {
327
+ document.querySelectorAll('.choice').forEach(el => {
328
+ el.classList.add('disabled');
329
+ });
330
+ document.getElementById('submitBtn').disabled = true;
331
+ }
332
+
333
+ function highlightCorrect() {
334
+ document.querySelectorAll('.choice')[quizData.correctIndex].classList.add('correct');
335
+ }
336
+
337
+ function sendResult(correct, timeMs) {
338
+ sendResultToBridge({
339
+ quizType: 'timed-choice',
340
+ answer: quizData.choices[selectedIndex] || null,
341
+ correct,
342
+ timeMs,
343
+ });
344
+ document.getElementById('announcer').textContent =
345
+ correct ? 'Correct!' : 'Incorrect. The right answer was ' + quizData.choices[quizData.correctIndex];
346
+ }
347
+
348
+ window.addEventListener('message', (e) => {
349
+ if (e.data && e.data.type === 'quiz-data') {
350
+ init(e.data.payload);
351
+ }
352
+ });
353
+
354
+ if (window.__QUIZ_DATA__) {
355
+ init(window.__QUIZ_DATA__);
356
+ } else {
357
+ init();
358
+ }
359
+ </script>
360
+ </body>
361
+ </html>