Skip to content

Open-source AI browser agent for Chrome and Firefox (monorepo) 🧠

Load Firefox injected scripts from the extension root

Emre Sokullud6aafd2c2901
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b9a737e5b..96d9c3083 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,9 @@ This changelog was generated from the repository Git history and release tags. V
## [Unreleased]
+### Fixed
+- Firefox: **download_social_media** saves media instead of stopping with "An unexpected error occurred", and after WebBrain is updated or reloaded, actions in tabs that were already open no longer fail because WebBrain could not load its page scripts into them again. Firefox looks up a script the background page injects from the background page's own folder, not from the extension's root, so every such injection named a file that does not exist. They now name the file from the root.
+
## [42.4.0] - 2026-10-24
### Added
diff --git a/src/chrome/src/agent/page-feedback.js b/src/chrome/src/agent/page-feedback.js
index c492890d6..0d4d5e1a2 100644
--- a/src/chrome/src/agent/page-feedback.js
+++ b/src/chrome/src/agent/page-feedback.js
@@ -137,11 +137,11 @@ async function captureMediaState(api, tabId) {
try {
let result;
if (typeof api.tabs.executeScript === 'function') {
- await api.tabs.executeScript(tabId, { file: 'src/agent/social-media-downloader.js' });
+ await api.tabs.executeScript(tabId, { file: '/src/agent/social-media-downloader.js' });
stage = 'capture_failed';
result = (await api.tabs.executeScript(tabId, { code: `(${captureMediaDownloadStateInPage.toString()})()` }))?.[0];
} else {
- await api.scripting.executeScript({ target: { tabId }, world: 'MAIN', files: ['src/agent/social-media-downloader.js'] });
+ await api.scripting.executeScript({ target: { tabId }, world: 'MAIN', files: ['/src/agent/social-media-downloader.js'] });
stage = 'capture_failed';
result = (await api.scripting.executeScript({ target: { tabId }, world: 'MAIN', func: captureMediaDownloadStateInPage }))?.[0]?.result;
}
diff --git a/src/chrome/src/feedback-handoff.js b/src/chrome/src/feedback-handoff.js
index ad6b55bf5..ff1d2dfe9 100644
--- a/src/chrome/src/feedback-handoff.js
+++ b/src/chrome/src/feedback-handoff.js
@@ -44,9 +44,9 @@ export function createFeedbackHandoff(api) {
inflight.add(tabId);
try {
if (api.scripting?.executeScript) await api.scripting.executeScript({
- target: { tabId, frameIds: [0] }, files: ['src/content/feedback-attachment.js'],
+ target: { tabId, frameIds: [0] }, files: ['/src/content/feedback-attachment.js'],
});
- else await api.tabs.executeScript(tabId, { file: 'src/content/feedback-attachment.js', frameId: 0 });
+ else await api.tabs.executeScript(tabId, { file: '/src/content/feedback-attachment.js', frameId: 0 });
} finally { inflight.delete(tabId); }
}
diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.js
index dae08c8c9..69a497ecb 100644
--- a/src/firefox/src/agent/agent.js
+++ b/src/firefox/src/agent/agent.js
@@ -12300,7 +12300,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
`;
let elements = (await browser.tabs.executeScript(tabId, { code }))?.[0];
if (elements === null) {
- await browser.tabs.executeScript(tabId, { file: 'src/content/accessibility-tree.js' });
+ await browser.tabs.executeScript(tabId, { file: '/src/content/accessibility-tree.js' });
elements = (await browser.tabs.executeScript(tabId, { code }))?.[0];
}
return Array.isArray(elements) ? elements : [];
@@ -45136,7 +45136,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
// which subsequent executeScript calls in the same tab can reach.
// Idempotent across reinjection.
await browser.tabs.executeScript(tabId, {
- file: 'src/agent/social-media-downloader.js',
+ file: '/src/agent/social-media-downloader.js',
});
const bulkSocialDownload = !!toolArgs.scroll || toolArgs.mode === 'all';
const opts = {
@@ -46629,10 +46629,10 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
}
async _injectCoreContentScripts(tabId) {
- await browser.tabs.executeScript(tabId, { file: 'src/content/page-monitor-shadow-loader.js' });
- await browser.tabs.executeScript(tabId, { file: 'src/content/page-monitor.js' });
+ await browser.tabs.executeScript(tabId, { file: '/src/content/page-monitor-shadow-loader.js' });
+ await browser.tabs.executeScript(tabId, { file: '/src/content/page-monitor.js' });
await browser.tabs.executeScript(tabId, {
- file: 'src/content/file-picker-guard-loader.js',
+ file: '/src/content/file-picker-guard-loader.js',
});
// The loader fetches a web-accessible extension script into the page's
// main world. Give that local load a brief head start before content.js
@@ -46640,21 +46640,21 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
// shared DOM for the bridge to pick up if the load completes slightly
// later.
await new Promise(resolve => setTimeout(resolve, 50));
- await browser.tabs.executeScript(tabId, { file: 'src/content/file-upload-policy.js' });
+ await browser.tabs.executeScript(tabId, { file: '/src/content/file-upload-policy.js' });
await browser.tabs.executeScript(tabId, {
- file: 'src/content/rich-text-toolbar-heuristic.js',
+ file: '/src/content/rich-text-toolbar-heuristic.js',
});
await browser.tabs.executeScript(tabId, {
- file: 'src/content/accessibility-tree.js',
+ file: '/src/content/accessibility-tree.js',
});
await browser.tabs.executeScript(tabId, {
- file: 'src/content/chat-observation.js',
+ file: '/src/content/chat-observation.js',
});
await browser.tabs.executeScript(tabId, {
- file: 'src/content/content.js',
+ file: '/src/content/content.js',
});
await browser.tabs.executeScript(tabId, {
- file: 'src/content/agent-visual-indicator.js',
+ file: '/src/content/agent-visual-indicator.js',
});
}
diff --git a/src/firefox/src/agent/page-feedback.js b/src/firefox/src/agent/page-feedback.js
index c492890d6..0d4d5e1a2 100644
--- a/src/firefox/src/agent/page-feedback.js
+++ b/src/firefox/src/agent/page-feedback.js
@@ -137,11 +137,11 @@ async function captureMediaState(api, tabId) {
try {
let result;
if (typeof api.tabs.executeScript === 'function') {
- await api.tabs.executeScript(tabId, { file: 'src/agent/social-media-downloader.js' });
+ await api.tabs.executeScript(tabId, { file: '/src/agent/social-media-downloader.js' });
stage = 'capture_failed';
result = (await api.tabs.executeScript(tabId, { code: `(${captureMediaDownloadStateInPage.toString()})()` }))?.[0];
} else {
- await api.scripting.executeScript({ target: { tabId }, world: 'MAIN', files: ['src/agent/social-media-downloader.js'] });
+ await api.scripting.executeScript({ target: { tabId }, world: 'MAIN', files: ['/src/agent/social-media-downloader.js'] });
stage = 'capture_failed';
result = (await api.scripting.executeScript({ target: { tabId }, world: 'MAIN', func: captureMediaDownloadStateInPage }))?.[0]?.result;
}
diff --git a/src/firefox/src/agent/rich-text-toolbar-probe.js b/src/firefox/src/agent/rich-text-toolbar-probe.js
index 3e78b9302..073e033c0 100644
--- a/src/firefox/src/agent/rich-text-toolbar-probe.js
+++ b/src/firefox/src/agent/rich-text-toolbar-probe.js
@@ -51,7 +51,7 @@ export class RichTextToolbarProbe {
try {
await browser.tabs.executeScript(tabId, {
frameId: frame.frameId,
- file: 'src/content/redaction-regions.js',
+ file: '/src/content/redaction-regions.js',
});
payload = await collect();
} catch {
@@ -438,15 +438,15 @@ export class RichTextToolbarProbe {
try {
await browser.tabs.executeScript(tabId, {
frameId: frame.frameId,
- file: 'src/content/rich-text-toolbar-heuristic.js',
+ file: '/src/content/rich-text-toolbar-heuristic.js',
});
await browser.tabs.executeScript(tabId, {
frameId: frame.frameId,
- file: 'src/content/accessibility-tree.js',
+ file: '/src/content/accessibility-tree.js',
});
await browser.tabs.executeScript(tabId, {
frameId: frame.frameId,
- file: 'src/content/content.js',
+ file: '/src/content/content.js',
});
probe = await request();
} catch {
@@ -537,15 +537,15 @@ export class RichTextToolbarProbe {
try {
await browser.tabs.executeScript(tabId, {
frameId: child.frameId,
- file: 'src/content/rich-text-toolbar-heuristic.js',
+ file: '/src/content/rich-text-toolbar-heuristic.js',
});
await browser.tabs.executeScript(tabId, {
frameId: child.frameId,
- file: 'src/content/accessibility-tree.js',
+ file: '/src/content/accessibility-tree.js',
});
await browser.tabs.executeScript(tabId, {
frameId: child.frameId,
- file: 'src/content/content.js',
+ file: '/src/content/content.js',
});
await announce();
} catch {}
diff --git a/src/firefox/src/feedback-handoff.js b/src/firefox/src/feedback-handoff.js
index ad6b55bf5..ff1d2dfe9 100644
--- a/src/firefox/src/feedback-handoff.js
+++ b/src/firefox/src/feedback-handoff.js
@@ -44,9 +44,9 @@ export function createFeedbackHandoff(api) {
inflight.add(tabId);
try {
if (api.scripting?.executeScript) await api.scripting.executeScript({
- target: { tabId, frameIds: [0] }, files: ['src/content/feedback-attachment.js'],
+ target: { tabId, frameIds: [0] }, files: ['/src/content/feedback-attachment.js'],
});
- else await api.tabs.executeScript(tabId, { file: 'src/content/feedback-attachment.js', frameId: 0 });
+ else await api.tabs.executeScript(tabId, { file: '/src/content/feedback-attachment.js', frameId: 0 });
} finally { inflight.delete(tabId); }
}
diff --git a/test/run.js b/test/run.js
index 9ee94dc5a..bcf6dc51f 100644
--- a/test/run.js
+++ b/test/run.js
@@ -62212,13 +62212,13 @@ test('the rich-text toolbar heuristic has exactly one implementation', () => {
);
}
- const firefoxContentInjections = [...firefoxAgent.matchAll(/file: 'src\/content\/content\.js'/g)];
+ const firefoxContentInjections = [...firefoxAgent.matchAll(/file: '\/src\/content\/content\.js'/g)];
assert.ok(firefoxContentInjections.length > 0, 'Firefox must have content-script recovery paths');
let previousContentIndex = 0;
for (const match of firefoxContentInjections) {
const injectionSequence = firefoxAgent.slice(previousContentIndex, match.index);
- const heuristicIndex = injectionSequence.lastIndexOf("file: 'src/content/rich-text-toolbar-heuristic.js'");
- const accessibilityIndex = injectionSequence.lastIndexOf("file: 'src/content/accessibility-tree.js'");
+ const heuristicIndex = injectionSequence.lastIndexOf("file: '/src/content/rich-text-toolbar-heuristic.js'");
+ const accessibilityIndex = injectionSequence.lastIndexOf("file: '/src/content/accessibility-tree.js'");
assert.ok(
heuristicIndex >= 0 && accessibilityIndex > heuristicIndex,
'every Firefox content-script recovery path must inject the heuristic before accessibility-tree.js and content.js',
@@ -111583,15 +111583,15 @@ test('Chrome click paths suppress native file choosers and redirect to upload_fi
const agent = Object.create(AgentFx.prototype);
await agent._injectCoreContentScripts(43);
assert.deepEqual(firefoxInjections.map(injection => injection.file), [
- 'src/content/page-monitor-shadow-loader.js',
- 'src/content/page-monitor.js',
- 'src/content/file-picker-guard-loader.js',
- 'src/content/file-upload-policy.js',
- 'src/content/rich-text-toolbar-heuristic.js',
- 'src/content/accessibility-tree.js',
- 'src/content/chat-observation.js',
- 'src/content/content.js',
- 'src/content/agent-visual-indicator.js',
+ '/src/content/page-monitor-shadow-loader.js',
+ '/src/content/page-monitor.js',
+ '/src/content/file-picker-guard-loader.js',
+ '/src/content/file-upload-policy.js',
+ '/src/content/rich-text-toolbar-heuristic.js',
+ '/src/content/accessibility-tree.js',
+ '/src/content/chat-observation.js',
+ '/src/content/content.js',
+ '/src/content/agent-visual-indicator.js',
]);
} finally {
if (previousBrowser === undefined) delete globalThis.browser;
@@ -111658,6 +111658,35 @@ test('Chrome click paths suppress native file choosers and redirect to upload_fi
}
});
+test('Firefox injects extension scripts by root-absolute paths', () => {
+ // Firefox resolves a relative tabs.executeScript or scripting.executeScript
+ // file against the calling page (src/background.html), not the extension
+ // root, so 'src/content/content.js' would load src/src/content/content.js.
+ const relative = [];
+ const missing = [];
+ const walk = (dir) => {
+ for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
+ const full = path.join(dir, entry.name);
+ if (entry.isDirectory()) {
+ walk(full);
+ continue;
+ }
+ if (!/\.m?js$/.test(entry.name)) continue;
+ const source = fs.readFileSync(full, 'utf8');
+ for (const property of source.matchAll(/\bfiles?:\s*(\[[^\]]*\]|'[^']*'|"[^"]*")/g)) {
+ for (const literal of property[1].matchAll(/['"]([^'"]+\.(?:js|css))['"]/g)) {
+ const where = `${path.relative(ROOT, full)}: ${literal[1]}`;
+ if (!literal[1].startsWith('/')) relative.push(where);
+ else if (!fs.existsSync(path.join(ROOT, 'src/firefox', literal[1]))) missing.push(where);
+ }
+ }
+ }
+ };
+ walk(path.join(ROOT, 'src/firefox/src'));
+ assert.deepEqual(relative, [], 'Firefox script injections must start at the extension root');
+ assert.deepEqual(missing, [], 'Firefox script injections must name files the extension ships');
+});
+
test('upload_file prefers a valid downloadId and falls back to filePath for an invalid id (chrome)', async () => {
const originalChrome = globalThis.chrome;
const originalCdp = {
diff --git a/test/social-media-downloader-reinjection.mjs b/test/social-media-downloader-reinjection.mjs
index 15631a1bf..0915f6ae4 100644
--- a/test/social-media-downloader-reinjection.mjs
+++ b/test/social-media-downloader-reinjection.mjs
@@ -111,12 +111,12 @@ for (const browser of ['chrome', 'firefox']) {
const api = browser === 'chrome'
? { tabs: {}, scripting: { async executeScript(options) {
assert.equal(options.world, 'MAIN');
- if (options.files) { assert.deepEqual(Array.from(options.files), ['src/agent/social-media-downloader.js']); load(source, realm); return []; }
+ if (options.files) { assert.deepEqual(Array.from(options.files), ['/src/agent/social-media-downloader.js']); load(source, realm); return []; }
captures++; return [{ result: options.func() }];
} } }
: { tabs: { async executeScript(tabId, options) {
assert.equal(tabId, 7);
- if (options.file) { assert.equal(options.file, 'src/agent/social-media-downloader.js'); load(source, realm); return []; }
+ if (options.file) { assert.equal(options.file, '/src/agent/social-media-downloader.js'); load(source, realm); return []; }
captures++; return [vm.runInContext(options.code, realm.context)];
} } };
const observation = await realm.context.captureMediaState(api, 7);