-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathwidgets.js
More file actions
469 lines (404 loc) · 16.3 KB
/
widgets.js
File metadata and controls
469 lines (404 loc) · 16.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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
// DEPRECATED: This script has been replaced by React components in snippets/broadcast-card.mdx and snippets/broadcast-carousel.mdx
// The livestreams.mdx page now uses these components directly instead of dynamic DOM manipulation.
// This file is kept for reference but is no longer needed for the livestreams page functionality.
// Simple custom Twitter/X broadcast link script
// Creates clickable cards linking to broadcasts (avoiding 429 rate limits)
(function() {
console.log('X broadcast link script starting...');
var processedElements = new WeakSet(); // Track processed elements
function extractBroadcastId(url) {
// Extract broadcast ID from URL
var match = url.match(/\/i\/broadcasts\/([^\/\?#]+)/);
return match ? match[1] : null;
}
function isBroadcastLink(url) {
// Check if URL is a broadcast link
return /\/i\/broadcasts\//.test(url);
}
function createBroadcastCard(url, container, title, targetContainer) {
// Mark as processed immediately
processedElements.add(container);
var broadcastId = extractBroadcastId(url);
if (!broadcastId) {
console.warn('Could not extract broadcast ID from:', url);
return;
}
// Create a nice card linking to the broadcast
var card = document.createElement('a');
card.href = url;
card.target = '_blank';
card.rel = 'noopener noreferrer';
card.style.display = 'block';
card.style.textDecoration = 'none';
card.style.color = 'inherit';
card.style.margin = '1em 0';
card.style.maxWidth = '480px';
card.style.marginLeft = '0';
card.style.marginRight = 'auto';
card.style.borderRadius = '12px';
card.style.overflow = 'hidden';
card.style.backgroundColor = '#000';
card.style.border = '1px solid #333';
card.style.transition = 'transform 0.2s, box-shadow 0.2s';
// Hover effect
card.addEventListener('mouseenter', function() {
this.style.transform = 'translateY(-2px)';
this.style.boxShadow = '0 4px 12px rgba(0,0,0,0.3)';
});
card.addEventListener('mouseleave', function() {
this.style.transform = 'translateY(0)';
this.style.boxShadow = 'none';
});
// Create preview container with default thumbnail
var previewContainer = document.createElement('div');
previewContainer.style.position = 'relative';
previewContainer.style.width = '100%';
previewContainer.style.paddingTop = '56.25%'; // 16:9 aspect ratio
previewContainer.style.background = 'linear-gradient(135deg, #0f0f0f 0%, #050505 100%)'; // Very dark gray gradient for black background
previewContainer.style.backgroundSize = 'cover';
previewContainer.style.backgroundPosition = 'center';
previewContainer.style.overflow = 'hidden';
// Create logo container (offset position)
var logoContainer = document.createElement('div');
logoContainer.style.position = 'absolute';
logoContainer.style.bottom = '-14%';
logoContainer.style.left = '-7%';
logoContainer.style.width = '400px';
logoContainer.style.height = '400px';
logoContainer.style.opacity = '0.15';
// Create SVG logo
var logoSvg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
logoSvg.setAttribute('width', '400');
logoSvg.setAttribute('height', '400');
logoSvg.setAttribute('viewBox', '0 0 1200 1227');
logoSvg.setAttribute('fill', 'none');
logoSvg.setAttribute('xmlns', 'http://www.w3.org/2000/svg');
var logoPath = document.createElementNS('http://www.w3.org/2000/svg', 'path');
logoPath.setAttribute('d', 'M714.163 519.284L1160.89 0H1055.03L667.137 450.887L357.328 0H0L468.492 681.821L0 1226.37H105.866L515.491 750.218L842.672 1226.37H1200L714.137 519.284H714.163ZM569.165 687.828L521.697 619.934L144.011 79.6944H306.615L611.412 515.685L658.88 583.579L1055.08 1150.3H892.476L569.165 687.854V687.828Z');
logoPath.setAttribute('fill', 'white');
logoSvg.appendChild(logoPath);
logoContainer.appendChild(logoSvg);
// Create overlay with play button
var overlay = document.createElement('div');
overlay.style.position = 'absolute';
overlay.style.top = '0';
overlay.style.left = '0';
overlay.style.right = '0';
overlay.style.bottom = '0';
overlay.style.display = 'flex';
overlay.style.flexDirection = 'column';
overlay.style.alignItems = 'center';
overlay.style.justifyContent = 'center';
overlay.style.color = '#fff';
overlay.style.cursor = 'pointer';
overlay.style.backgroundColor = 'rgba(0,0,0,0.3)';
// Play icon container
var playIconContainer = document.createElement('div');
playIconContainer.style.width = '80px';
playIconContainer.style.height = '80px';
playIconContainer.style.borderRadius = '50%';
playIconContainer.style.backgroundColor = 'rgba(20, 20, 20, 0.9)';
playIconContainer.style.border = '2px solid rgba(255, 255, 255, 0.3)';
playIconContainer.style.display = 'flex';
playIconContainer.style.alignItems = 'center';
playIconContainer.style.justifyContent = 'center';
playIconContainer.style.marginBottom = '16px';
playIconContainer.style.transition = 'transform 0.2s';
var playIcon = document.createElement('div');
playIcon.innerHTML = '▶';
playIcon.style.fontSize = '32px';
playIcon.style.color = '#ffffff';
playIcon.style.marginLeft = '4px';
playIconContainer.appendChild(playIcon);
card.addEventListener('mouseenter', function() {
playIconContainer.style.transform = 'scale(1.1)';
});
card.addEventListener('mouseleave', function() {
playIconContainer.style.transform = 'scale(1)';
});
overlay.appendChild(playIconContainer);
previewContainer.appendChild(logoContainer);
previewContainer.appendChild(overlay);
card.appendChild(previewContainer);
// Add title if provided
if (title) {
var titleContainer = document.createElement('div');
titleContainer.style.padding = '16px 20px';
titleContainer.style.backgroundColor = '#000';
titleContainer.style.borderTop = '1px solid #333';
// Parse title and date (date is in parentheses)
var titleText = title;
var dateText = null;
var dateMatch = title.match(/\(([^)]+)\)$/);
if (dateMatch) {
dateText = dateMatch[1];
titleText = title.replace(/\s*\([^)]+\)$/, '').trim();
}
// Title element
var titleElement = document.createElement('div');
titleElement.style.color = '#fff';
titleElement.style.fontSize = '16px';
titleElement.style.fontWeight = '600';
titleElement.style.lineHeight = '1.4';
titleElement.style.marginBottom = dateText ? '4px' : '0';
titleElement.textContent = titleText;
titleContainer.appendChild(titleElement);
// Date element (if date found)
if (dateText) {
var dateElement = document.createElement('div');
dateElement.style.color = '#999';
dateElement.style.fontSize = '14px';
dateElement.style.fontWeight = '400';
dateElement.style.lineHeight = '1.4';
dateElement.textContent = dateText;
titleContainer.appendChild(dateElement);
}
card.appendChild(titleContainer);
}
// Append the card to target container or original container
if (targetContainer) {
// Update card styles for carousel
card.style.flexShrink = '0';
card.style.width = '480px';
card.style.maxWidth = '480px';
card.style.margin = '0';
targetContainer.appendChild(card);
} else {
// Append to original container
container.innerHTML = '';
container.appendChild(card);
}
// Clean up original container if using target
if (targetContainer) {
container.innerHTML = '';
container.style.display = 'none';
}
console.log('Broadcast card created for:', url);
return card;
}
function parseDate(dateString) {
// Parse date string like "September 25, 2025"
var months = {
'January': 0, 'February': 1, 'March': 2, 'April': 3, 'May': 4, 'June': 5,
'July': 6, 'August': 7, 'September': 8, 'October': 9, 'November': 10, 'December': 11
};
var parts = dateString.match(/(\w+)\s+(\d+),\s+(\d+)/);
if (parts) {
return new Date(parseInt(parts[3]), months[parts[1]], parseInt(parts[2]));
}
return null;
}
function formatMonthYear(date) {
var months = ['January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December'];
return months[date.getMonth()] + ' ' + date.getFullYear();
}
function embedTweets() {
var tweetDivs = document.querySelectorAll('div.twitter-tweet');
console.log('Found', tweetDivs.length, 'div.twitter-tweet elements');
// First pass: collect all broadcast cards with their dates
var broadcasts = [];
tweetDivs.forEach(function(div, index) {
// Skip if already processed
if (processedElements.has(div)) {
return;
}
// Check if we already created a card
if (div.querySelector('a[href*="broadcasts"][target="_blank"]')) {
var existingCard = div.querySelector('div[style*="position: relative"]');
if (existingCard) {
processedElements.add(div);
return;
}
}
// Find the anchor tag
var anchor = div.querySelector('a');
if (!anchor || !anchor.href) {
return;
}
var url = anchor.href;
// Only handle broadcast links
if (isBroadcastLink(url)) {
// Find the previous h3 element (title)
var title = null;
var prevElement = div.previousElementSibling;
while (prevElement) {
if (prevElement.tagName === 'H3') {
title = prevElement.textContent.trim();
break;
}
prevElement = prevElement.previousElementSibling;
}
// Extract date from title
var date = null;
var dateMatch = title ? title.match(/\(([^)]+)\)$/) : null;
if (dateMatch) {
date = parseDate(dateMatch[1]);
}
broadcasts.push({
div: div,
url: url,
title: title,
date: date
});
}
});
// Sort by date (newest first)
broadcasts.sort(function(a, b) {
if (!a.date && !b.date) return 0;
if (!a.date) return 1;
if (!b.date) return -1;
return b.date - a.date;
});
// Find the "Past Broadcasts" heading and create carousel container after it
var carouselContainer = null;
// First, check if a carousel container already exists
var existingCarousel = document.querySelector('[data-broadcast-carousel="true"]');
if (existingCarousel) {
console.log('Reusing existing carousel container');
carouselContainer = existingCarousel;
} else {
var headings = document.querySelectorAll('h2');
var pastBroadcastsHeading = null;
console.log('Looking for "Past Broadcasts" heading. Found', headings.length, 'h2 elements');
for (var i = 0; i < headings.length; i++) {
var headingText = headings[i].textContent.trim();
console.log('Checking h2:', headingText);
if (headingText === 'Past Broadcasts' || headingText.includes('Past Broadcasts')) {
pastBroadcastsHeading = headings[i];
console.log('Found "Past Broadcasts" heading!');
break;
}
}
if (!pastBroadcastsHeading) {
console.warn('Could not find "Past Broadcasts" heading');
}
if (pastBroadcastsHeading && broadcasts.length > 0) {
console.log('Creating carousel container with', broadcasts.length, 'broadcasts');
carouselContainer = document.createElement('div');
carouselContainer.setAttribute('data-broadcast-carousel', 'true');
carouselContainer.style.display = 'flex';
carouselContainer.style.overflowX = 'auto';
carouselContainer.style.overflowY = 'hidden';
carouselContainer.style.gap = '20px';
carouselContainer.style.padding = '20px 0';
carouselContainer.style.scrollBehavior = 'smooth';
carouselContainer.style.webkitOverflowScrolling = 'touch';
// Hide scrollbar but keep functionality
carouselContainer.style.scrollbarWidth = 'thin';
carouselContainer.style.scrollbarColor = '#333 transparent';
// Insert carousel container after the "Past Broadcasts" heading
// Find the next element sibling or insert after the heading
var nextElement = pastBroadcastsHeading.nextElementSibling;
if (nextElement) {
pastBroadcastsHeading.parentNode.insertBefore(carouselContainer, nextElement);
} else {
pastBroadcastsHeading.parentNode.appendChild(carouselContainer);
}
console.log('Carousel container inserted');
} else {
console.warn('Cannot create carousel: heading found =', !!pastBroadcastsHeading, ', broadcasts =', broadcasts.length);
}
}
// Render broadcasts in carousel
var embedded = 0;
if (!carouselContainer) {
console.warn('No carousel container available, skipping card creation');
return embedded;
}
broadcasts.forEach(function(broadcast) {
// Skip if already processed
if (processedElements.has(broadcast.div)) {
console.log('Skipping already processed broadcast:', broadcast.url);
return;
}
// Check if a card for this URL already exists in the carousel
var existingCard = carouselContainer.querySelector('a[href="' + broadcast.url + '"]');
if (existingCard) {
console.log('Card already exists in carousel for:', broadcast.url);
processedElements.add(broadcast.div);
return;
}
// Hide the original h3
var prevElement = broadcast.div.previousElementSibling;
while (prevElement) {
if (prevElement.tagName === 'H3') {
prevElement.style.display = 'none';
break;
}
prevElement = prevElement.previousElementSibling;
}
console.log('Creating broadcast card for:', broadcast.url);
createBroadcastCard(broadcast.url, broadcast.div, broadcast.title, carouselContainer);
embedded++;
});
if (embedded > 0) {
console.log('Created', embedded, 'broadcast card(s)...');
} else {
console.warn('No broadcast cards created. Found', tweetDivs.length, 'tweet divs');
}
return embedded;
}
var observerDisconnected = false;
// Try multiple times with delays to catch dynamically loaded content
function tryEmbedding() {
var result = embedTweets();
if (result === 0) {
// If no elements found, try again after a delay
setTimeout(function() {
console.log('Retrying embed after delay...');
embedTweets();
}, 1000);
}
}
// Try immediately
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', function() {
tryEmbedding();
});
} else {
tryEmbedding();
}
// Also try after window load
window.addEventListener('load', function() {
setTimeout(function() {
console.log('Trying embed after window load...');
embedTweets();
}, 500);
});
// Also watch for dynamically added content
if (window.MutationObserver && !observerDisconnected) {
var observer = new MutationObserver(function(mutations) {
if (observerDisconnected) {
return;
}
var shouldCheck = false;
mutations.forEach(function(mutation) {
mutation.addedNodes.forEach(function(node) {
if (node.nodeType === 1) {
if (node.matches && node.matches('div.twitter-tweet')) {
shouldCheck = true;
} else if (node.querySelector && node.querySelector('div.twitter-tweet')) {
shouldCheck = true;
}
}
});
});
if (shouldCheck) {
embedTweets();
}
});
var target = document.body || document.documentElement;
if (target) {
observer.observe(target, {
childList: true,
subtree: true
});
setTimeout(function() {
observer.disconnect();
observerDisconnected = true;
}, 5000);
}
}
console.log('X broadcast link script initialized');
})();