-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
171 lines (144 loc) · 5.24 KB
/
script.js
File metadata and controls
171 lines (144 loc) · 5.24 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
// FULL SALSING - Minimal JavaScript for maximum spice with minimal calories
// Traffic-optimized gossip delivery system
const CAROUSEL_SENTENCES = [
'Tech news with a spicy flavour',
'Sriracha based tech gossip',
'Your unique source of truth',
'Idiocracy++',
'Sensationalism As A Service',
'Silicon Valley\'s guilty pleasure',
'Jalapeño-infused hot takes',
'Burning through your feed',
'Tech drama, extra crispy',
'Hacker News but spicier',
'NaCl with attitude',
'FOMO as a feature',
'Clickbait you can trust',
'Too hot for Hacker News',
'Chaos engineering for feeds',
'Weaponized tech takes',
'Where nuance goes to die',
'Viral by design',
'Your daily dose of drama',
'Tech tabloid, zero apologies'
];
let newsStore = [];
let currentCarouselIndex = 0;
// Initialize the page
document.addEventListener('DOMContentLoaded', () => {
startCarousel();
loadNews();
});
// Start carousel rotation
function startCarousel() {
const carouselText = document.getElementById('carousel-text');
if (!carouselText) return;
// Set initial sentence
carouselText.textContent = CAROUSEL_SENTENCES[currentCarouselIndex];
// Rotate every 4 seconds
setInterval(() => {
currentCarouselIndex = (currentCarouselIndex + 1) % CAROUSEL_SENTENCES.length;
carouselText.textContent = CAROUSEL_SENTENCES[currentCarouselIndex];
// Restart animation by removing and re-adding class
carouselText.style.animation = 'none';
setTimeout(() => {
carouselText.style.animation = '';
}, 10);
}, 4000);
}
// Load news from JSON file
function loadNews() {
fetch('./news-data.json')
.then(response => response.json())
.then(data => {
newsStore = (data.news || []).reverse();
renderNews();
})
.catch(error => {
console.error('Error loading news:', error);
newsStore = [];
renderNews();
});
}
// Render all news blocks
function renderNews() {
const container = document.getElementById('blocks-container');
container.innerHTML = '';
let hasEmbeds = false;
newsStore.forEach(news => {
const block = createNewsBlock(news);
container.appendChild(block);
if (news.embed_html) {
hasEmbeds = true;
}
});
// Load Twitter script if there are embeds
if (hasEmbeds && !window.twttr) {
const script = document.createElement('script');
script.src = 'https://platform.twitter.com/widgets.js';
script.async = true;
script.charset = 'utf-8';
script.onload = () => {
// Process tweets after script loads
if (window.twttr && window.twttr.widgets) {
window.twttr.widgets.load();
}
};
document.body.appendChild(script);
} else if (hasEmbeds && window.twttr && window.twttr.widgets) {
// Script already loaded, just process
window.twttr.widgets.load();
}
}
// Create a single news block element
function createNewsBlock(news) {
const block = document.createElement('article');
block.className = `news-block ${news.size}`;
const imageHTML = news.image ? `<img src="${news.image}" alt="${news.title}" class="block-image" loading="lazy">` : '';
block.innerHTML = `
${imageHTML}
<span class="block-category">${news.category}</span>
<h2 class="block-title">${news.title}</h2>
<p class="block-content">${news.content}</p>
`;
// Add link button with sriracha emoji (use link or tweet_url)
const linkUrl = news.link || news.tweet_url;
if (linkUrl) {
const linkButton = document.createElement('a');
linkButton.href = linkUrl;
linkButton.target = '_blank';
linkButton.className = 'block-link-button';
linkButton.innerHTML = '<img src="./sriracha.webp" alt="link" class="link-icon"> Link...';
block.appendChild(linkButton);
}
// Generate Twitter embed if link is a Twitter URL and no custom embed provided
let embedHtml = news.embed_html;
if (!embedHtml && linkUrl && isTwitterUrl(linkUrl)) {
embedHtml = generateTwitterEmbed(linkUrl);
}
// Add Twitter embed if available
if (embedHtml) {
const embedContainer = document.createElement('div');
embedContainer.className = 'tweet-embed-container';
embedContainer.innerHTML = embedHtml;
block.appendChild(embedContainer);
}
return block;
}
// Check if URL is a Twitter/X URL
function isTwitterUrl(url) {
return /(?:twitter\.com|x\.com)\/\w+\/status\/\d+/.test(url);
}
// Extract tweet ID from Twitter URL
function extractTweetId(url) {
const match = url.match(/(?:twitter\.com|x\.com)\/\w+\/status\/(\d+)/);
return match ? match[1] : null;
}
// Generate Twitter embed HTML from URL
function generateTwitterEmbed(tweetUrl) {
const tweetId = extractTweetId(tweetUrl);
if (!tweetId) return null;
// Convert x.com to twitter.com for embed
const twitterUrl = `https://twitter.com/i/web/status/${tweetId}`;
return `<blockquote class="twitter-tweet" data-media-max-width="560"><a href="${twitterUrl}"></a></blockquote>`;
}