According to the docs for packaged applications, which may or may not be the same for extensions, you use the onLaunched event from the chrome.app API to run code when the application starts:
// start doing something when the app first loads.... chrome.app.runtime.onLaunched.addListener(function() { // all your monitoring code will go here });
Please note: if you are creating an extension and your extensions should start listening as soon as the browser loads, you may not need to declare the listener "onLaunched".
In addition, you will need to keep track of changes in the active tab, assuming that you always need to know the URL that the user is enabled on the current active tab. The Chrome Tabs API contains a lot of information that will help you work with tabs from the extension, for example, here is the code for monitoring changes to the active tab:
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) { console.info("This is the url of the tab = " + changeInfo.url);
If the user changes the URL on the active tab, this handler will track this change:
chrome.tabs.onActivated.addListener(function(activeInfo) { .. }
However, the documentation for onActivated states the following:
It starts when the tab in the window is active. Note that the bookmark URL cannot be set when this event fires, but you can listen for onUpdated events that will be notified when the URL is set.
Thus, you will most likely use the onUpdated example.
When you hit a point on the background page where you are faced with the page into which you want to enter JavaScript. For example, to change the background color of a page using JavaScript, you use the following code:
chrome.tabs.executeScript(null, {code:"document.body.bgColor='red'"});
Remember that your background page cannot directly run JavaScript or control the DOM of a web page; that way, you will need to enter the code in a way that includes injections of things like jQuery and other resources that you will need.
Remember to enable tab permissions and the background page in your manifest. json:
/* in manifest.json */ "permissions": [ "tabs", "http://*/*" ], "background": { "scripts": ["background.js"] },
To inject jQuery into a page, you basically write the code that creates the script element, and then you enter it into the page:
chrome.tabs.executeScript(null, {code: "var script = document.createElement('script');" + "script.setAttribute('src', '//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js');" + "script.setAttribute('type','text/javascript');" + "document.getElementsByTagName('head')[0].appendChild(script);" } );
Double-quoted code does not run on your background page. Instead, it starts when you enter a tab page. I use single quotes for content inside double quotes, so the parser is not confused and throws string literals.
Please note that there may be a better, cleaner way to inject such code into a page, but for small, small injections, this is enough.
Keep in mind that if jQuery is already entered on the page, you may run into conflicts.