Chrome Extension Event Logging in Background.js

My extension should provide him some service, and part of his job is to listen to tab creation events. I have all my javascript in popup.js and chrome.tabs.onCreated.addListener itself is called (function (tab) {

The fact is that the extension only listens for tab creation events if the user clicks on the extension symbol (if he opens the extension and then activates popup.js), and if the poupup screen is closed, all listening for events stops. (I checked this by writing a console, and it makes sense if you stop thinking about it) My question is: I want to listen to events for the duration of my life (in other words, since the user opens the chrome), and not only when user popup extension.

I know what the background page is called there, should I try to move all my code there? Am I the normal practice for background pages? It's just that event listeners depend on more functions and more objects, and it seems that almost all of my code will go there. popup.js will remain virtually unchanged. Another thing is that I'm not sure if it is possible to register events on the background page before the "domcontentloaded" event was fired (and this event only fires when the user pops up an extension).

Any help would be appreciated!

+4
source share
1 answer

A background page is the best way to handle this; it sits in the background while the browser is running. See http://code.google.com/chrome/extensions/background_pages.html for more details. It is worth noting that this should not be an HTML page, you can just have a script file.

Everything you need to run regardless of browser action should be in the background of the script; the popup essentially does not exist until you click the button. You can transfer data between background scripts and a popup, although if you need to, see http://code.google.com/chrome/extensions/messaging.html .

Another thing to note is that the background page does not have access to the DOM. There will also be no popup.js, so this may not be a problem.

Hope this helps!

+2
source

All Articles