-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader.js
More file actions
279 lines (231 loc) · 8.74 KB
/
Copy pathreader.js
File metadata and controls
279 lines (231 loc) · 8.74 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
import { parseUrl, formatTime, stripHtml } from './lib.js';
import { addBookmark } from './instapaper.js';
function setErrorStatus(container, statusEl, message) {
const el = document.createElement('div');
el.className = 'error';
el.textContent = message;
statusEl.textContent = '';
container.innerHTML = '';
container.appendChild(el);
}
function setStatus(container, statusEl, message, type) {
if (type === 'loading') {
setLoadingStatus(statusEl);
container.innerHTML = '';
return;
}
if (type === 'error') {
setErrorStatus(container, statusEl, message);
return;
}
setPlainStatus(statusEl, message);
}
function setPlainStatus(el, text) {
el.textContent = text;
}
function setLoadingStatus(el) {
el.innerHTML = '<span class="spinner">↻</span> Loading thread…';
}
function sanitizeHtml(html) {
const doc = new DOMParser().parseFromString(html, 'text/html');
doc.querySelectorAll('script, iframe, object, embed, form').forEach(el => el.remove());
for (const el of doc.querySelectorAll('*')) {
for (const attr of [...el.attributes]) {
if (attr.name.startsWith('on')) {
el.removeAttribute(attr.name);
}
else if (attr.name === 'href' || attr.name === 'src' || attr.name === 'action') {
if (!isSafeUrl(attr.value)) el.removeAttribute(attr.name);
}
}
}
return doc.body.innerHTML;
}
function isSafeUrl(url) {
if (!url) return false;
try {
const u = new URL(url);
return u.protocol === 'http:' || u.protocol === 'https:';
}
catch {
return false;
}
}
function renderPost(post, isFirst, showConnector) {
const acc = post.account;
const mediaItems = (post.media_attachments || []).filter(m => m.type === 'image');
const wrapper = document.createElement('div');
const postEl = document.createElement('div');
postEl.className = 'post';
const avatarCol = document.createElement('div');
avatarCol.className = 'avatar-col';
if (isFirst && acc.avatar) {
const img = document.createElement('img');
img.className = 'avatar';
img.src = acc.avatar;
img.alt = stripHtml(acc.display_name || '');
const fallback = document.createElement('div');
fallback.className = 'avatar-fallback';
fallback.textContent = (stripHtml(acc.display_name || acc.username || '?')).slice(0, 2).toUpperCase();
fallback.style.display = 'none';
img.onerror = () => { img.style.display = 'none'; fallback.style.display = 'flex'; };
avatarCol.appendChild(img);
avatarCol.appendChild(fallback);
}
if (showConnector) {
const connector = document.createElement('div');
connector.className = 'connector';
avatarCol.appendChild(connector);
}
const body = document.createElement('div');
body.className = 'post-body';
if (isFirst) {
const meta = document.createElement('div');
meta.className = 'post-meta';
const author = document.createElement('span');
author.className = 'post-author';
author.textContent = stripHtml(acc.display_name || acc.username);
const time = document.createElement('span');
time.className = 'post-time';
time.textContent = formatTime(post.created_at);
meta.appendChild(author);
meta.appendChild(time);
body.appendChild(meta);
}
const content = document.createElement('div');
content.className = 'post-content';
content.setHTML(sanitizeHtml(post.content));
body.appendChild(content);
if (mediaItems.length) {
const mediaDiv = document.createElement('div');
mediaDiv.className = 'post-media';
mediaItems.forEach(m => {
const img = document.createElement('img');
img.src = m.preview_url || m.url;
img.alt = m.description || '';
img.loading = 'lazy';
img.addEventListener('click', () => openLightbox(m.url || m.preview_url));
mediaDiv.appendChild(img);
});
body.appendChild(mediaDiv);
}
postEl.appendChild(avatarCol);
postEl.appendChild(body);
wrapper.appendChild(postEl);
return wrapper;
}
async function loadThread() {
let url = document.getElementById('url-input').value.trim();
const parsed = parseUrl(url);
const container = document.getElementById('thread-container');
const statusEl = document.getElementById('status');
if (!parsed) {
setStatus(container, statusEl, 'Invalid URL. Paste a valid Mastodon post URL.', 'error');
return;
}
// Construct safe URL from parsed instance + known-safe protocol
url = `${parsed.instance}/statuses/${parsed.id}`;
setStatus(container, statusEl, 'Loading thread…', 'loading');
container.innerHTML = '';
document.title = 'Loading thread…';
try {
const [postRes, ctxRes] = await Promise.all([
fetch(`${parsed.instance}/api/v1/statuses/${parsed.id}`),
fetch(`${parsed.instance}/api/v1/statuses/${parsed.id}/context`)
]);
if (!postRes.ok) throw new Error(`HTTP ${postRes.status} — post not found or instance unreachable`);
const [post, ctx] = await Promise.all([postRes.json(), ctxRes.json()]);
const authorId = post.account.id;
const ancestors = ctx.ancestors || [];
const descendants = (ctx.descendants || []).filter(p =>
p.account.id === authorId
&& (p.in_reply_to_account_id == null || p.in_reply_to_account_id === authorId)
);
const allPosts = [...ancestors, post, ...descendants];
const count = allPosts.length;
const displayName = stripHtml(post.account.display_name || post.account.username);
document.title = `Thread by ${displayName}`;
setPlainStatus(statusEl, `✓ ${count} post${count > 1 ? 's' : ''} loaded`);
const header = document.createElement('div');
header.className = 'thread-header';
const threadTitle = document.createElement('div');
threadTitle.className = 'thread-title';
threadTitle.textContent = `Thread by ${displayName}`;
const threadMeta = document.createElement('div');
threadMeta.className = 'thread-meta';
threadMeta.appendChild(document.createTextNode(
`${count} post${count > 1 ? 's' : ''} · `
));
const viewLink = document.createElement('a');
viewLink.href = url;
viewLink.target = '_blank';
viewLink.rel = 'noopener';
viewLink.textContent = 'View on Mastodon ↗';
threadMeta.appendChild(viewLink);
header.appendChild(threadTitle);
header.appendChild(threadMeta);
const thread = document.createElement('div');
thread.className = 'thread';
allPosts.forEach((p, i) => thread.appendChild(renderPost(p, i === 0, i < allPosts.length - 1)));
container.appendChild(header);
container.appendChild(thread);
const instaBtn = renderInstapaperButton(url, `Thread by ${displayName}`, container);
container.appendChild(instaBtn);
} catch(e) {
setStatus(container, statusEl, `Error: ${e.message}`, 'error');
document.title = 'Thread Reader — Error';
}
}
function openLightbox(src) {
document.getElementById('lightbox-img').src = src;
document.getElementById('lightbox').classList.add('open');
}
function closeLightbox() {
document.getElementById('lightbox').classList.remove('open');
}
function renderInstapaperButton(postUrl, title, container) {
const btn = document.createElement('button');
btn.id = 'instapaper-btn';
btn.textContent = 'Save to Instapaper';
btn.style.cssText = `
display: inline-flex; align-items: center; gap: 6px;
font-family: inherit; font-size: 13px; font-weight: 500;
padding: 7px 14px; border: 0.5px solid var(--border-hover);
border-radius: var(--radius); background: var(--bg); color: var(--text);
cursor: pointer; margin-top: 1rem;
`;
btn.addEventListener('click', async () => {
const { instaToken, instaSecret } = await browser.storage.local.get(['instaToken', 'instaSecret']);
if (!instaToken) {
browser.runtime.openOptionsPage();
return;
}
btn.textContent = 'Saving…';
btn.disabled = true;
try {
const content = container.querySelector('.thread').outerHTML;
await addBookmark(instaToken, instaSecret, { url: postUrl, title, content });
btn.textContent = '✓ Saved to Instapaper';
} catch (e) {
btn.textContent = 'Error — retry';
btn.disabled = false;
console.error(e);
}
});
return btn;
}
document.addEventListener('DOMContentLoaded', () => {
const btn = document.getElementById('load-btn');
const input = document.getElementById('url-input');
const lightbox = document.getElementById('lightbox');
btn.addEventListener('click', loadThread);
input.addEventListener('keydown', e => { if (e.key === 'Enter') loadThread(); });
lightbox.addEventListener('click', closeLightbox);
document.addEventListener('keydown', e => { if (e.key === 'Escape') closeLightbox(); });
const params = new URLSearchParams(globalThis.location.search);
const urlParam = params.get('url');
if (urlParam) {
input.value = urlParam;
loadThread();
}
});