diff --git a/manifest.json b/manifest.json index 64d4262..48a5f05 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 3, "name": "__MSG_appName__", - "version": "0.12.0", + "version": "0.13.0", "description": "__MSG_appDescription__", "minimum_chrome_version": "109", "permissions": [ @@ -17,7 +17,9 @@ "https://twitter.com/*", "https://www.twitter.com/*", "https://pbs.twimg.com/*", - "https://abs.twimg.com/*" + "https://abs.twimg.com/*", + "http://127.0.0.1:8787/*", + "http://localhost:8787/*" ], "background": { "service_worker": "background.js" diff --git a/package.json b/package.json index 4458212..a62f70b 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,8 @@ { "name": "x2pdf", - "version": "0.12.0", + "version": "0.13.0", "private": true, - "description": "A local-first Chromium extension that exports X Articles and posts to structured, searchable PDFs.", + "description": "A local-first Chromium extension that exports X Articles and posts to structured, searchable PDFs and X2RED.", "license": "MIT", "repository": { "type": "git", diff --git a/preview.html b/preview.html index dc5f544..a0390b2 100644 --- a/preview.html +++ b/preview.html @@ -8,130 +8,29 @@
-
-
X
-
- X2PDF - Structured Article preview v0.12 -
-
- +
X
X2PDFStructured Article preview v0.13
- - - - - - - - + + + + - +
- - - + -
- - - + +
- - - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + diff --git a/scripts/check-repository.js b/scripts/check-repository.js index f5e2e85..7d69ba3 100644 --- a/scripts/check-repository.js +++ b/scripts/check-repository.js @@ -22,6 +22,7 @@ const referenced = [ "preview.html", "preview.css", "preview.js", + "x2red-bridge.js", "background.js", "extractor.js", "structured-parser.js", diff --git a/scripts/package_extension.py b/scripts/package_extension.py index 2014689..cb318ed 100755 --- a/scripts/package_extension.py +++ b/scripts/package_extension.py @@ -11,7 +11,7 @@ runtime_files = [ "manifest.json", "background.js", "extractor.js", "structured-parser.js", - "preview.html", "preview.css", "preview.js", "i18n.js", + "preview.html", "preview.css", "preview.js", "x2red-bridge.js", "i18n.js", "code-highlighter.js", "formula-renderer.js", "mathjax-config.js", "clipboard-utils.js", "LICENSE", "THIRD_PARTY_NOTICES.md" ] diff --git a/x2red-bridge.js b/x2red-bridge.js new file mode 100644 index 0000000..c82c0a4 --- /dev/null +++ b/x2red-bridge.js @@ -0,0 +1,62 @@ +(() => { + "use strict"; + + const ENDPOINT = "http://127.0.0.1:8787/api/integrations/x2pdf/documents"; + const button = document.getElementById("sendX2REDButton"); + const status = document.getElementById("x2redStatus"); + const title = document.getElementById("documentTitle"); + if (!button) return; + + const isZh = (globalThis.XPDFI18n?.locale || navigator.language || "en").startsWith("zh"); + const copy = { + send: isZh ? "发送到 X2RED" : "Send to X2RED", + sending: isZh ? "发送中…" : "Sending…", + connecting: isZh ? "正在连接本机 X2RED…" : "Connecting to local X2RED…", + noDocument: isZh ? "没有可发送的标准化长文。" : "No normalized document is available.", + sent: isZh ? "已发送到 X2RED" : "Sent to X2RED", + sentButton: isZh ? "已发送 ✓" : "Sent ✓", + unavailable: isZh ? "X2RED 不可用" : "X2RED unavailable", + blocks: isZh ? "个内容块" : "blocks", + }; + button.textContent = copy.send; + + button.addEventListener("click", async () => { + button.disabled = true; + button.textContent = copy.sending; + if (status) status.textContent = copy.connecting; + try { + const stored = await chrome.storage.session.get("pendingXDocument"); + const documentValue = structuredClone(stored.pendingXDocument || null); + if (!documentValue || !Array.isArray(documentValue.blocks)) { + throw new Error(copy.noDocument); + } + if (title?.textContent?.trim()) { + documentValue.metadata = { ...(documentValue.metadata || {}), title: title.textContent.trim() }; + } + const response = await fetch(ENDPOINT, { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ document: documentValue }), + }); + const payload = await response.json().catch(() => ({})); + if (!response.ok) throw new Error(payload.detail || `X2RED HTTP ${response.status}`); + if (status) status.textContent = `${copy.sent} · ${payload.block_count || 0} ${copy.blocks}`; + button.textContent = copy.sentButton; + setTimeout(() => { + button.textContent = copy.send; + button.disabled = false; + }, 1800); + window.open( + `http://127.0.0.1:8787/?source=${encodeURIComponent(payload.source_id)}`, + "_blank", + "noopener", + ); + return; + } catch (error) { + const message = error instanceof Error ? error.message : String(error); + if (status) status.textContent = `${copy.unavailable}: ${message}`; + button.textContent = copy.send; + } + button.disabled = false; + }); +})();