如何从后台脚本更改BadgeText

时间:2019-01-14 11:22:21

标签: google-chrome-extension

这里有新手。

我正在尝试创建一个非常简单的chrome扩展程序。它的主要功能是根据某些API端点响应来更改其BadgeText。我想每5秒检查一次端点,以便在后台运行它。这是我所拥有的:

manifest.json:

{
  "manifest_version": 2,

  "name": "BusyIndicator",
  "description": "busy indicator",
  "version": "1.0",

  "background": ["background.js"],
  "browser_action": {
   "default_icon": "icon.png",
   "default_popup": "popup.html"
  },
  "permissions": []
}

popup.html:

<!doctype html>
<html>
  <head>
    <title>BusyIndicator</title>
    <script src="popup.js"></script>
  </head>
  <body>
    <h3>nothing here yet</h3>
  </body>
</html>

popup.js现在只是一个空文件

background.js

setInterval(function() {
  fetch('https://toiletbusy.herokuapp.com/')
    .then(function(response) {
      if (response.json.busy == false) {
        chrome.browserAction.setBadgeText({text: 'BUSY'});
      } else {
        chrome.browserAction.setBadgeText({text: 'FREE'});
      };
    });
}, 5000);

我尝试了不同的方法,但似乎background.js根本没有得到执行(例如console.log并未记录任何内容)

0 个答案:

没有答案