Modifying javascript functions in <script> tags

1,659 views
Skip to first unread message

Steve

unread,
Aug 30, 2007, 10:42:34 AM8/30/07
to greasemonkey-users
For various reasons I want to modify a function on a page which is
contained within <script></script> tags on a web page. I want to be
able to modify the function's code as a string, so that I can use
methods such as indexof, substring and replace.

Since GreaseMonkey's user scripts run after the javascript has been
evaluated, it isn't enough to modify the text attribute of the script
node.

I've discovered it's possible to access the function through
unsafeWindow.function_name, uneval() it, and modify the resulting
string. This is no different really to what I could already do with
the script node's text attribute, apart from separating the function
from any other code within the script tags.
I then managed to make the function anonymous by removing the function
name from the string with replace(), make my modifications, eval() it
and assign it to unsafeWindow.function_name. This sucessfully replaces
the function and when the function is called by the page my modified
function executes as desired instead of the original one.

Unfortunately, there are two problems with this:
1) This uses unsafeWindow, which of course should be avoided if
possible
2) The new function executes in greasemonkey's context. Not only does
this open a security hole, but the script I'm editing no longer works
because when the modified function attempts to access global variables
or other functions it cannot do so because they now need to be
accessed through unsafeWindow.

What I want is some way of modifying the code and recompiling the
function so that it executes within the page's context, preferably
avoiding unsafeWindow.

Anyone have any idea how to do this?

Regards,
-Steven Ayre

Anthony Lieuallen

unread,
Aug 30, 2007, 10:44:47 AM8/30/07
to greasemon...@googlegroups.com
On 8/30/2007 10:42 AM, Steve wrote:
> What I want is some way of modifying the code and recompiling the
> function so that it executes within the page's context, preferably
> avoiding unsafeWindow.

Best bet: http://wiki.greasespot.net/Location_hack

Steve

unread,
Aug 30, 2007, 11:20:10 AM8/30/07
to greasemonkey-users


Cheers, that's working now. I'm unfortunately still using
unsafeWindow, but it works and the modified function no longer runs in
GreaseMonkey's sandbox which is what I wanted.


Here's the code for what I'm doing:
// Get source code of function
var s = uneval(unsafeWindow.function_name)

// Make the function anonymous
s = 'function (arg1,arg2) ' + s.substring(s.indexOf('{'));

// Make my modifications
// e.g. s = s.replace('from', 'to');
// blah blah blah

// Replace the function with a wrapper
unsafeWindow.function_name = eval(function (arg1,arg2) {
// Uses the location hack to get the modified function to run
outside the GreaseMonkey scope
GM_log('performing location hack');
location.href = 'javascript:('+eval(s)+')('+arg1+','+arg2+')';
});


Regards,
-Steven Ayre

Reply all
Reply to author
Forward
0 new messages