Using GreasyThug to Answer a Greasemonkey Question

On StackOverflow a user asked: How can I create an object of a class which defined in the remote page?

The page includes code like this (which I entered into Firebug):

function foo(){ this.bar = 0; }

Then I verified that it could be read from Greasemonkey with the GreasyThug console by the following expression:

_foo = unsafeWindow.foo;
x = new _foo();
Debugging with GreasyThug
Debugging with GreasyThug

This caused a “Not enough arguments” error, whatever the hell that is. Not quite the poster’s actual error. What if we added an argument? “Illegal Value” Bingo! Replicated the issue. Now to solve it.

Let’s try and migrate the function over into the Greasemonkey script zone.

_foo = eval('(' + unsafeWindow.foo.toSource() + ')')
=> function foo(){ this.bar = 0; }

That’s the ticket! Now to instantiate and verify:

The magic of a debugging thug
The magic of a debugging thug

Ship it! Holla!