add: devtool support

This commit is contained in:
windingwind 2023-12-07 21:48:02 +08:00
parent a56e31fa0d
commit 1208ad54a9
2 changed files with 63 additions and 1 deletions

View File

@ -18,7 +18,7 @@
"build-prod": "cross-env NODE_ENV=production node scripts/build.mjs", "build-prod": "cross-env NODE_ENV=production node scripts/build.mjs",
"build": "concurrently -c auto npm:build-prod npm:tsc", "build": "concurrently -c auto npm:build-prod npm:tsc",
"tsc": "tsc --noEmit", "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", "start-watch": "npm run build-dev && concurrently -c auto npm:start npm:watch",
"stop": "node scripts/stop.mjs", "stop": "node scripts/stop.mjs",
"restart-dev": "npm run build-dev && npm run stop && npm run start", "restart-dev": "npm run build-dev && npm run stop && npm run start",
@ -26,6 +26,7 @@
"restart": "npm run restart-dev", "restart": "npm run restart-dev",
"reload": "npm run build-dev && node scripts/reload.mjs", "reload": "npm run build-dev && node scripts/reload.mjs",
"watch": "chokidar \"src/**\" \"addon/**\" -c \"npm run reload\"", "watch": "chokidar \"src/**\" \"addon/**\" -c \"npm run reload\"",
"devtool": "node scripts/devtool.mjs",
"release": "release-it", "release": "release-it",
"lint": "prettier --write . && eslint . --ext .ts --fix", "lint": "prettier --write . && eslint . --ext .ts --fix",
"test": "echo \"Error: no test specified\" && exit 1", "test": "echo \"Error: no test specified\" && exit 1",

61
scripts/devtool.mjs Normal file
View File

@ -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);