chrome extension popup and background ajax

后端 未结 1 1761
别跟我提以往
别跟我提以往 2021-02-10 07:07

I have a requirement where the background.html continous to update every 10 minutes and when I click on the popup it should trigger the background to update immediately and show

相关标签:
1条回答
  • 2021-02-10 07:31

    Well, if you want to listen for changes on the Background Page, you have two ways to do what you want.

    1. In your Popup, you can register chrome.extension.onRequest.addListener in your Popup page, and in your background page you can chrome.extension.sendRequest when you get stuff updated.
    2. You have direct access to the Popup DOM, you can get an instance from chrome.extension.getViews({type:'popup'}), and once you get that, you can just call a method in that DOM. From the popup, you can access the background page easily too with chrome.extension.getBackgroundPage(). For both cases, you get a DOMWindow returned.

    I personally would use #2 because you belong in the same extension process, you do not need to communicate to an injected Content Script.

    var popups = chrome.extension.getViews({type: "popup"});
    if (popups.length != 0) {
      var popup = popups[0];
      popup.doSomething();
    }
    

    Hope this helps.

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