I'm trying to create a Google Chrome extension that presses a button on page load. I've been able to trigger the button using VBscript for Internet Explorer by using this code:
IE.Navigate("javascript:changeIframe();")
But, now I need to do it in Google Chrome via an extension. However, the button does not have an id:
<input type="button" value="Start!" onclick="javascript:changeIframe();">
and here's what I've tried so far and nothing seems to work:
window.onload="javascript:changeIframe()";
javascript:changeIframe();
document.getElementById('').click();
document.getElementById('').submit();
It seems that .click
and .submit
is not defined in Google-Chrome's javascript?
The weird thing is that when I use this script:
javascript:changeIframe();
in Chrome's javascript console, it presses the button automatically and everything works fine. But, when I put the same script in my .js file it does not work.
I just need to know what I have to put inside my .js in order to press the button automatically on page load.
I'm trying to create a Google Chrome extension that presses a button on page load. I've been able to trigger the button using VBscript for Internet Explorer by using this code:
IE.Navigate("javascript:changeIframe();")
But, now I need to do it in Google Chrome via an extension. However, the button does not have an id:
<input type="button" value="Start!" onclick="javascript:changeIframe();">
and here's what I've tried so far and nothing seems to work:
window.onload="javascript:changeIframe()";
javascript:changeIframe();
document.getElementById('').click();
document.getElementById('').submit();
It seems that .click
and .submit
is not defined in Google-Chrome's javascript?
The weird thing is that when I use this script:
javascript:changeIframe();
in Chrome's javascript console, it presses the button automatically and everything works fine. But, when I put the same script in my .js file it does not work.
I just need to know what I have to put inside my .js in order to press the button automatically on page load.
Share Improve this question edited Jul 9, 2012 at 1:05 Brock Adams 93.5k23 gold badges240 silver badges304 bronze badges asked Jul 8, 2012 at 23:41 Cogneti BaseCogneti Base 331 gold badge2 silver badges6 bronze badges 2- Not sure why you'd bother using VBScript since it'll only work in IE. – Lee Taylor Commented Jul 8, 2012 at 23:48
- i could do anything i wanted with IE and it worked fine but IE wont let me to run more that 30 instances simultaneously thats the reason why i moved to google chrome – Cogneti Base Commented Jul 9, 2012 at 0:16
2 Answers
Reset to default 11The traditional way to do this is to inject the code1:
var scriptNode = document.createElement ('script');
scriptNode.textContent = 'changeIframe ();';
document.body.appendChild (scriptNode);
You won't normally have to listen for onload
either, as content scripts will fire roughly at that point by default.
1 The extension JS operates in an "Isolated World" and cannot interact with the page's javascript without some tricks that are not needed here.
Great that you got a solution, but thought Id show you another way of simulating a click.
With this way you dont have to inject code or know the function that the onclick will run, just get the button and simulate a click....
// https://developer.mozilla.org/en/DOM/element.dispatchEvent
function simulateClick(obj) {
var evt = document.createEvent("MouseEvents");
evt.initMouseEvent("click", true, true, window,
0, 0, 0, 0, 0, false, false, false, false, 0, null);
var canceled = !obj.dispatchEvent(evt);
/*
if(canceled) {
// A handler called preventDefault
alert("canceled");
} else {
// None of the handlers called preventDefault
alert("not canceled");
}
*/
}
var what = document.querySelector('input[type="button"][value="Start!"]');
simulateClick(what);