I have a method in the Javascript runtime for a tab that I want to fire from my extension's content script. At face value, this calls for message passing between contexts, but I cannot find any documentation of CS-to-tab messages, only tab-to-CS or CS-to-background. Essentially I want to flip the Communication with the embedding page section of the content scripts reference.
So far, I am attaching an event listener to the tab's window
by injecting a small Javascript payload (this will trigger the tab method on the correct type of message):
listenerScript = document.createElement('script');
listenerScript.textContent = "window.addEventListener('message',function(ev){console.log('New event: ' + ev);},false);console.log('installed');";
(document.head||document.documentElement).appendChild(listenerScript);
listenerScript.remove();
This listener is registered, however I can't figure out how to trigger it from my content script. window.postMessage()
in the content script doesn't seem to do anything, and the documentation only talks about using chrome.runtime
to create a port, which is for background scripts.
How do I pass messages, in the simplest way possible, from content script to tab?