Tag Archives: Chrome Extension Development

As of today, Google has some awesome developer docs for getting started with Chrome extension development. When it comes to more advanced maneuvers however, Stack Overflow is a better option. I hope Google fixes their Chrome docs, but in the meantime if you’re looking for a way to call your background page functions from a content script, here’s a quick guide for getting it to work.

Requirement:

I was developing a chrome extension for Oracle’s PPM Task Management Cloud offering with a Browser Action ( popup.html ) that would communicate with a background page ( background.html ) and multiple content scripts that were run on URLs that matched Oracle’s internal Bug DB web application’s pattern ( https://bug.oraclecorp.com/*&rptno=* ).

The content script (bugdb.js) was supposed to check if a user is logged in by calling a background page function ( bgApplicationController.isBgUserLoggedIn() ) , and then attach a function from the background page ( bgApplicationController.createTodoTask() ) to a DOM element on the current page accessed by the content script. Simple enough, right?

Well the caveat here is that we need to use Chrome’s messaging APIs to be able to communicate between content scripts and the background page.

We’re using chrome.runtime.sendmessage to send a message to the background page. The ‘type’ object field is checked on the background page, along with other data fields if needed. The background page can then invoke functions and send a response back to the content script by passing data in its callback. Check out the code in the background page script below :

Hope that helps! Also do note that the APIs used in this blog post are current as of Feb 2016, however the coding practices are not. Google discourages the use of persistent background pages – something I haven’t taken care of.