For debugging purposes, is it possible to access userscript variables from the console in Google Chrome?
Suppose you had a Chrome userscript with this code:
var userscriptVar = "I'm a global variable, userscript context.";
window.var2 = "I'm a window.scope variable, userscript context.";
console.log ("Hello from the userscript.");
To access these userscript variables:
Determine the userscript's ID. You can see it on the extensions page (chrome://extensions/
):
Switch to the script's context by clicking on the context menu at the bottom of the console:
Note that the id (pfnbaeafniclcjhfkndoploalomdmgkc
) is the same as that listed on the extensions page.
Now you will immediately be able to see and change the window-scoped variable (var2
), but you can't see the userscript's global (userscriptVar
) because the script-instance has long since finished and disappeared. :
To access and change userscript variables, while the script is still active, set a breakpoint in the script and use the debugger. See this answer for how to do that.
With the userscript paused at a suitable breakpoint, you can see, but not change the global values, from the console.
But you can change the value from the debugger: