Sunday, April 3, 2011

Debugging greasemonkey-like scripts

I'm writing javascript code that is read in as a string and executed via eval() by a firefox extension. Firebug does "see" my script so I am not able to use breakpoints, see objects, etc.

I am currently using Firefox's error console which I'm starting to find limiting. What are my other options? Ideally, I would be able to use Firebug or something similar to it. How do people generally debug Greasemonkey scripts?

I've tried using Lint and other validators, but my script uses a lot of objects and functions provided by the extension environment, making of a lot of the errors reported irrelevant. Also, the output tends to be too nitpicky (focusing of spacing issues, etc).

From stackoverflow
  • AFIK Firebug supports eval() debugging since 1.1, specifically for GreaseMonkey, you can use GM_log for basic logging, you might find useful this tools also:

  • I've created an interactive console for debugging Greasemonkey scripts. It requires Google Gears, but it includes a persistent history of commands that were typed.

    UPDATE: Here's a link to a blog post describing using GreasyThug to debug a GM script. It's got a step by step procedure to replicate an error and devise a solution.

  • I'm using this code in order to be able to access page DOM using jQuery (no need for unsafeWindow hacks) and log errors to Firebug console:

    (function(_, $){
      try {
        var GM_log = function(obj) { _.console.log(obj); }
    
        // $("#my_div").reaplaceWith("hello world!");
        // _.someFunctionDefinedInTheWebsite();
    
      } catch(e) {
        GM_log(e);
      }
    })(unsafeWindow, unsafeWindow.jQuery);
    

    NOTE: this example assumes that the page you are accessing already uses jQuery

0 comments:

Post a Comment