I have the following anchor tag, which contains dynamically-generated arguments arg1,...,arg5 to the JavaScript function foo(), which runs on the w
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");