Refer to the technique of having the same JavaScript to run in both a web page and an iframe, as described in this answer:
For example, suppose you have this page at domain_A.com:
<html>
<body>
<iframe src="http://domain_B.com/SomePage.htm"></iframe>
</body>
</html>
If you set your @match directives like this:
// @match http://domain_A.com/*
// @match http://domain_B.com/*
Then your script will run twice -- once on the main page and once on the iframe as though it were a standalone page.
What are the options to have the two instances of the script to communicate with each other?
This would be needed to sync the instances. For example, have the iframe script-instance execute its task only after the webpage script-instance completed, and vice versa.
The two script instances can communicate with each other using
postMessage()
. Regarding:That's exactly what is shown in this, stunningly brilliant, answer. ;)
But Chrome has bugs in how it presents frames/iframes to extensions (And Chrome userscripts are extensions with an auto-generated manifest).
So, to work around these bugs, you must inject the code that calls
postMessage()
.The following script shows how. It:
Install this script:
Then visit this test page at jsFiddle.
You will see this in the javascript console: