chrome.extension.getBackgroundPage() returns null after awhile

后端 未结 2 1652
轮回少年
轮回少年 2021-01-13 15:47

When my chrome extension loads on chrome startup, everything seems to be ok and chrome.extension.getBackgroundPage() returns the right value (lunched from popup.js). But aft

相关标签:
2条回答
  • 2021-01-13 15:50

    Change your background(Event Page) to a real background Page.

    Modify your manifest file from

    "background": {
            "scripts": [
                "background.js"
            ],
            "persistent": false
        }
    

    to

    "background": {
            "scripts": [
                "background.js"
            ],
            "persistent": true
        }
    

    Event pages are very similar to background pages, with one important difference: event pages are loaded only when they are needed. When the event page is not actively doing something, it is unloaded, freeing memory and other system resources.

    Reference

    • Difference between Event and Background Page
    0 讨论(0)
  • 2021-01-13 15:57

    According to the referenced page (Difference between Event and Background Page) there is a better option to get the background while still using Event Page :

    If your extension uses, extension.getBackgroundPage, switch to runtime.getBackgroundPage instead. The newer method is asynchronous so that it can start the event page if necessary before returning it.

    This worked well for me, just do your job in the asynchronous callback, which receives the background page as a function parameter.

    Here is the specification of the method runtime.getBackgroundPage

    0 讨论(0)
提交回复
热议问题