views:

140

answers:

1

I have the following anchor tag, which contains dynamically-generated arguments arg1,...,arg5 to the JavaScript function foo(), which runs on the web site-side. There are several of these anchor tags throughout the page in question, with unique id values and argN values:

<a href="#" id="foo1234567890" onclick="javascript:foo(arg1,arg2,arg3,arg4,arg5);return false;" target="_self" title="foobarbaz"> blah </a>

I would like to programmatically fire the foo() function by looping through all hits for this element, searching for the arguments, and passing those arguments to Greasemonkey's unsafeWindow.function(args) call.

Here is the Greasemonkey script I have written so far:

function removeAllProperties() {
    var xpath = "//A[@title='foobarbaz']";
    var actionNodes = document.evaluate(
                                    xpath,
                                    document,
                                    null,
                                    XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
                                    null
                                    );
    var total = actionNodes.snapshotLength;

    for (var i=0; i < total; i++) {
        var candidate = actionNodes.snapshotItem(i);
        //alert(candidate.nodeName);                                                                                                                                                                                                                                                      
        if (candidate.nodeName === "A") {
            var onclick_value = candidate.getAttribute('onclick');
            var needle = /javascript:foo\((.*)\);return false\;/;
            onclick_value.match(needle);
            //alert(onclick_value);                                                                                                                                                                                                                                                       
            //alert(RegExp.$1);                                                                                                                                                                                                                                                           
            var script_args = RegExp.$1;
            if (confirm(script_args)) { 
                unsafeWindow.foo(script_args); 
            }
        }
    }
}

removeAllProperties();

When I install this script and run it, I get the alert with the arguments arg1,...,arg5. If I click OK, I get the following error in Google Chrome 5.0.375.125 for OS X:

Uncaught TypeError: Object [object DOMWindow] has no method 'foo'

The web page in question definitely has foo() — if I click on blah then the onclick event triggers foo(). I just can't get it working through this Greasemonkey script.

Am I using unsafeWindow.function(args) incorrectly, or does Google Chrome not support this method of triggering remote JavaScript functions?

Is there a different way I should use this function, or should I use another method entirely?

Thanks for your advice.

+3  A: 

In Chrome, you're not allowed to access variables/functions defined by the content page from the userscript context (see here).

Instead of calling unsafeWindow.foo, you could just call candidate.onclick() in your loop.

If that isn't what you want, you either have to inject the function call into the DOM using something like

var s = document.createElement("script");
s.innerHTML = "foo("+ script_args + ")";
document.body.appendChild(s);

or assign a JS-uri to location:

location.assign("javascript:foo(" + script_args +");void 0");
Ventero
Cheers, much appreciated!
Alex Reynolds