I do not believe that you can do this in one script. If you create two scripts, this will work. Make sure that each script includes only one domain.
First, a script that receives an iframe and sends it a message:
// ==UserScript== // @name Test // @namespace // @include domainA // @version 1 // @grant none // ==/UserScript== if (window === top) // prevents the script from running twice on domain A { window.setTimeout(delay, 15000); function delay() { console.log("Delay"); document.getElementsByTagName("Iframe")[0].contentWindow.postMessage('message', 'domainB'); //The issue is probably here } }
The second script that matches the iframe domain and attaches an event handler:
// ==UserScript== // @name Test IFrame // @namespace // @include domainB // @version 1 // @grant none // ==/UserScript== if (window.addEventListener) { window.addEventListener("message", function (event) { console.log("Done"); } } else // IE8 or earlier { window.attachEvent("onmessage", function (event) { console.log("Done"); } }
Steve
source share