自动更新Chrome扩展徽章的方法 [英] Method to autoupdate Chrome Extension badge

查看:135
本文介绍了自动更新Chrome扩展徽章的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建Chrome扩展程序,使徽章显示从API返回的数字。我有代码工作正常,但我听它的DOMContentLoaded,所以它只会在用户打开扩展时更新。

I'm building a Chrome extension that makes the badge show a number returned from an API. I have the code working fine, but I have it listening for DOMContentLoaded, so it only updates when the user opens up the extension.

我希望扩展程序检查API每次浏览器加载页面时。我不需要改变页面中的任何内容,我只是想用它来计时。

I would like the extension to check the API every time the browser loads a page. I do not need to change anything in the page, I just want to use it for timing.

我不确定我应该使用什么,我应该使用后台页面活动页面,还是别的?什么是最好的方式去做这件事?

I'm not sure what I should be using, should I be using background pages, event pages, or something else? What would be the best way to go about this?

提前致谢!

推荐答案

您希望每当浏览器加载页面时使用的API是 chrome .tabs.onUpdated 。你会有:
$ b

The api you want for "every time the browser loads a page" is chrome.tabs.onUpdated. You’d have:

chrome.tabs.onUpdated.addListener(function(tabId,changeInfo,tab) {
    chrome.browserAction.setBadgeText({"text":"ABCD","tabId":tabId});
});

一种简单的开发方法是使用背景页面,让它工作,然后弄清楚您需要将其转换到活动页面。然而,有了这个存根,没有什么能阻止你将它作为活动页面。

An easy approach for development is to use a background page, get it working, and then figure out what changes you need to convert it to an event page. With this stub however, nothing is stopping you from making it an event page.

这篇关于自动更新Chrome扩展徽章的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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