如何在安装chrome扩展时定期检查是否创建了选项卡

How to periodically check whether a tab is created or not, just when the chrome extension is installed

本文关键字:是否 检查 创建 选项 安装 chrome 扩展      更新时间:2023-09-26

我已经将我的网站设置为chrome扩展。目前,当我点击扩展图标时,它会导航到网站的登录页面。它访问服务器文件,并根据服务器文件的输出设置图标的颜色。现在我需要的是,所有这些操作都是在单击扩展图标时执行的。我希望在chrome中安装扩展时执行这些操作。应该在安装扩展插件时定期检查是否有来自服务器文件的输出。这是我的背景.js

function getGmailUrl() {
    return "http://calpinemate.com/";
}
function isGmailUrl(url) {
    return url.indexOf(getGmailUrl()) == 0;
}
chrome.browserAction.onClicked.addListener(goToPage);
function goToPage() {
    chrome.tabs.query({
        url: "http://calpinemate.com/*",
        currentWindow: true
    }, function(tabs) {
        if (tabs.length > 0) {
            var tab = tabs[0];
            console.log("Found (at least one) Gmail tab: " + tab.url);
            console.log("Focusing and refreshing count...");
            chrome.tabs.update(tab.id, { active: true });
            updateIcon();
        } else {
            console.log("Could not find Gmail tab. Creating one...");
            chrome.tabs.create({ url: getGmailUrl() });
            updateIcon();
        }
    });
}
function updateIcon() {
    var req = new XMLHttpRequest();
    req.addEventListener("readystatechange", function() {
        if (req.readyState == 4) {
            if (req.status == 200) {
                localStorage.item = req.responseText;
                if (localStorage.item == 1) {
                    chrome.browserAction.setIcon({
                        path: "calpine_logged_in.png"
                    });
                    chrome.browserAction.setBadgeBackgroundColor({
                        color: [190, 190, 190, 230]
                    });
                    chrome.browserAction.setBadgeText({
                        text: ""
                    });   
                } else {
                    chrome.browserAction.setIcon({
                        path: "calpine_not_logged_in.png"
                    });
                    chrome.browserAction.setBadgeBackgroundColor({
                        color: [190, 190, 190, 230]
                    });
                    chrome.browserAction.setBadgeText({
                        text:""
                    }); 
                }
            } else {
                // Handle the error
                alert("ERROR: status code " + req.status);
            }
        }
    });
    req.open("GET", "http://blog.calpinetech.com/test/index.php", true);
    req.send(null);
}
if (chrome.runtime && chrome.runtime.onStartup) {
    chrome.runtime.onStartup.addListener(function() {
        updateIcon();
    });
} else {
    chrome.windows.onCreated.addListener(function() {
        updateIcon();
    });
}

仍然对onClicked事件执行所有动作。如何使它在启动事件中执行?

如果要执行定期检查,可以使用chrome.alarms API(与window.setInterval相比,它也适用于非持久性后台传递)
例如:

manifest.json:

"permissions": [
    "alarms",
    ...

background.js:

...
var checkInterval = 5;
chrome.alarms.create("checkServer", {
    delayInMinutes: checkInterval,
    periodInMinutes: checkInterval
});
chrome.alarms.onAlarm.addListener(function(alarm) {
    if (alarm.name === "checkServer") {
        updateIcon();
    }
});