From 1208ad54a9afc277130e0f7fc397506d6359a3ca Mon Sep 17 00:00:00 2001 From: windingwind <33902321+windingwind@users.noreply.github.com> Date: Thu, 7 Dec 2023 21:48:02 +0800 Subject: [PATCH] add: devtool support --- package.json | 3 ++- scripts/devtool.mjs | 61 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 scripts/devtool.mjs diff --git a/package.json b/package.json index 6dd403e..93bcf23 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "build-prod": "cross-env NODE_ENV=production node scripts/build.mjs", "build": "concurrently -c auto npm:build-prod npm:tsc", "tsc": "tsc --noEmit", - "start": "node scripts/start.mjs", + "start": "concurrently 'node scripts/start.mjs' 'node -e \"setTimeout(()=>{},2000)\" && npm run devtool'", "start-watch": "npm run build-dev && concurrently -c auto npm:start npm:watch", "stop": "node scripts/stop.mjs", "restart-dev": "npm run build-dev && npm run stop && npm run start", @@ -26,6 +26,7 @@ "restart": "npm run restart-dev", "reload": "npm run build-dev && node scripts/reload.mjs", "watch": "chokidar \"src/**\" \"addon/**\" -c \"npm run reload\"", + "devtool": "node scripts/devtool.mjs", "release": "release-it", "lint": "prettier --write . && eslint . --ext .ts --fix", "test": "echo \"Error: no test specified\" && exit 1", diff --git a/scripts/devtool.mjs b/scripts/devtool.mjs new file mode 100644 index 0000000..ad36818 --- /dev/null +++ b/scripts/devtool.mjs @@ -0,0 +1,61 @@ +import { exit } from "process"; +import { execSync } from "child_process"; +import cmd from "./zotero-cmd.json" assert { type: "json" }; + +const { zoteroBinPath, profilePath } = cmd.exec; + +const startZotero = `"${zoteroBinPath}" --debugger --purgecaches -profile "${profilePath}"`; + +const script = ` +(async () => { + Zotero.Prefs.set("devtools.debugger.remote-enabled", true, true); + Zotero.Prefs.set("devtools.debugger.remote-port", 6100, true); + Zotero.Prefs.set("devtools.debugger.prompt-connection", false, true); + Zotero.Prefs.set("devtools.debugger.chrome-debugging-websocket", false, true); + + env = + Services.env || + Cc["@mozilla.org/process/environment;1"].getService(Ci.nsIEnvironment); + + env.set("MOZ_BROWSER_TOOLBOX_PORT", 6100); + Zotero.openInViewer( + "chrome://devtools/content/framework/browser-toolbox/window.html", + { + onLoad: (doc) => { + doc.querySelector("#status-message-container").style.visibility = + "collapse"; + let toolboxBody; + waitUntil( + () => { + toolboxBody = doc + .querySelector(".devtools-toolbox-browsertoolbox-iframe") + ?.contentDocument?.querySelector(".theme-body"); + return toolboxBody; + }, + () => { + toolboxBody.style = "pointer-events: all !important"; + } + ); + }, + } + ); + + function waitUntil(condition, callback, interval = 100, timeout = 10000) { + const start = Date.now(); + const intervalId = setInterval(() => { + if (condition()) { + clearInterval(intervalId); + callback(); + } else if (Date.now() - start > timeout) { + clearInterval(intervalId); + } + }, interval); + } +})()`; + +const url = `zotero://ztoolkit-debug/?run=${encodeURIComponent(script)}`; + +const command = `${startZotero} -url "${url}"`; + +execSync(command); +exit(0);