I'm new to tamper monkey and trying to write a script that allows me to press a hotkey and have it search through the page and open a URL based on the results of that search. Here's the code I have so far:
// ==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);
In reading the Stack Overflow question adding-a-custom-keyboard-shortcut-using-userscript-to-chrome-with-tampermonkey and the unsafeWindow documentation, it seemed like it would be as simple as adding unsafeWindow before my function calls, but that isn't working. I've tried just inserting the code into the console and it works when I do that. It seems like there is some sort of setup on Tamper Monkey that I don't understand. Any input would be greatly appreciated. Thank you
It looks like the unsafeWindow was actually the problem to running the page. When I changed
function doc_keyUp(e) {
if (e.altKey && e.key === "w") {
unsafeWindow.iterateThroughWorkflowTabs();
}
}
to
function doc_keyUp(e) {
if (e.altKey && e.key === "w") {
iterateThroughWorkflowTabs();
}
}
The script ran without a problem