-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathembed.html
More file actions
416 lines (361 loc) · 13.3 KB
/
embed.html
File metadata and controls
416 lines (361 loc) · 13.3 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Plebs Embedded Video</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html, body {
width: 100%;
height: 100%;
overflow: hidden;
background: #000;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}
.embed-container {
position: relative;
width: 100%;
height: 100%;
}
video {
width: 100%;
height: 100%;
object-fit: contain;
background: #000;
}
/* Plebs Logo Watermark */
.watermark {
position: absolute;
bottom: 70px;
right: 12px;
display: flex;
align-items: center;
gap: 6px;
padding: 6px 10px;
background: rgba(0, 0, 0, 0.6);
border-radius: 6px;
text-decoration: none;
color: white;
font-size: 12px;
font-weight: 600;
opacity: 0.8;
transition: opacity 0.2s;
z-index: 10;
}
.watermark:hover {
opacity: 1;
}
.watermark svg {
width: 20px;
height: 20px;
}
/* Loading state */
.loading {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: white;
font-size: 14px;
display: flex;
flex-direction: column;
align-items: center;
gap: 12px;
}
.spinner {
width: 40px;
height: 40px;
border: 3px solid rgba(255, 255, 255, 0.3);
border-radius: 50%;
border-top-color: #ff5500;
animation: spin 1s linear infinite;
}
@keyframes spin {
to { transform: rotate(360deg); }
}
/* Error state */
.error {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: white;
text-align: center;
padding: 20px;
}
.error h3 {
margin-bottom: 10px;
color: #ff5500;
}
.error p {
color: #aaa;
font-size: 14px;
}
.error a {
color: #ff5500;
text-decoration: none;
}
.error a:hover {
text-decoration: underline;
}
/* Hide watermark when controls are hidden */
video::-webkit-media-controls {
display: flex !important;
}
</style>
</head>
<body>
<div class="embed-container" id="embedContainer">
<div class="loading" id="loadingState">
<div class="spinner"></div>
<span>Loading video...</span>
</div>
</div>
<script>
// Relay configuration (same as main app)
const RELAYS = [
'wss://relay.primal.net',
'wss://relay.damus.io',
'wss://nos.lol',
'wss://relay.nostr.band'
];
// NIP-71 Video Event Kinds
const NIP71_VIDEO_KIND = 34235;
const NIP71_SHORT_KIND = 34236;
const NIP71_VIDEO_KIND_LEGACY = 21;
const NIP71_SHORT_KIND_LEGACY = 22;
const ALL_VIDEO_KINDS = [1, NIP71_VIDEO_KIND, NIP71_SHORT_KIND, NIP71_VIDEO_KIND_LEGACY, NIP71_SHORT_KIND_LEGACY];
// Get event ID from URL
const urlParams = new URLSearchParams(window.location.search);
const eventId = urlParams.get('id');
if (!eventId) {
showError('No video ID provided');
} else {
loadVideo(eventId);
}
async function loadVideo(eventId) {
try {
// Connect to relays and fetch event
const event = await fetchEvent(eventId);
if (!event) {
showError('Video not found', 'The video may have been deleted or is unavailable.');
return;
}
// Parse video data
const videoData = parseVideoEvent(event);
if (!videoData || !videoData.url) {
showError('Invalid video data', 'Could not parse video information.');
return;
}
// Create video player with fallback support
createPlayer(videoData, eventId);
} catch (error) {
console.error('Error loading video:', error);
showError('Failed to load video', error.message);
}
}
async function fetchEvent(eventId) {
return new Promise((resolve, reject) => {
let resolved = false;
let event = null;
const sockets = [];
const timeout = setTimeout(() => {
if (!resolved) {
resolved = true;
sockets.forEach(ws => ws.close());
resolve(event);
}
}, 10000);
RELAYS.forEach(relayUrl => {
try {
const ws = new WebSocket(relayUrl);
sockets.push(ws);
ws.onopen = () => {
const subId = 'embed_' + Math.random().toString(36).substr(2, 9);
const filter = {
ids: [eventId]
};
ws.send(JSON.stringify(['REQ', subId, filter]));
};
ws.onmessage = (msg) => {
try {
const data = JSON.parse(msg.data);
if (data[0] === 'EVENT' && data[2]) {
event = data[2];
if (!resolved) {
resolved = true;
clearTimeout(timeout);
sockets.forEach(s => s.close());
resolve(event);
}
}
} catch (e) {}
};
ws.onerror = () => {};
} catch (e) {}
});
});
}
function parseVideoEvent(event) {
const tags = event.tags || [];
// Check if NIP-71 event
if ([NIP71_VIDEO_KIND, NIP71_SHORT_KIND, NIP71_VIDEO_KIND_LEGACY, NIP71_SHORT_KIND_LEGACY].includes(event.kind)) {
return parseNip71Event(event);
}
// Parse kind 1 event
const urlTag = tags.find(t => t[0] === 'url' || t[0] === 'video_url');
const url = urlTag ? urlTag[1] : null;
if (!url) {
// Try to find URL in content
const urlMatch = event.content?.match(/https?:\/\/[^\s]+\.(mp4|webm|mov|m3u8)/i);
if (urlMatch) {
return { url: urlMatch[0], fallbackUrls: [] };
}
return null;
}
// Get fallback URLs
const fallbackUrls = tags
.filter(t => t[0] === 'fallback')
.map(t => t[1])
.filter(Boolean);
return {
url,
fallbackUrls,
title: tags.find(t => t[0] === 'title')?.[1] || ''
};
}
function parseNip71Event(event) {
const tags = event.tags || [];
// Get primary video URL from imeta tag
let primaryUrl = null;
let fallbackUrls = [];
// Parse imeta tags for video URLs
const imetaTags = tags.filter(t => t[0] === 'imeta');
for (const imeta of imetaTags) {
const tagData = {};
for (let i = 1; i < imeta.length; i++) {
const [key, ...valueParts] = imeta[i].split(' ');
tagData[key] = valueParts.join(' ');
}
// Check if this is a video
if (tagData.m && tagData.m.startsWith('video/') && tagData.url) {
if (!primaryUrl) {
primaryUrl = tagData.url;
} else {
fallbackUrls.push(tagData.url);
}
}
}
// Also check for direct URL tag
const urlTag = tags.find(t => t[0] === 'url');
if (urlTag && urlTag[1]) {
if (!primaryUrl) {
primaryUrl = urlTag[1];
} else if (!fallbackUrls.includes(urlTag[1])) {
fallbackUrls.push(urlTag[1]);
}
}
// Check fallback tags
const fallbackTags = tags.filter(t => t[0] === 'fallback');
fallbackTags.forEach(t => {
if (t[1] && !fallbackUrls.includes(t[1]) && t[1] !== primaryUrl) {
fallbackUrls.push(t[1]);
}
});
// Get thumbnail
const thumbTag = tags.find(t => t[0] === 'thumb' || t[0] === 'image');
const thumbnail = thumbTag ? thumbTag[1] : null;
// Get title
const titleTag = tags.find(t => t[0] === 'title');
const title = titleTag ? titleTag[1] : '';
return {
url: primaryUrl,
fallbackUrls,
thumbnail,
title
};
}
function createPlayer(videoData, eventId) {
const container = document.getElementById('embedContainer');
const allUrls = [videoData.url, ...videoData.fallbackUrls].filter(Boolean);
let currentUrlIndex = 0;
// Create video element
const video = document.createElement('video');
video.controls = true;
video.autoplay = false;
video.playsInline = true;
video.preload = 'metadata';
// Set poster if available
if (videoData.thumbnail) {
video.poster = videoData.thumbnail;
}
// Error handler with fallback
video.onerror = () => {
currentUrlIndex++;
if (currentUrlIndex < allUrls.length) {
console.log('Trying fallback URL:', allUrls[currentUrlIndex]);
video.src = allUrls[currentUrlIndex];
video.load();
} else {
showError('Video unavailable', 'All video sources failed to load.');
}
};
// Set initial source
video.src = allUrls[0];
// Create watermark
const watermark = document.createElement('a');
watermark.className = 'watermark';
watermark.href = window.location.origin + '/#/video/' + eventId;
watermark.target = '_blank';
watermark.rel = 'noopener noreferrer';
watermark.innerHTML = `
<svg viewBox="0 0 24 24">
<path d="M4 7 Q4 4, 6 5.5 L11 10.5 Q14 12, 11 13.5 L6 18.5 Q4 20, 4 17 Z" fill="#1da1f2" opacity="0.4"/>
<path d="M10 7 Q10 4, 12 5.5 L17 10.5 Q20 12, 17 13.5 L12 18.5 Q10 20, 10 17 Z" fill="#1da1f2"/>
</svg>
<span>Watch on Plebs</span>
`;
// Clear loading state and add player
container.innerHTML = '';
container.appendChild(video);
container.appendChild(watermark);
// Show/hide watermark based on video state
let hideTimeout;
const showWatermark = () => {
watermark.style.opacity = '0.8';
clearTimeout(hideTimeout);
hideTimeout = setTimeout(() => {
if (!video.paused) {
watermark.style.opacity = '0.3';
}
}, 3000);
};
video.addEventListener('play', showWatermark);
video.addEventListener('pause', () => {
watermark.style.opacity = '0.8';
clearTimeout(hideTimeout);
});
video.addEventListener('mousemove', showWatermark);
container.addEventListener('mousemove', showWatermark);
}
function showError(title, message = '') {
const container = document.getElementById('embedContainer');
const origin = window.location.origin || window.location.href.split('/embed.html')[0];
container.innerHTML = `
<div class="error">
<h3>${title}</h3>
${message ? `<p>${message}</p>` : ''}
<p style="margin-top: 15px;">
<a href="${origin}" target="_blank">Visit Plebs</a>
</p>
</div>
`;
}
</script>
</body>
</html>