greasemonkey/greasemonkey — Issue #1508: Userscripts are not executed on first iframe load.
Author: MayhemYDG
Created: 2012-02-03 21:51:58+00:00
State: open

Install this userscript, and go on http://www.greasespot.net/.

```javascript
// ==UserScript==
// @name iframe test
// @version 1
// @namespace iframe
// @include http://www.greasespot.net/
// @include http://userscripts.org/
// @run-at document-start
// ==/UserScript==

var d = document;

if(location.hostname === 'www.greasespot.net') {
window.addEventListener('message', function(e) {
d.querySelector('textarea').value += e.data;
}, false);
}

d.addEventListener('DOMContentLoaded', function() {
if(location.hostname === 'userscripts.org') {
parent.postMessage(new Date().toLocaleTimeString() + ': the userscript has now been executed in the iframe.\n', '*');
}
else {
d.documentElement.innerHTML = '';
d.querySelector('iframe').onload = function(e) {
d.querySelector('textarea').value += new Date().toLocaleTimeString() + ': iframe onload event (' + e.target.src + ')\n';
};
setTimeout(function() {
d.querySelector('iframe').src = 'about:blank';
}, 5000);
setTimeout(function() {
d.querySelector('iframe').src = 'http://userscripts.org/';
}, 10000);
}
}, false);
```

Output with Scriptish:

```
22:42:00: the userscript has now been executed in the iframe.
22:42:01: iframe onload event (http://userscripts.org/)
22:42:04: iframe onload event (about:blank)
22:42:09: the userscript has now been executed in the iframe.
22:42:10: iframe onload event (http://userscripts.org/)
```

Output with Greasemonkey:

```
22:43:18: iframe onload event (http://userscripts.org/)
22:43:22: iframe onload event (about:blank)
22:43:27: the userscript has now been executed in the iframe.
22:43:27: the userscript has now been executed in the iframe.
22:43:28: iframe onload event (http://userscripts.org/)
```

Not only does is the script not executed on the first load, but it seems to be executed twice on the second load?
It is executed fine on Chrome as well as on Scriptish, except that it doesn't let it access the `parent` so it cannot send the message, but that's [another issue](crbug.com/20773).