Skip to content

A self-hostable bookmark-everything app (links, notes and images) with AI-based automatic tagging and full text search

fix(extension): restore client-side SingleFile capture (#3241)

Mohamed Bassem2c33f771a624
diff --git a/apps/browser-extension/src/content-scripts/singlefile-content-script.ts b/apps/browser-extension/src/content-scripts/singlefile-content-script.ts
index 987791eb..d2f442ef 100644
--- a/apps/browser-extension/src/content-scripts/singlefile-content-script.ts
+++ b/apps/browser-extension/src/content-scripts/singlefile-content-script.ts
@@ -10,29 +10,28 @@ declare global {
}
}
-if (window.__karakeepSingleFileLoaded__) {
- // Already registered in this page context — don't re-register listeners.
- // Using `throw` short-circuits re-injection cleanly.
- throw new Error("karakeep singlefile content script already loaded");
-}
-window.__karakeepSingleFileLoaded__ = true;
+// Re-injecting into a page that already has the script must be a no-op, so
+// listeners aren't registered twice.
+if (!window.__karakeepSingleFileLoaded__) {
+ window.__karakeepSingleFileLoaded__ = true;
-init({});
+ init({});
-chrome.runtime.onMessage.addListener((message, _sender, sendResponse) => {
- if (message.type === "CAPTURE_PAGE") {
- captureCurrentPage({ blockImages: message.blockImages === true })
- .then((html) => sendResponse({ success: true, html }))
- .catch((error) =>
- sendResponse({
- success: false,
- error: error instanceof Error ? error.message : String(error),
- }),
- );
- // Return true to indicate we'll send a response asynchronously
- return true;
- }
-});
+ chrome.runtime.onMessage.addListener((message, _sender, sendResponse) => {
+ if (message.type === "CAPTURE_PAGE") {
+ captureCurrentPage({ blockImages: message.blockImages === true })
+ .then((html) => sendResponse({ success: true, html }))
+ .catch((error) =>
+ sendResponse({
+ success: false,
+ error: error instanceof Error ? error.message : String(error),
+ }),
+ );
+ // Return true to indicate we'll send a response asynchronously
+ return true;
+ }
+ });
+}
async function captureCurrentPage(opts: {
blockImages: boolean;
diff --git a/apps/browser-extension/src/utils/singlefile.ts b/apps/browser-extension/src/utils/singlefile.ts
index f6530739..3a4d1254 100644
--- a/apps/browser-extension/src/utils/singlefile.ts
+++ b/apps/browser-extension/src/utils/singlefile.ts
@@ -64,35 +64,10 @@ async function injectSingleFileContentScript(tabId: number): Promise<void> {
if (!files || files.length === 0) {
throw new Error("SingleFile content script not declared in manifest");
}
- // The bundle is an ES module (crxjs emits chunks with `import.meta`), so
- // `executeScript({ files })` — which loads as a classic script — fails.
- // Use a dynamic import in the isolated world instead.
- const urls = files.map((f) => chrome.runtime.getURL(f));
- const [result] = await chrome.scripting.executeScript({
- target: { tabId },
- func: async (moduleUrls: string[]) => {
- try {
- for (const url of moduleUrls) {
- await import(/* @vite-ignore */ url);
- }
- return { ok: true as const };
- } catch (e) {
- return {
- ok: false as const,
- error: e instanceof Error ? e.message : String(e),
- };
- }
- },
- args: [urls],
- });
- const res = result?.result;
- // Treat the re-entry guard as success — the listener is already registered
- // from a concurrent injection, so the retried sendMessage will succeed.
- if (!res || (!res.ok && !res.error?.includes("already loaded"))) {
- throw new Error(
- `Failed to inject SingleFile content script: ${res?.error ?? "unknown error"}`,
- );
- }
+ // The content script is built as a standalone IIFE (see `standaloneFiles` in
+ // vite.config.ts), so it registers its CAPTURE_PAGE listener synchronously
+ // and the listener exists once `executeScript` resolves.
+ await chrome.scripting.executeScript({ target: { tabId }, files });
}
/**
diff --git a/apps/browser-extension/vite.config.ts b/apps/browser-extension/vite.config.ts
index dd856ce4..11222622 100644
--- a/apps/browser-extension/vite.config.ts
+++ b/apps/browser-extension/vite.config.ts
@@ -11,6 +11,12 @@ export default defineConfig({
crx({
manifest,
browser: process.env.VITE_BUILD_FIREFOX ? "firefox" : "chrome",
+ contentScripts: {
+ // Injected on demand with `chrome.scripting.executeScript({ files })`,
+ // which needs a self-contained classic script rather than crxjs's
+ // default loader (it imports the real module asynchronously).
+ standaloneFiles: ["src/content-scripts/singlefile-content-script.ts"],
+ },
}),
],
server: {