Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

User Script Debugging in Google Chrome

What's the best way to debug custom User Scripts (aka Greasemonkey) in Chrome? Is there a way to enable User Script tracking in the Developer Tools?

like image 930
d34dh0r53 Avatar asked Oct 19 '09 21:10

d34dh0r53


1 Answers

What kind of debugging do you want? Like Alex said, user scripts will be listed in the same context as debugging the page itself. If you go to the "scripts" tab in the developer tools, you'll see a bar with a dropdown which will allow you to select the appropriate javascript file you wish to debug. Such scripts should have urls that look like chrome-extension://<hash>/<script file>.js. Those scripts will also log to the console of the page they're embedded on.

Additionally, if you want to log in the same place for all pages, you can try building your script as a full chrome extension, using the user script as a content script. Then you can send a message from your content script to your background page and log there. For example, if this were your content script:

function log(text) {
  chrome.extension.sendRequest({'action' : 'log', 'text' : text}, function() {});
};
log("Content script loaded: " + window.location.href);

And this were your background page:

<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
    <script>
      function onRequest(request, sender, callback) {
        if (request.action && request.action == 'log') {
          console.log(request.text);
        }
      };

      chrome.extension.onRequest.addListener(onRequest);
    </script>
  </body>
</html>

You would see each load of the content script in the background page's log.

like image 167
Arne Roomann-Kurrik Avatar answered Oct 07 '22 17:10

Arne Roomann-Kurrik