I already read these methods as alternative for unsafeWindow. But I still can't get it works to use the Recaptcha object without unsafeWindow. My code using unsafeWindow is (this code works fine):
var myscript= document.createElement('script');
myscript.setAttribute('src','http://www.google.com/recaptcha/api/js/recaptcha_ajax.js');
document.body.appendChild(myscript);
Recaptcha = unsafeWindow.Recaptcha;
Recaptcha.create(theKey,"recaptcha_widget_div",{theme:"red"});
I tried this with no luck:
//after script inject
Recaptcha = location.assign("javascript:Recaptcha();void(0)");
Recaptcha.create(theKey,"recaptcha_widget_div",{theme:"red"});
What did I do wrong with the location hack?
Note that I want the code to be usable in a Chrome content script, too.
location.assign()
loads a new page; "poofing" away the script and any variables like Recaptcha
.
In this case, go ahead and use unsafeWindow
. It's not really that big a deal unless you know a webmaster is targeting Greasemonkey scripts.
It's even less dangerous in this case, because you are adding the recaptcha code. The target page isn't.
Update:
The OP indicated that this is to work in Chrome content scripts too. In that case, inject both parts like so:
function addJS_Node (text, s_URL) {
var D = document;
var scriptNode = D.createElement ('script');
if (text) scriptNode.textContent = text;
if (s_URL) scriptNode.src = s_URL;
var targ = D.getElementsByTagName ('head')[0] || D.body || D.documentElement;
targ.appendChild (scriptNode);
}
addJS_Node (null, 'http://www.google.com/recaptcha/api/js/recaptcha_ajax.js');
//-- Might need a delay here, but probably not.
addJS_Node ('Recaptcha.create(theKey,"recaptcha_widget_div",{theme:"red"});');