forked from tronicum/inkpour
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
515 lines (458 loc) · 21.9 KB
/
Copy pathbackground.js
File metadata and controls
515 lines (458 loc) · 21.9 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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/**
* background.js — Inkpour service worker
* Handles keyboard shortcut commands and context menus without needing the popup open.
*/
// ─── Shared utilities (buildMarkdown, buildFilename, buildJSON, buildZip, etc.)
importScripts('src/utils.js');
// ─── Secret scrubbing (scanForSecrets, redactSecrets) ─────────────────────
importScripts('src/redact.js');
const api = (typeof browser !== 'undefined') ? browser : chrome;
// ─── Safari downloads polyfill ────────────────────────────────────────────
// Safari does not implement browser.downloads — fall back to injecting a
// temporary <a download> click into the active tab via the content script.
const hasBrowserDownloads = !!(api.downloads && api.downloads.download);
function safeDownload(tabId, url, filename) {
if (hasBrowserDownloads) {
api.downloads.download({ url, filename, saveAs: false });
} else {
// Safari path: ask content script to do the <a download> trick
api.tabs.sendMessage(tabId, { action: 'safariDownload', url, filename });
// Clean up object URL after a delay (content script may need a moment)
setTimeout(() => { try { URL.revokeObjectURL(url); } catch (_) {} }, 5000);
}
}
// ─── Downloads subfolder helper ───────────────────────────────────────────
// obsidianVault takes precedence over downloadSubfolder when set.
function withSubfolder(settings, filename) {
const sub = ((settings.obsidianVault || settings.downloadSubfolder || '')).trim().replace(/\/+$/, '');
return sub ? sub + '/' + filename : filename;
}
// ─── In-page button export requests ──────────────────────────────────────
// Content script sends { action: 'inPageExport', format: 'pdf'|'zip'|'docx'|'html' }
// Background handles it so the SW can download files / open tabs.
api.runtime.onMessage.addListener((message, sender) => {
if (message.action !== 'inPageExport' || !sender.tab?.id) return;
(async () => {
const tabId = sender.tab.id;
let response;
try {
// In-page FAB export has no popup UI to show its own "Extracting…"
// status (unlike the popup, which shows this inline) — the auto-scroll
// lazy-loading step in extractMessages() can take a couple of seconds
// on ChatGPT/Gemini/AI Studio, so without this the page just sits there
// with no feedback. Fire-and-forget: if the toast fails to show for any
// reason, the export itself must not be blocked by it.
api.tabs.sendMessage(tabId, { action: 'showToast', text: api.i18n.getMessage('popupStatusExtracting') }).catch(() => {});
response = await api.tabs.sendMessage(tabId, { action: 'extract' });
} catch { return; }
if (!response?.messages?.length) return;
const stored = await api.storage.local.get('inkpour_settings');
const settings = Object.assign(
{ yamlFrontMatter: false, generateTOC: false, filenameTemplate: '{platform}-{title}', downloadSubfolder: '', obsidianVault: '', obsidianTags: false, scrubSecrets: true },
stored?.inkpour_settings ?? {}
);
const sourceUrl = sender.tab.url || '';
const wordCount = (response.messages || []).reduce((s, m) => s + m.content.trim().split(/\s+/).filter(Boolean).length, 0);
const filename = buildFilename(settings.filenameTemplate, response.platform, response.filename, sourceUrl, wordCount, (response.messages||[]).length);
if (message.format === 'pdf') {
const bodyContent = buildPrintBodyHTML(response.messages, response.title, response.site);
await api.storage.local.set({ inkpour_print_pending: bodyContent });
api.tabs.create({ url: api.runtime.getURL('print.html') });
}
if (message.format === 'zip') {
const { files } = buildZipExport(response.messages, response.title, response.site, settings, sourceUrl);
const zipBytes = buildZip(files);
const b64 = uint8ToBase64(zipBytes);
const url = 'data:application/zip;base64,' + b64;
safeDownload(tabId, url, withSubfolder(settings, filename + '.zip'));
}
if (message.format === 'docx') {
const docxBytes = buildDocx(response.messages, response.title, response.site, settings, sourceUrl);
const b64 = uint8ToBase64(docxBytes);
const url = 'data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64,' + b64;
safeDownload(tabId, url, withSubfolder(settings, filename + '.docx'));
}
if (message.format === 'html') {
const html = buildStandaloneHTML(response.messages, response.title, response.site);
const url = 'data:text/html;charset=utf-8,' + encodeURIComponent(html);
safeDownload(tabId, url, withSubfolder(settings, filename + '.html'));
}
})();
});
// ─── Context menu setup ───────────────────────────────────────────────────
api.runtime.onInstalled.addListener(() => {
// Parent item only appears on supported AI chat pages — reuse the exact
// match patterns from manifest.json's content_scripts entry (read via the
// manifest itself, not a separately maintained list) so this can't drift
// out of sync the way SUPPORTED_HOSTS below already has (still lists
// phind.com, still missing www.meta.ai/arena.ai). Only the parent item
// needs documentUrlPatterns: the child items below are only reachable
// through its submenu, so if the parent doesn't match, they never show.
const documentUrlPatterns = api.runtime.getManifest()?.content_scripts?.[0]?.matches;
api.contextMenus.create({
id: 'inkpour-parent',
title: 'Export with Inkpour',
contexts: ['page'],
...(documentUrlPatterns ? { documentUrlPatterns } : {}),
});
api.contextMenus.create({
id: 'inkpour-md',
parentId: 'inkpour-parent',
title: 'Export Markdown',
contexts: ['page'],
});
api.contextMenus.create({
id: 'inkpour-copy',
parentId: 'inkpour-parent',
title: 'Copy Markdown',
contexts: ['page'],
});
api.contextMenus.create({
id: 'inkpour-json',
parentId: 'inkpour-parent',
title: 'Export JSON',
contexts: ['page'],
});
api.contextMenus.create({
id: 'inkpour-zip',
parentId: 'inkpour-parent',
title: 'Export ZIP (chat + code files)',
contexts: ['page'],
});
api.contextMenus.create({
id: 'inkpour-docx',
parentId: 'inkpour-parent',
title: 'Export Word document (.docx)',
contexts: ['page'],
});
api.contextMenus.create({
id: 'inkpour-gist',
parentId: 'inkpour-parent',
title: 'Upload to GitHub Gist',
contexts: ['page'],
});
});
api.contextMenus.onClicked.addListener(async (info, tab) => {
if (!tab?.id || !info.menuItemId.startsWith('inkpour-')) return;
if (info.menuItemId === 'inkpour-parent') return;
let response;
try {
// Same "Extracting…" feedback as the in-page FAB path above — the
// right-click menu has no status UI of its own either.
api.tabs.sendMessage(tab.id, { action: 'showToast', text: api.i18n.getMessage('popupStatusExtracting') }).catch(() => {});
response = await api.tabs.sendMessage(tab.id, { action: 'extract' });
} catch {
return;
}
if (!response?.messages?.length) return;
const stored = await api.storage.local.get('inkpour_settings');
const settings = Object.assign(
{ yamlFrontMatter: false, generateTOC: false, filenameTemplate: '{platform}-{title}', downloadSubfolder: '', obsidianVault: '', obsidianTags: false, scrubSecrets: true },
stored?.inkpour_settings ?? {}
);
const sourceUrl = tab?.url || '';
const wordCount = (response.messages || []).reduce((s, m) => s + m.content.trim().split(/\s+/).filter(Boolean).length, 0);
const filename = buildFilename(settings.filenameTemplate, response.platform, response.filename, sourceUrl, wordCount, (response.messages||[]).length);
if (info.menuItemId === 'inkpour-md') {
const md = buildMarkdown(response.messages, response.title, response.site, settings, sourceUrl);
const url = 'data:text/markdown;charset=utf-8,' + encodeURIComponent(md);
safeDownload(tab.id, url, withSubfolder(settings, filename + '.md'));
}
if (info.menuItemId === 'inkpour-copy') {
const md = buildMarkdown(response.messages, response.title, response.site, settings, sourceUrl);
await api.tabs.sendMessage(tab.id, { action: 'copyToClipboard', text: md });
}
if (info.menuItemId === 'inkpour-json') {
const json = buildJSON(response.messages, response.title, response.site, response.platform);
const url = 'data:application/json;charset=utf-8,' + encodeURIComponent(json);
safeDownload(tab.id, url, withSubfolder(settings, filename + '.json'));
}
if (info.menuItemId === 'inkpour-zip') {
const { files } = buildZipExport(response.messages, response.title, response.site, settings, sourceUrl);
const zipBytes = buildZip(files);
const b64 = uint8ToBase64(zipBytes);
const url = 'data:application/zip;base64,' + b64;
safeDownload(tab.id, url, withSubfolder(settings, filename + '.zip'));
}
if (info.menuItemId === 'inkpour-docx') {
const docxBytes = buildDocx(response.messages, response.title, response.site, settings, sourceUrl);
const b64 = uint8ToBase64(docxBytes);
const url = 'data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64,' + b64;
safeDownload(tab.id, url, withSubfolder(settings, filename + '.docx'));
}
if (info.menuItemId === 'inkpour-gist') {
await doGistUpload(tab, settings, response, sourceUrl, filename);
doWebhook(settings, 'gist', response, wordCount, tab.id);
return;
}
// Fire webhook after context-menu exports
const menuFormatMap = {
'inkpour-md': 'md',
'inkpour-copy': 'copy-md',
'inkpour-json': 'json',
'inkpour-zip': 'zip',
'inkpour-docx': 'docx',
};
const fmt = menuFormatMap[info.menuItemId];
if (fmt) doWebhook(settings, fmt, response, wordCount, tab.id);
});
// ─── Webhook helper ───────────────────────────────────────────────────────
async function doWebhook(settings, format, response, wordCount, tabId) {
const url = (settings.webhookUrl || '').trim();
if (!url) return;
let title = response.title;
// Scrub likely secrets from any free-text fields before they leave the
// machine. background.js's webhook payload doesn't carry full chat content
// today (see below), but the title can be arbitrary text copied from the
// page, so it's still worth passing through the scrubber.
if (settings.scrubSecrets !== false) {
const { cleaned, findings } = redactSecrets(title || '');
title = cleaned;
if (findings.length > 0 && tabId != null) {
const types = [...new Set(findings.map(f => f.type))].join(', ');
const key = findings.length === 1 ? 'contentToastRedactedOne' : 'contentToastRedactedOther';
api.tabs.sendMessage(tabId, {
action: 'showToast',
text: api.i18n.getMessage(key, [String(findings.length), types]),
variant: 'info',
}).catch(() => {});
}
}
const record = {
source: 'inkpour',
id: Date.now().toString(),
title,
platform: response.platform,
format,
messageCount: (response.messages || []).length,
wordCount,
exportedAt: new Date().toISOString(),
};
// Optionally include the content — background.js doesn't have the built
// markdown/json at this point in all branches, so we omit content here.
// Content-inclusive webhooks should be configured via the popup export buttons.
fetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(record),
}).catch(() => {}); // best-effort
}
// ─── GitHub Gist upload helper ────────────────────────────────────────────
async function doGistUpload(tab, settings, response, sourceUrl, filename) {
const token = settings.githubToken || '';
if (!token) {
api.runtime.openOptionsPage();
return;
}
// Gist exports always carry YAML front matter + base tags for GitHub search.
const gistSettings = { ...settings, yamlFrontMatter: true, obsidianTags: true, gistExtraTags: settings.gistTags || '' };
let md = buildMarkdown(response.messages, response.title, response.site, gistSettings, sourceUrl);
let description = response.title;
const gistFilename = filename + '.md';
// Scrub likely secrets (API keys, tokens, emails, ...) before the content
// leaves the machine, unless the user has explicitly disabled this.
if (settings.scrubSecrets !== false) {
const bodyResult = redactSecrets(md);
const titleResult = redactSecrets(description);
md = bodyResult.cleaned;
description = titleResult.cleaned;
const allFindings = [...bodyResult.findings, ...titleResult.findings];
if (allFindings.length > 0) {
const types = [...new Set(allFindings.map(f => f.type))].join(', ');
const key = allFindings.length === 1 ? 'contentToastRedactedOne' : 'contentToastRedactedOther';
await api.tabs.sendMessage(tab.id, {
action: 'showToast',
text: api.i18n.getMessage(key, [String(allFindings.length), types]),
variant: 'info',
}).catch(() => {});
}
}
let res;
try {
res = await fetch('https://api.github.com/gists', {
method: 'POST',
headers: {
'Authorization': `token ${token}`,
'Accept': 'application/vnd.github.v3+json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
description,
public: settings.gistPublic === true,
files: { [gistFilename]: { content: md } },
}),
});
} catch {
await api.tabs.sendMessage(tab.id, {
action: 'showToast', text: api.i18n.getMessage('contentToastGistFailedNetwork'), variant: 'error',
}).catch(() => {});
return;
}
if (!res.ok) {
const errBody = await res.json().catch(() => ({}));
const msg = errBody.message || `HTTP ${res.status}`;
await api.tabs.sendMessage(tab.id, {
action: 'showToast', text: api.i18n.getMessage('contentToastGistFailed', [msg]), variant: 'error',
}).catch(() => {});
return;
}
const gist = await res.json();
api.tabs.create({ url: gist.html_url });
await api.tabs.sendMessage(tab.id, {
action: 'showToast', text: api.i18n.getMessage('contentToastGistCreated'), variant: 'success',
}).catch(() => {});
}
// ─── Keyboard shortcuts ───────────────────────────────────────────────────
// Chrome caps manifest.json's "commands" to 4 entries with a pre-suggested
// default keybinding (5+ triggers "Too many shortcuts specified... Could not
// load manifest" and the WHOLE extension fails to load in Chrome — unlike
// Firefox, which has no such cap). "upload-gist" lost its suggested_key for
// that reason. Its Alt+Shift+G hotkey still works via a content-script
// keydown listener (src/content.js) that sends a 'runShortcutCommand'
// runtime message here instead of going through chrome.commands — see
// runCommand() below, shared by both paths.
async function runCommand(command) {
const [tab] = await api.tabs.query({ active: true, currentWindow: true });
if (!tab) return;
let response;
try {
// Same "Extracting…" feedback as the in-page FAB and context-menu paths
// — keyboard shortcuts fire the export instantly with no visible UI at
// all otherwise.
api.tabs.sendMessage(tab.id, { action: 'showToast', text: api.i18n.getMessage('popupStatusExtracting') }).catch(() => {});
response = await api.tabs.sendMessage(tab.id, { action: 'extract' });
} catch {
return; // not a supported page
}
if (!response?.messages?.length) return;
// Load user settings so keyboard shortcuts respect all preferences
const stored = await api.storage.local.get('inkpour_settings');
const settings = Object.assign(
{ yamlFrontMatter: false, generateTOC: false, filenameTemplate: '{platform}-{title}', downloadSubfolder: '', obsidianVault: '', obsidianTags: false, scrubSecrets: true },
stored?.inkpour_settings ?? {}
);
const sourceUrl = tab.url || '';
const wordCount = (response.messages || []).reduce((s, m) => s + m.content.trim().split(/\s+/).filter(Boolean).length, 0);
const filename = buildFilename(settings.filenameTemplate, response.platform, response.filename, sourceUrl, wordCount, (response.messages||[]).length);
if (command === 'export-markdown') {
const md = buildMarkdown(response.messages, response.title, response.site, settings, sourceUrl);
const url = 'data:text/markdown;charset=utf-8,' + encodeURIComponent(md);
safeDownload(tab.id, url, withSubfolder(settings, filename + '.md'));
}
if (command === 'export-pdf') {
// SW has no localStorage — store in storage.local, print.js reads both
const bodyContent = buildPrintBodyHTML(response.messages, response.title, response.site);
await api.storage.local.set({ inkpour_print_pending: bodyContent });
api.tabs.create({ url: api.runtime.getURL('print.html') });
}
if (command === 'copy-markdown') {
// Service workers don't have clipboard access — send to content script to copy
const md = buildMarkdown(response.messages, response.title, response.site, settings, sourceUrl);
await api.tabs.sendMessage(tab.id, { action: 'copyToClipboard', text: md });
}
if (command === 'copy-html') {
// Copy full standalone HTML to clipboard via content script (no clipboard in SW)
const html = buildStandaloneHTML(response.messages, response.title, response.site);
await api.tabs.sendMessage(tab.id, { action: 'copyToClipboard', text: html });
}
if (command === 'export-json') {
const json = buildJSON(response.messages, response.title, response.site, response.platform);
const url = 'data:application/json;charset=utf-8,' + encodeURIComponent(json);
safeDownload(tab.id, url, withSubfolder(settings, filename + '.json'));
}
if (command === 'export-zip') {
const { files } = buildZipExport(response.messages, response.title, response.site, settings, sourceUrl);
const zipBytes = buildZip(files);
// base64-encode for data: URL (no createObjectURL in SW)
const b64 = uint8ToBase64(zipBytes);
const url = 'data:application/zip;base64,' + b64;
safeDownload(tab.id, url, withSubfolder(settings, filename + '.zip'));
}
if (command === 'export-docx') {
const docxBytes = buildDocx(response.messages, response.title, response.site, settings, sourceUrl);
const b64 = uint8ToBase64(docxBytes);
const url = 'data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64,' + b64;
safeDownload(tab.id, url, withSubfolder(settings, filename + '.docx'));
}
if (command === 'upload-gist') {
await doGistUpload(tab, settings, response, sourceUrl, filename);
doWebhook(settings, 'gist', response, wordCount, tab.id);
return; // gist handler already opened a tab + toasted
}
// Fire webhook after non-gist commands (best-effort)
const formatMap = {
'export-markdown': 'md',
'export-pdf': 'pdf',
'export-docx': 'docx',
'copy-markdown': 'copy-md',
'copy-html': 'copy-html',
'export-json': 'json',
'export-zip': 'zip',
};
const fmt = formatMap[command];
if (fmt) doWebhook(settings, fmt, response, wordCount, tab.id);
}
api.commands.onCommand.addListener((command) => { runCommand(command); });
// Fallback trigger for commands that don't fit Chrome's 4-shortcut cap
// (see comment above runCommand). content.js listens for the raw key
// combo itself and just tells us which command to run.
api.runtime.onMessage.addListener((msg, _sender, sendResponse) => {
if (msg?.action === 'runShortcutCommand' && msg.command) {
runCommand(msg.command);
sendResponse({ ok: true });
return true;
}
});
// ─── Action badge — show "ON" on supported AI chat pages ──────────────────
// Canonical source of truth is supported-sites.json — this flat array is
// derived from it (service workers can't fetch local extension files at runtime).
const SUPPORTED_HOSTS = [
'chatgpt.com', 'chat.openai.com',
'claude.ai',
'gemini.google.com', 'aistudio.google.com',
'www.google.com', 'google.com',
'copilot.microsoft.com', 'copilot.com', 'www.copilot.com',
'grok.com',
'console.groq.com',
'perplexity.ai',
'chat.deepseek.com',
'meta.ai', 'www.meta.ai',
'chat.mistral.ai',
'huggingface.co',
'poe.com',
'notebooklm.google.com',
'kagi.com',
'chat.z.ai',
'venice.ai',
'arena.ai', 'lmarena.ai', 'chat.lmsys.org',
'character.ai', 'www.character.ai',
'coral.cohere.com',
'pi.ai',
];
function updateBadge(tabId, url) {
if (!api.action?.setBadgeText) return; // not available in all browsers/contexts
let isSupported = false;
try {
const hostname = new URL(url).hostname;
isSupported = SUPPORTED_HOSTS.some(h => hostname === h || hostname.endsWith('.' + h));
} catch { /* not a real URL */ }
if (isSupported) {
api.action.setBadgeText({ text: 'ON', tabId });
api.action.setBadgeBackgroundColor({ color: '#16a34a', tabId }); // green
} else {
api.action.setBadgeText({ text: '', tabId });
}
}
// Update badge when a tab finishes loading
api.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status === 'complete' && tab.url) {
updateBadge(tabId, tab.url);
}
});
// Update badge when user switches tabs
api.tabs.onActivated.addListener(async ({ tabId }) => {
try {
const tab = await api.tabs.get(tabId);
if (tab?.url) updateBadge(tabId, tab.url);
} catch { /* tab may be gone */ }
});