Nilesh Guria
Nilesh Guria

Reputation: 139

How to keep chrome extension running in background

I would like to know how to keep chrome extensions running in background, i.e user does not need to click on the browser extension button at the right hand corner, and the functionality of the extension is still incorporated into the browser. Something like adblock, etc. which runs whenever we open the browser without the user needing to explicitly click on the extension button.

Thanks.

Upvotes: 4

Views: 7917

Answers (1)

Yoichiro Tanaka
Yoichiro Tanaka

Reputation: 925

Chrome provides many events for your extension. For example, chrome.tabs.on***d, chrome.alerms.***d, chrome.idle.on***d, chrome.runtime.on***d and etc. You can register each event handler with:

chrome.***.on***.addListener((evt) => {...});

In almost cases, it is enough only to handle the events above to implement features you want. That is, I think that you don't need to keep your extension in background. Instead, you should handle each event. If your extension is inactive, and when some event which your event handler is registered occur, your extension becomes active automatically by Chrome.

However, if you want to keep your extension in background strongly, you also can keep it in background by writing the following definition in your manifest.json file:

"background": {
  ...
  "persistent": true
}

Note that not recommended. Basically, you should specify "false" and handle events.

Upvotes: 3

Related Questions