Chrome扩展弹出窗口和背景ajax [英] chrome extension popup and background ajax

查看:149
本文介绍了Chrome扩展弹出窗口和背景ajax的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要求,即每隔10分钟连续更新一次background.html,当我点击弹出窗口时,它应该立即触发背景更新并在弹出窗口中显示结果。

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.

我使用ajax工作后台更新,我弹出触发背景以使用ajax进行即时更新。然而,我坚持如何在弹出窗口中显示最新结果...如何知道背景ajax调用何时完成并在弹出窗口中显示最新结果?

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. 在你的Popup中,你可以注册 chrome.extension.onRequest.addListener ,然后在您的背景页面中 chrome.extension.sendRequest

  2. 访问Popup DOM,您可以从 chrome.extension.getViews ({类型: '弹出'}),一旦你得到了,你可以在该DOM中调用一个方法。在弹出窗口中,您可以使用 chrome.extension轻松访问后台页面。 getBackgroundPage()。对于这两种情况,您都会返回一个DOMWindow。

  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.

我个人会使用#2,因为您属于相同的扩展过程,不需要与注入的内容脚本进行通信。

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();
}

希望这有助于您。

这篇关于Chrome扩展弹出窗口和背景ajax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆