-
-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathbluesky-thread.html
More file actions
426 lines (403 loc) · 14.2 KB
/
bluesky-thread.html
File metadata and controls
426 lines (403 loc) · 14.2 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Bluesky Thread Viewer</title>
<style>
:root {
--depth-color-1: hsl(200, 70%, 50%);
--depth-color-2: hsl(30, 80%, 50%);
--depth-color-3: hsl(120, 60%, 40%);
--depth-color-4: hsl(0, 70%, 50%);
--depth-color-5: hsl(280, 60%, 50%);
--depth-color-6: hsl(20, 60%, 40%);
--depth-color-7: hsl(330, 60%, 50%);
--depth-color-8: hsl(0, 0%, 40%);
}
body {
font-family: sans-serif;
margin: 1em;
}
header {
max-width: 800px;
}
.controls {
margin-bottom: 1em;
}
form {
display: flex;
gap: 0.5em;
}
input[type="text"] {
flex: 1;
font-size: 1rem;
padding: 0.5em;
}
#urlForm button {
background-color: #007bff;
color: white;
border: none;
padding: 0.5em 1em;
border-radius: 4px;
font-size: 1rem;
cursor: pointer;
}
.copy-container {
display: none;
margin-bottom: 1em;
}
#copyBtn,
#copyJsonBtn {
background-color: #28a745;
color: white;
border: none;
padding: 0.5em 1em;
border-radius: 4px;
font-size: 1rem;
cursor: pointer;
margin-right: 0.5em;
}
.tabs {
display: none;
margin-bottom: 1em;
border-bottom: 2px solid #ddd;
}
.tab {
background: none;
border: none;
padding: 0.75em 1.5em;
font-size: 1rem;
cursor: pointer;
border-bottom: 3px solid transparent;
margin-bottom: -2px;
color: #666;
}
.tab:hover {
color: #333;
}
.tab.active {
color: #007bff;
border-bottom-color: #007bff;
font-weight: bold;
}
.reply-to {
font-size: 0.8rem;
color: #888;
margin-bottom: 0.25em;
}
.reply-to a {
color: #007bff;
text-decoration: none;
}
.reply-to a:hover {
text-decoration: underline;
}
.post.highlighted {
background-color: #fffde7;
transition: background-color 0.3s;
}
.post {
position: relative;
border: 1px solid #ccc;
padding: 0.75em 2px 0.75em 0.75em;
border-radius: 6px;
margin-top: 1em;
}
.post::before {
content: '';
position: absolute;
top: 0;
bottom: 0;
left: 0;
width: 4px;
background-color: var(--stripe-color, transparent);
border-top-left-radius: 6px;
border-bottom-left-radius: 6px;
}
.depth-1 { --stripe-color: var(--depth-color-1); }
.depth-2 { --stripe-color: var(--depth-color-2); }
.depth-3 { --stripe-color: var(--depth-color-3); }
.depth-4 { --stripe-color: var(--depth-color-4); }
.depth-5 { --stripe-color: var(--depth-color-5); }
.depth-6 { --stripe-color: var(--depth-color-6); }
.depth-7 { --stripe-color: var(--depth-color-7); }
.depth-8 { --stripe-color: var(--depth-color-8); }
.author {
font-weight: bold;
margin-bottom: 0.25em;
}
.meta {
color: #666;
font-size: 0.85rem;
margin-bottom: 0.5em;
display: flex;
gap: 0.5em;
align-items: center;
}
.meta a {
font-size: 0.85rem;
color: #007bff;
text-decoration: none;
}
.text {
white-space: pre-wrap;
font-size: 1rem;
line-height: 1.4;
}
@media (max-width: 600px) {
.post { padding-left: 1em; }
}
</style>
</head>
<body>
<header>
<h1>Bluesky Thread Viewer</h1>
<div class="controls">
<form id="urlForm">
<input type="text" id="postUrl" placeholder="Bluesky post URL" required />
<button type="submit">Fetch Thread</button>
</form>
</div>
<div class="copy-container">
<button id="copyBtn">Copy</button>
<button id="copyJsonBtn">Copy JSON</button>
</div>
<div class="tabs" id="viewTabs">
<button class="tab active" data-view="thread">Thread View</button>
<button class="tab" data-view="recent">Most Recent First</button>
</div>
</header>
<div id="threadContainer"></div>
<script>
(async () => {
const form = document.getElementById('urlForm');
const container = document.getElementById('threadContainer');
const copyBtn = document.getElementById('copyBtn');
const copyJsonBtn = document.getElementById('copyJsonBtn');
const copyContainer = document.querySelector('.copy-container');
const viewTabs = document.getElementById('viewTabs');
const postUrl = document.getElementById('postUrl');
let lastThread = null;
let allPosts = []; // Flat array of all posts with metadata
let currentView = 'thread';
postUrl.addEventListener('keydown', (e) => {
if (e.key === 'Enter') { e.preventDefault(); form.requestSubmit(); }
});
// Extract post ID from URI for use as element ID
function getPostId(uri) {
return 'post-' + uri.split('/').pop();
}
// Flatten thread into array of posts with parent info
function flattenThread(item, parentUri = null, parentAuthor = null) {
const posts = [];
posts.push({
item,
parentUri,
parentAuthor,
uri: item.post.uri,
createdAt: new Date(item.post.record.createdAt)
});
if (item.replies && item.replies.length) {
const authorName = item.post.author.displayName || item.post.author.handle;
item.replies.forEach(reply => {
posts.push(...flattenThread(reply, item.post.uri, authorName));
});
}
return posts;
}
// Fixed function to generate thread text in a readable format
function generateThreadText(thread) {
const lines = [];
function processPost(item, prefix = '1') {
const author = item.post.author.displayName || item.post.author.handle;
const text = item.post.record.text.replace(/\n/g, ' ');
lines.push(`[${prefix}] ${author}: ${text}`);
if (item.replies && item.replies.length > 0) {
item.replies.forEach((reply, i) => {
processPost(reply, `${prefix}.${i+1}`);
});
}
}
processPost(thread);
return lines.join('\n\n');
}
// Display post in thread view (nested)
function displayPostThread(item, parent, depth = 1) {
const el = document.createElement('div');
el.className = `post depth-${Math.min(depth, 8)}`;
el.id = getPostId(item.post.uri);
const authorEl = document.createElement('div');
authorEl.className = 'author';
authorEl.textContent = `${item.post.author.displayName} (@${item.post.author.handle})`;
const metaEl = document.createElement('div');
metaEl.className = 'meta';
metaEl.textContent = new Date(item.post.record.createdAt).toLocaleString();
const link = document.createElement('a');
link.href = `https://bsky.app/profile/${item.post.author.handle}/post/${item.post.uri.split('/').pop()}`;
link.textContent = 'View';
link.target = '_blank';
metaEl.appendChild(link);
const textEl = document.createElement('div');
textEl.className = 'text';
textEl.textContent = item.post.record.text;
el.append(authorEl, metaEl, textEl);
parent.appendChild(el);
if (item.replies && item.replies.length) item.replies.forEach(reply => displayPostThread(reply, el, depth+1));
}
// Display posts in chronological order (most recent first)
function displayPostsChronological() {
container.innerHTML = '';
const sorted = [...allPosts].sort((a, b) => b.createdAt - a.createdAt);
sorted.forEach(postData => {
const item = postData.item;
const el = document.createElement('div');
el.className = 'post depth-1';
el.id = getPostId(item.post.uri);
// Add "in reply to" if this is a reply
if (postData.parentUri) {
const replyToEl = document.createElement('div');
replyToEl.className = 'reply-to';
const replyLink = document.createElement('a');
replyLink.href = '#' + getPostId(postData.parentUri);
replyLink.textContent = `in reply to ${postData.parentAuthor}`;
replyLink.addEventListener('click', (e) => {
e.preventDefault();
const targetEl = document.getElementById(getPostId(postData.parentUri));
if (targetEl) {
targetEl.scrollIntoView({ behavior: 'smooth', block: 'center' });
targetEl.classList.add('highlighted');
setTimeout(() => targetEl.classList.remove('highlighted'), 2000);
}
});
replyToEl.appendChild(replyLink);
el.appendChild(replyToEl);
}
const authorEl = document.createElement('div');
authorEl.className = 'author';
authorEl.textContent = `${item.post.author.displayName} (@${item.post.author.handle})`;
const metaEl = document.createElement('div');
metaEl.className = 'meta';
metaEl.textContent = new Date(item.post.record.createdAt).toLocaleString();
const link = document.createElement('a');
link.href = `https://bsky.app/profile/${item.post.author.handle}/post/${item.post.uri.split('/').pop()}`;
link.textContent = 'View';
link.target = '_blank';
metaEl.appendChild(link);
const textEl = document.createElement('div');
textEl.className = 'text';
textEl.textContent = item.post.record.text;
el.append(authorEl, metaEl, textEl);
container.appendChild(el);
});
}
// Display thread view
function displayThreadView() {
container.innerHTML = '';
if (lastThread) {
displayPostThread(lastThread, container);
}
}
// Render current view
function renderCurrentView() {
if (currentView === 'thread') {
displayThreadView();
} else {
displayPostsChronological();
}
}
// Tab click handlers
viewTabs.addEventListener('click', (e) => {
if (e.target.classList.contains('tab')) {
const view = e.target.dataset.view;
if (view !== currentView) {
currentView = view;
viewTabs.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));
e.target.classList.add('active');
renderCurrentView();
}
}
});
copyBtn.addEventListener('click', async () => {
if (!lastThread) return;
try {
const textToCopy = generateThreadText(lastThread);
await navigator.clipboard.writeText(textToCopy);
const orig = copyBtn.textContent;
copyBtn.textContent = 'Copied!';
setTimeout(() => (copyBtn.textContent = orig), 2000);
} catch (err) {
console.error('Copy failed', err);
}
});
copyJsonBtn.addEventListener('click', async () => {
if (!lastThread) return;
const jsonToCopy = JSON.stringify(lastThread, null, 2);
try {
await navigator.clipboard.writeText(jsonToCopy);
const orig = copyJsonBtn.textContent;
copyJsonBtn.textContent = 'Copied!';
setTimeout(() => (copyJsonBtn.textContent = orig), 2000);
} catch (err) {
console.error('Copy JSON failed', err);
}
});
form.addEventListener('submit', async (e) => {
e.preventDefault();
container.innerHTML = '';
copyContainer.style.display = 'none';
viewTabs.style.display = 'none';
lastThread = null;
allPosts = [];
currentView = 'thread';
// Reset tabs to default
viewTabs.querySelectorAll('.tab').forEach(t => {
t.classList.toggle('active', t.dataset.view === 'thread');
});
// Update URL with the submitted post URL
const newUrl = new URL(window.location);
newUrl.searchParams.set('url', postUrl.value.trim());
history.replaceState(null, '', newUrl);
try {
const url = new URL(postUrl.value.trim());
if (url.hostname !== 'bsky.app') throw new Error('URL must be from bsky.app');
const parts = url.pathname.split('/').filter(Boolean);
if (parts[0] !== 'profile' || parts[2] !== 'post') throw new Error('Use https://bsky.app/profile/{handle}/post/{postId}');
const rawHandle = parts[1];
const actor = rawHandle.includes('.') ? rawHandle : `${rawHandle}.bsky.social`;
const postId = parts[3];
const profileRes = await fetch(
`https://public.api.bsky.app/xrpc/app.bsky.actor.getProfile?actor=${encodeURIComponent(actor)}`
);
if (!profileRes.ok) throw new Error('Profile fetch failed ' + profileRes.status);
const profileJson = await profileRes.json();
const did = profileJson.did || profileJson.profile?.did;
if (!did) throw new Error('Could not parse DID');
const atUri = `at://${did}/app.bsky.feed.post/${postId}`;
const threadRes = await fetch(
`https://public.api.bsky.app/xrpc/app.bsky.feed.getPostThread?uri=${encodeURIComponent(atUri)}&depth=1000&parentHeight=0`
);
if (!threadRes.ok) throw new Error('Thread fetch failed ' + threadRes.status);
const threadJson = await threadRes.json();
if (threadJson.thread.$type === 'app.bsky.feed.defs#notFoundPost') throw new Error('Post not found');
lastThread = threadJson.thread;
allPosts = flattenThread(threadJson.thread);
renderCurrentView();
copyContainer.style.display = 'block';
viewTabs.style.display = 'block';
} catch (err) {
console.error(err);
container.textContent = 'Error: ' + err.message;
}
});
// Check for ?url= query parameter on page load
const params = new URLSearchParams(window.location.search);
const urlParam = params.get('url');
if (urlParam) {
postUrl.value = urlParam;
form.requestSubmit();
}
})();
</script>
</body>
</html>