Debug tampermonkey script

Google ChromeGoogle Chrome-ExtensionTampermonkey

Google Chrome Problem Overview


I would like to debug a Tampermonkey script with Chrome's console, but I can't find my script in the list..

enter image description here

Am I doing something wrong, or is it just that the Tampermonkey scripts don't appear there? And in that case, how can I debug it?

Google Chrome Solutions


Solution 1 - Google Chrome

Tampermonkey is simply an extension that injects boilerplate scripts to evaluate your custom scripts, so you can debug any of these scripts if you can find them..

The trouble is that it is evaluating userscripts as if someone called eval() on them, so you will see VM### instead of something nice like myscript.js and you can't normally navigate to them like permanent scripts.

Instead, add debugger lines:

  • Settings Checkmark:

TamperMonkey Dashboard -> Settings -> General (Config mode: Advanced) -> Debug scripts Tampermonkey general settings w/debug checked

  • Or, in your userscript add the line:

    debugger;

like so: debugger line in userscript

(Doing this at the top of a userscript is equivalent to the Tampermonkey setting)

When you have a console open on a page using the script it will pause when the debugger lines are hit and show you your source file (surrounded with some tampermonkey boilerplate).

Which should look like this: chrome paused on tampermonkey script

You can then instrument any other lines you need to from within the debugger.

If you run into trouble, you can also debug the main logic of tampermonkey itself by opening the background page inspection in chrome://extensions. It prints nice messages to let you know what it is up to which you can use to jump around in its code.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionthestralView Question on Stackoverflow
Solution 1 - Google ChromelossleaderView Answer on Stackoverflow