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 expo开发者_运维问答se 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?
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!
Make sure you have Greasemonkey 9.0, or later installed.
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 exampleD:\Program Files\TextPad\TextPad.exe
, but any decent programming editor should do.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.
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.
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.
There is not a way to set Firebug breakpoints on user scripts atm, but the John Barton (of Firebug) and others are planning a way to make this possible, so look out for it in the future!
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?
If you use Scriptish then there is no delay when installing.
Try Venkman JS debugger. I'm not sure whether it has access to the scripts, but it's worth a try. Another thing to test is to have Firebug open/enabled and stick a few "debugger;" statements to see if it picks it up. These are some ideas, I haven't tested them yet.
精彩评论