使用 Tamper Monkey 脚本添加热键
Adding a hotkey with a Tamper Monkey script
我是 tamper monkey 的新手,我正在尝试编写一个脚本,允许我按下热键并让它搜索页面并根据搜索结果打开 URL。这是我到目前为止的代码:
// ==UserScript==
// @name Paragon to Paramount Debug Forwarding
// @namespace http://tampermonkey.net/
// @version 1
// @description cntrl-click forwarding from a paragon run diag to paramount debugger
// @author taurone
// @run-at document-start
// ==/UserScript==
function getElementByXPath(path) {
return document.evaluate(
path,
document,
null,
XPathResult.FIRST_ORDERED_NODE_TYPE,
null
).singleNodeValue.nodeValue;
}
function openDebugURL(runDiag) {
let runDiagURL = `https://my_website.com/#/debugger/${runDiag}`;
window.open(runDiagURL);
}
function iterateThroughWorkflowTabs() {
let workflowTabs = document.getElementsByClassName("a-tabs")[0].children;
let tabCounter = 1;
let runDiag;
for (tab of workflowTabs) {
if (tab.className.includes("active")) {
try {
runDiag = getElementByXPath(
"/html/body/div[9]/div/div[2]/div/div[2]/div[2]/div/div[3]/div/div/div/pm-tab-set/div[" +
String(tabCounter) +
"]/pm-tab/ng-transclude/ng-transclude/pm-card/div/pm-workflow-card/div/div[2]/workflow-step-templates/div/workflow-default-step/div/div[3]/div/div[1]/div/pm-continue-button/div/aui-button/span/@data-csm-meta-pm-diagrunid"
);
} catch {
runDiag = getElementByXPath(
"/html/body/div[9]/div/div[2]/div/div[2]/div[2]/div/div[3]/div/div/div/pm-tab-set/div[" +
String(tabCounter) +
"]/pm-tab/ng-transclude/ng-transclude/pm-card/div/pm-workflow-card/div/div[2]/workflow-step-templates/div/workflow-default-step/div/div[3]/div/div[2]/label/@data-csm-meta-pm-diagrunid"
);
}
openDebugURL(runDiag);
return;
}
tabCounter++;
}
}
function doc_keyUp(e) {
if (e.altKey && e.key === "w") {
unsafeWindow.iterateThroughWorkflowTabs();
}
}
document.addEventListener("keyup", doc_keyUp, false);
在阅读 Stack Overflow 问题 adding-a-custom-keyboard-shortcut-using-userscript-to-chrome-with-tampermonkey and the unsafeWindow 文档时,它似乎就像在我的函数调用之前添加 unsafeWindow 一样简单,但这是行不通的。我试过将代码插入控制台,当我这样做时它会起作用。似乎 Tamper Monkey 上有某种我不明白的设置。任何投入将不胜感激。谢谢
看起来 unsafeWindow 实际上是 运行 页面的问题。当我改变
function doc_keyUp(e) {
if (e.altKey && e.key === "w") {
unsafeWindow.iterateThroughWorkflowTabs();
}
}
至
function doc_keyUp(e) {
if (e.altKey && e.key === "w") {
iterateThroughWorkflowTabs();
}
}
脚本运行没问题
我是 tamper monkey 的新手,我正在尝试编写一个脚本,允许我按下热键并让它搜索页面并根据搜索结果打开 URL。这是我到目前为止的代码:
// ==UserScript==
// @name Paragon to Paramount Debug Forwarding
// @namespace http://tampermonkey.net/
// @version 1
// @description cntrl-click forwarding from a paragon run diag to paramount debugger
// @author taurone
// @run-at document-start
// ==/UserScript==
function getElementByXPath(path) {
return document.evaluate(
path,
document,
null,
XPathResult.FIRST_ORDERED_NODE_TYPE,
null
).singleNodeValue.nodeValue;
}
function openDebugURL(runDiag) {
let runDiagURL = `https://my_website.com/#/debugger/${runDiag}`;
window.open(runDiagURL);
}
function iterateThroughWorkflowTabs() {
let workflowTabs = document.getElementsByClassName("a-tabs")[0].children;
let tabCounter = 1;
let runDiag;
for (tab of workflowTabs) {
if (tab.className.includes("active")) {
try {
runDiag = getElementByXPath(
"/html/body/div[9]/div/div[2]/div/div[2]/div[2]/div/div[3]/div/div/div/pm-tab-set/div[" +
String(tabCounter) +
"]/pm-tab/ng-transclude/ng-transclude/pm-card/div/pm-workflow-card/div/div[2]/workflow-step-templates/div/workflow-default-step/div/div[3]/div/div[1]/div/pm-continue-button/div/aui-button/span/@data-csm-meta-pm-diagrunid"
);
} catch {
runDiag = getElementByXPath(
"/html/body/div[9]/div/div[2]/div/div[2]/div[2]/div/div[3]/div/div/div/pm-tab-set/div[" +
String(tabCounter) +
"]/pm-tab/ng-transclude/ng-transclude/pm-card/div/pm-workflow-card/div/div[2]/workflow-step-templates/div/workflow-default-step/div/div[3]/div/div[2]/label/@data-csm-meta-pm-diagrunid"
);
}
openDebugURL(runDiag);
return;
}
tabCounter++;
}
}
function doc_keyUp(e) {
if (e.altKey && e.key === "w") {
unsafeWindow.iterateThroughWorkflowTabs();
}
}
document.addEventListener("keyup", doc_keyUp, false);
在阅读 Stack Overflow 问题 adding-a-custom-keyboard-shortcut-using-userscript-to-chrome-with-tampermonkey and the unsafeWindow 文档时,它似乎就像在我的函数调用之前添加 unsafeWindow 一样简单,但这是行不通的。我试过将代码插入控制台,当我这样做时它会起作用。似乎 Tamper Monkey 上有某种我不明白的设置。任何投入将不胜感激。谢谢
看起来 unsafeWindow 实际上是 运行 页面的问题。当我改变
function doc_keyUp(e) {
if (e.altKey && e.key === "w") {
unsafeWindow.iterateThroughWorkflowTabs();
}
}
至
function doc_keyUp(e) {
if (e.altKey && e.key === "w") {
iterateThroughWorkflowTabs();
}
}
脚本运行没问题