Chrome Extension Message Passing With Injected Code

My latest side-project is stereopaw, a bookmarking service that timestamps streaming music. Imagine a del.icio.us meets Pinterest for music: a user clicks on the browser extension, and the current track’s metadata is available to save in real-time. In building the extension, I wrestled with how to handle cross-communication within the chrome browser ecosystem; namely, how to control injected javascript code on a page, from an extension’s background or content script.

I’ll show an example of javascript injection, and through basic message passing, prevent runaway code when the extension closes by listening for a “shutdown” event.

/* * Note this executes in the *extension's* page context, and the chrome.tabs API * isn't available on the visited page. * @tab.id: the specified tab's id number, that could be retrieved from a call to 'chrome.tabs.query' */chrome.tabs.executeScript(tab.id,{code:"(function() { var e = document.createElement('script') e.setAttribute('id', 'sb-script') e.setAttribute('mode', 'extension') e.setAttribute('src','//[YOURHOST]/[YOURCODE].js) document.body.appendChild(e) })()"})

Inject a Function With Arguments

What if you want to inject a pre-defined function in your extension into your target page? Take the above codeblock one step further and pass in a “stringified” function. Concatenation coerces the function into a string, and you can pass in an args array via JSON.stringify(). In the example below, you’ll still need the tab id, and permission as declared in your manifest.json.

Motivation and Trade-offs

In the case of stereopaw, both the browser extension and marklet are wrappers that load external javascript – the “stereopaw code” – to access track/artist data from the service (soundcloud, youtube.) This offers marklet users (i.e. firefox and mobile) equal functionality as chrome extension users, but brings its own set of tradeoffs: of particular worry is the case where a service changes their audio player, or the manner in which they store metadata. Rather than force users to reinstall an extension with every breaking change, I can update a single code base loaded by both the marklet and extension. The downside is that the extension must now communicate with this decoupled javascript code – tricky – for instance, when the extension is closed, but the “stereopaw code” remains active.

The External Stereopaw Code

Clicking the marklet adds the external “stereopaw code” to the DOM, and launches a setInterval() loop that repeatedly renders track data and an updated timestamp. When the page overlay is closed, the event kills the loop.

With the chrome extension, the external code is similarly added to the DOM, but the extension’s popup lacks direct access to the page’s running javascript; and subsequently, a direct way to kill the same setInterval() loop. While the astute coder might suggest that the extension should contain the loop to clean itself up on close, I decided that a singular change in the extension would be easier to handle, rather than changing both the “stereopaw code” and extension – though it may be the less robust decision.

NB: Implementing an observer pattern is likely even better, but in the interest of “keeping it simple,” I decided to go with a loop.

Detecting a Shutdown Event

Chrome Extension components can cross-communicate via message passing. With stereopaw, the injected code’s setInterval() repeatedly sends a message containing track data and an updated timestamp. Upon closing the browser extension, the injected loop will waste resources, and continously send messages to nobody in particular. The problem compounds when the user runs the extension again, as another loop activates, and messages are now sent twice as often. The solution is two-fold: detect when the extension is closed, and then send a shutdown message / inject some “shutdown code” – or both.

Listen for a port.onDisconnect event, which fires when the popup is no longer active

Inject a shutdown function, or pass a shutdown message

Establishing a connection is straightforward. Simply stick the following in a content script:

/popup.js

1

chrome.extension.connect()

And the latter block in a background page.

/bg.js

123456789101112131415161718192021222324252627282930313233343536373839

varconnection_listener=function(port){varport_listener=function(event){varmsg_listener=function(request,sender,sendResponse){//3if(request.yourmessageobject_from_injected_code){/*option 1: a shutdown message*/sendResponse({shutdown:true})/*option 2: inject a shutdown function*/varkill=function(){/* * do what you must, like clear a setInterval * in your target page's javascript */clearInterval(_interval)}chrome.tabs.executeScript(tab.id,{code:"("+kill+")();"});}chrome.runtime.onMessageExternal.removeListener(msg_listener)//4}}port.onDisconnect.addListener(port_listener);//2}chrome.extension.onConnect.addListener(connection_listener)//1

When the content script is opened, chrome.extension.connect() establishes a connection. This event triggers the connection listener (1) in the background script, which adds a listener for the port.OnDisconnect event (2). Now you’re notified when the extension shuts down.

In this example, when the port.onDisconnect event fires, a message listener (3) is added that sends a shutdown response to the injected code, which is continuously messaging track information to a separate listener in the extension. It’s important to note that various messages may be passed at any given time, so the if-statement acts as a filter for the particular message source that can handle a shutdown response. One could additionally initiate a message through the shared DOM via port.postMessage, but opting for simplicity, I chose to keep the shutdown logic solely in the extension code, as opposed to adding an additional listener in the external “stereopaw code”. Lastly, one can inject shutdown code – the kill() function, as done above.

More detailed examples regarding extension message passing can be found in Google’s documentation.