How do I make functions added by Tampermonkey be available in console after the script has been ran?

Euphe picture Euphe · Dec 28, 2014 · Viewed 7.2k times · Source

I made a script that among other things has a function in it:

function updateGUI(){
    document.getElementById("cursoft").value = getSoftware();
    document.getElementById("curver").value = getCurrentVersion();
    document.getElementById("rcycles").value = getResearchCycles();
    document.getElementById("rcycle").value = getCurrentCycle();
    document.getElementById("curproc").value = getCurrentProcess();
    document.getElementById("curact").value = getCurrentAction();
}

The script runs on page load just fine, but when I try to run this function after the script finishes execution it's "undefined".
How can I make it "stay" in the current scope?

Answer

Etheryte picture Etheryte · Dec 28, 2014

Tampermonkey scripts are run in a separate scope. This means that to make a function available globally you'll want to do something along the following:

window.updateGUI = function () {...}

If you want to add a number of functions to the global scope, it's better to store them under a single object and address your functions through there. That helps avoid any possible collisions you might otherwise have.

var myFunctions = window.myFunctions = {};
myFunctions.updateGUI = function () {...};

After that you can simply call myFunctions.updateGUI();.