flow-extension/background.js
type-two 501431bf59 Flow rinse: browser downloads, fast-fail, autoStart, launcher + self-healing launchd
- background.js: chrome.downloads via page session (no CSP/base64)
- content.js: fast-fail on Flow error banner, image vs video timeouts, autoStart
- local_server_example.py: /enqueue + /save, downloads/ output, resume-skip
- launch.sh: idempotent server-up + caffeinate + single-tab focus
- install.sh + plist: self-healing agent every 10 min (TCC-safe App Support copy)
- flowclient.py: enqueue/wait_for helper for game projects
- README: full setup + macOS TCC gotcha

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-06 01:20:32 +10:00

65 lines
2.8 KiB
JavaScript

// Background service worker.
// * Trusted input via the Chrome DevTools Protocol — Flow's editor ignores synthetic
// DOM events (isTrusted=false); CDP Input.* events are trusted so it registers them.
// * Asset download — fetches the harvested media URL with the page's session cookies
// (host_permission covers labs.google) and hands the bytes to the local server. Runs
// in the worker, so it's not blocked by the page's CSP.
const attached = new Set();
function attach(tabId) {
return new Promise((res, rej) =>
chrome.debugger.attach({ tabId }, '1.3', () =>
chrome.runtime.lastError ? rej(chrome.runtime.lastError) : res()));
}
function cmd(tabId, method, params) {
return new Promise((res, rej) =>
chrome.debugger.sendCommand({ tabId }, method, params || {}, (r) =>
chrome.runtime.lastError ? rej(chrome.runtime.lastError) : res(r)));
}
async function ensure(tabId) {
if (attached.has(tabId)) return;
try { await attach(tabId); attached.add(tabId); }
catch (e) {
if (/already attached/i.test(String(e.message || e))) attached.add(tabId);
else throw e;
}
}
const ENTER = { key: 'Enter', code: 'Enter', windowsVirtualKeyCode: 13, nativeVirtualKeyCode: 13 };
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
const tabId = sender.tab && sender.tab.id;
(async () => {
try {
if (msg.type === 'insertText') {
await ensure(tabId);
await cmd(tabId, 'Input.insertText', { text: msg.text });
sendResponse({ ok: true });
} else if (msg.type === 'pressEnter') {
await ensure(tabId);
await cmd(tabId, 'Input.dispatchKeyEvent', { type: 'keyDown', ...ENTER });
await cmd(tabId, 'Input.dispatchKeyEvent', { type: 'keyUp', ...ENTER });
sendResponse({ ok: true });
} else if (msg.type === 'download') {
// Browser-native download: uses the page's session cookies, follows the media
// redirect, no CSP/CORS/base64. Lands under the browser's Downloads dir.
const dlId = await new Promise((res, rej) => chrome.downloads.download(
{ url: msg.url, filename: msg.filename, conflictAction: 'uniquify', saveAs: false },
(id) => chrome.runtime.lastError ? rej(chrome.runtime.lastError) : res(id)));
sendResponse({ ok: dlId != null, id: dlId });
} else if (msg.type === 'detach') {
if (attached.has(tabId)) { chrome.debugger.detach({ tabId }, () => {}); attached.delete(tabId); }
sendResponse({ ok: true });
} else {
sendResponse({ ok: false, error: 'unknown message' });
}
} catch (e) {
sendResponse({ ok: false, error: String(e.message || e) });
}
})();
return true; // async response
});
chrome.debugger.onDetach.addListener((src) => { if (src.tabId) attached.delete(src.tabId); });