Okay, I almost have it working now. A background page was indeed able
to receive the SSE messages without issue. Now I have another problem:
how can I get the messages from the background page into JS running in
the web page's DOM context? Previously, I was injecting a script onto
the page using "Method 1" from this stack overflow answer:
http://stackoverflow.com/questions/9515704/building-a-chrome-extension-inject-code-in-a-page-using-a-content-script/9517879#9517879
But this injected code cannot receive the message from the background
script; running 'chrome.runtime.onMessage.addListener(...)' gets the
error message:
Uncaught TypeError: Cannot read property 'addListener' of undefined
So how can I pass the message through?
To be clear, I have three different JS files running in three contexts:
1. background.js is running as a background page, and is able to
receive my SSE events and dispatch messages using code like this:
chrome.tabs.query({url: "
https://play.google.com/music/*"}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, msg, function() {});
});
2. playctrl.js runs in the DOM context of the target page itself and
knows how to dispatch DOM events that interact with the page. This
script is *not* able to receive the events from the background page.
3. contentscript.js injects playctrl.js onto the page, using code like this:
s = document.createElement("script");
s.src = chrome.extension.getURL("playctrl.js");
s.onload = function() { return this.parentNode.removeChild(this); };
(document.head || document.documentElement).appendChild(s);
This script *is* able to receive events from the background page (but
it cannot dispatch DOM events onto the page itself, hence the need for
injecting playctrl.js onto the page).
Anyone have more advice?
Thanks!
Caleb