-
-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathbluesky-thread.html
253 lines (240 loc) · 8.4 KB
/
bluesky-thread.html
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
<!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;
}
.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>
</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 postUrl = document.getElementById('postUrl');
let lastThread = null;
postUrl.addEventListener('keydown', (e) => {
if (e.key === 'Enter') { e.preventDefault(); form.requestSubmit(); }
});
// 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');
}
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';
lastThread = null;
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');
function displayPost(item, parent, depth = 1) {
const el = document.createElement('div');
el.className = `post depth-${Math.min(depth, 8)}`;
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 => displayPost(reply, el, depth+1));
}
displayPost(threadJson.thread, container);
lastThread = threadJson.thread;
copyContainer.style.display = 'block';
} catch (err) {
console.error(err);
container.textContent = 'Error: ' + err.message;
}
});
})();
</script>
</body>
</html>