开发者

chrome extension popup and background ajax

开发者 https://www.devze.com 2023-02-17 00:28 出处:网络
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 the res

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 the result in the popup.

I have the background updating using ajax working and I have the popup trigger the background to make an immediate update using ajax working as well. However, I am stuck on how to display the latest result in the popup...how can I tell when the background ajax call is complete and show the latest result in the popup?

thanks


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

精彩评论

暂无评论...
验证码 换一张
取 消