Chrome extension: Execute background page only once when chrome starts

前端 未结 3 2003
旧巷少年郎
旧巷少年郎 2021-02-03 14:12

I am playing with chrome extensions, my manifest loads a background page with:

...
"background": { "scripts": ["background_page.js"]         


        
3条回答
  •  醉酒成梦
    2021-02-03 15:04

    What you are using is an Event Page(background_page.js). Event pages are unloaded when the browser detects that the page is not doing anything. So what's happening is that when you open a new tab, the Event page is being reloaded and starts executing from the top again. This way chrome is able to have your app use less memory and speed up the browser.

    If you want to fix the problem simply use persistent:true which will make sure the page "persists" indefinitely or until the user closes the browser. If you would really like to keep your app efficient with memory, you should take a look at the runtime.onSuspend method which gets called each time your Event page unloads. This way you can save stuff before the page gets unloaded so that you can resume where you left off.

提交回复
热议问题