title: Greasemonkey, Chrome and unsafeWindow.foo()
body: <p>I have the following anchor tag, which contains dynamically-generated arguments <code>arg1,...,arg5</code> to the JavaScript function <code>foo()</code>, which runs on the web site-side. There are several of these anchor tags throughout the page in question, with unique <code>id</code> values and <code>argN</code> values:</p>
  
  <pre><code>&lt;a href="#" id="foo1234567890" onclick="javascript:foo(arg1,arg2,arg3,arg4,arg5);return false;" target="_self" title="foobarbaz"&gt; blah &lt;/a&gt;
  </code></pre>
  
  <p>I would like to programmatically fire the <code>foo()</code> function by looping through all hits for this element, searching for the arguments, and passing those arguments to Greasemonkey's <code>unsafeWindow.function(args)</code> call.</p>
  
  <p>Here is the Greasemonkey script I have written so far:</p>
  
  <pre><code>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 &lt; 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();
  </code></pre>
  
  <p>When I install this script and run it, I get the alert with the arguments <code>arg1,...,arg5</code>. If I click OK, I get the following error in Google Chrome 5.0.375.125 for OS X:</p>
  
  <pre><code>Uncaught TypeError: Object [object DOMWindow] has no method 'foo'
  </code></pre>
  
  <p>The web page in question definitely has <code>foo()</code> — if I click on <code>blah</code> then the <code>onclick</code> event triggers <code>foo()</code>. I just can't get it working through this Greasemonkey script.</p>
  
  <p>Am I using <code>unsafeWindow.function(args)</code> incorrectly, or does Google Chrome not support this method of triggering remote JavaScript functions?</p>
  
  <p>Is there a different way I should use this function, or should I use another method entirely?</p>
  
  <p>Thanks for your advice.</p>