简体   繁体   English

在 background.js 中的 Chrome 扩展中的 setInterval

[英]setInterval in Chrome extension in background.js

There have been quite a few similar questions on setInterval in background.js in a Chrome extension but none of the answers worked for me.在 Chrome 扩展中的 background.js 中的 setInterval 有很多类似的问题,但没有一个答案对我有用。 I have a simple extension that checks connectivity to a server by calling an API and checking whether there is a 200 response and then updating the extension icon in the tray accordingly.我有一个简单的扩展程序,它通过调用 API 并检查是否有 200 响应然后相应地更新托盘中的扩展程序图标来检查与服务器的连接。

background.js背景.js

chrome.runtime.onInstalled.addListener(() => {

   chrome.browserAction.setIcon({path: "/images/checking.png"});
   console.log('VPN check extension started');
    // main block - make API calls periodically and monitor the server response
    async function ping()  {
        try {
            const response = await axios.get('http://[IP]/api/v1/ping', {
                timeout: 4000
            });
            access = response.status;
            if (access == 200) {
            chrome.browserAction.setIcon({path: "/images/OK_53017.png"});
        } else {
            chrome.browserAction.setIcon({path: "/images/not_OK.png"});
        }
        } catch (error) {
            chrome.browserAction.setIcon({path: "/images/not_OK.png"});
        }
    }

    window.setInterval(ping, 1000 * 10);
});

chrome.runtime.onStartup.addListener(() => {

   chrome.browserAction.setIcon({path: "/images/checking.png"});
   console.log('VPN check extension started');
    // main block - make API calls periodically and monitor the server response
    async function ping()  {
        try {
            const response = await axios.get('http://[IP]/api/v1/ping', {
                timeout: 4000
            });
            access = response.status;
            if (access == 200) {

            chrome.browserAction.setIcon({path: "/images/OK_53017.png"});
        } else {
            chrome.browserAction.setIcon({path: "/images/not_OK.png"});
        }
        } catch (error) {
            chrome.browserAction.setIcon({path: "/images/not_OK.png"});
            console.log('error');
        }
    }

    window.setInterval(ping, 1000 * 10);
});

Neither onStartup nor onInstalled works well - when I restart Chrome or switch windows the extension becomes unresponsive. onStartup 和 onInstalled 都不能正常工作 - 当我重新启动 Chrome 或切换 windows 时,扩展程序变得无响应。

Manifest显现

{
    "name": "Access status",
    "version": "0.0.3",
    "description": "Chrome extension to check if access to the network is provided.",
    "background": {
      "scripts": ["axios.min.js", "background.js"],
      "persistent": false
    },
    "browser_action": {
      "default_popup": "popup.html",
      "default_icon": {
        "16": "images/checking.png"
      },
      "icons": {
      "16": "images/checking.png"
      }
    },
    "permissions": ["<all_urls>"],
    "manifest_version": 2,
  }

content.js is empty content.js 为空

Any suggestion how to make it work, regardless of which tab is in focus or which window is open and get it to set the interval when fresh Chrome opens?任何建议如何使它工作,无论哪个选项卡处于焦点或哪个 window 是打开的,并让它设置新 Chrome 打开时的间隔? Thanks谢谢

A quick google brought up this post .一个快速的谷歌提出了这篇文章 Basically you should probably try using the alarms API instead since chrome will just kill your background script in order to optimize performance.基本上,您应该尝试使用警报 API 代替,因为 chrome 只会杀死您的后台脚本以优化性能。

Your code would probably work at least until someone restarts the browser if you had the background as persistent: true .如果您将背景设置为persistent: true ,您的代码可能至少在有人重新启动浏览器之前可以工作。 However that option seems to be deprecated now.但是,该选项现在似乎已被弃用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM