Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stepping through a greasemonkey script

I know that firebug has no access to greasemonkey scripts but I was wondering if there was a way to expose the script to firebug and have it set a breakpoint. I would like to be able to eventually step through the scripts that I'm working on.

On a similar note: is there any way to test scripts without having to wait every time for 4 seconds to be able to install it?

like image 296
qwertymk Avatar asked Mar 07 '11 23:03

qwertymk


1 Answers

Easy part first:

"On a similar note: is there any way to test scripts without having to wait every time for 4 seconds to be able to install it?"

Yes!

  1. Make sure you have Greasemonkey 9.0, or later installed.

  2. Make sure you have an editor configured with GM. You can set this in the GM options, or open about:config and set greasemonkey.editor, for example D:\Program Files\TextPad\TextPad.exe, but any decent programming editor should do.

  3. Now Edit the script by opening GM's Script-Manager and pressing the appropriate Edit button. Your editor should open with the correct file loaded and any changes you make will take effect immediately, every time you save the file.

  4. Beware that changes made this way to @require directives, will still not take effect. That is, the new file will not get copied nor be used. You must still uninstall/reinstall to get @require changes to stick.


"I was wondering if there was a way to expose the script to firebug and have it set a breakpoint. I would like to be able to eventually step through the scripts that I'm working on."

There is a new add-on, FireBugMonkey, that might help (I've yet to need to try it).

Talk of getting Firebug to work well on GM scripts has been ongoing for over 4 years.   Here is the most recent/relevant thread on the Greasemonkey-Dev Group.

For the near future, there is no way to step through GM scripts that use GM_ functions.

You can work around this, for parts of code that don't use such functions, by injecting that code into the target page, where Firebug can then see it.

For example, structure your code like so:

function localMain ()
{
    /*--- Put EVERYTHING inside this wrapper, functions and variables.
        Call or use nothing else that's defined in the GM script here.
        You can use objects in the source page's scope, though.
    */
    console.log ("Hiya!");
}


Then on Firefox, you can use unsafeWindow.localMain = localMain; to inject the code, Firebug will see it.

unsafeWindow.localMain(); runs the code from GM, localMain(); runs it from the Firebug console.

Beware that this method does expose a route for malicious JavaScript (from the target page) to obtain elevated privileges and potentially pwn your system (one of the reasons why GM was moved to a sandbox in the first place). But, it's quick and simple when targeting trusted pages.

~~~
Alternatively, you could inject the script like so:

var scriptNode          = document.createElement ("script");
scriptNode.textContent  = localMain.toString() + "\n localMain ();";
document.body.appendChild (scriptNode);

This method works on all relevant browsers.

~~~
Libraries, like jQuery, may be copied or injected in similar fashion.

like image 82
Brock Adams Avatar answered Sep 26 '22 06:09

Brock Adams