Using postmessage to communicate with iframe - where is the error?

I have a Greasemonkey script that should send postmessage to a built-in iframe where the same script runs the function. My focus is on a lightweight message to run a function inside an iframe. Website and iframe are not in the same domain. My js skills are pretty bad and i can't find the problem.

thanks for reading

// ==UserScript== // @name Test // @namespace // @include domainA // @include domainB // @version 1 // @grant none // ==/UserScript== if ("domainA" === location.hostname) { 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 } } } else // domain B { window.onmessage = function() // or here { console.log("Done"); // Didnt start } } 

edit: Iam using Firefox

+7
javascript greasemonkey iframe
source share
2 answers

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"); } } 
+3
source share

Unfortunately, I do not have access to the iframe html, so I tried to add script code using Greasemonkey. I found this code in an older stackoverflow question. I don’t see something. Is there a way to check iframe for my embed code?

Edit // Ok nvm rawframe.contentDocument does not work if I add console.log("rawframe.contentDocument") to my script, it returns NULL and the script.text line blocks my script hole if I comment on it console.log("Delay") works again.

 // ==UserScript== // @name Test // @namespace // @include https://domainA.com // @version 1 // @grant none // ==/UserScript== if (window === top) // prevents the script from running twice on domain A { var rawframe = document.getElementsByTagName("Iframe")[0]; var framedoc = rawframe.contentDocument; // is NULL if (!framedoc && rawframe.contentWindow) { framedoc = rawframe.contentWindow.document; } var script = doc.createElement('script'); script.type = "text/javascript"; script.text = "window.addEventListener("message", function (event) {console.log("Done");}"; framedoc.body.appendChild(script); window.setTimeout(delay, 15000); function delay() { console.log("Delay"); document.getElementsByTagName("Iframe")[0].contentWindow.postMessage('message', 'https://domainB.com'); } } 
0
source share

All Articles