且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何定期检查是否创建了选项卡,仅在安装了Chrome扩展程序时才会创建

更新时间:2023-09-11 15:29:46

如果要执行定期检查,可以使用 chrome.alarms API (它也可用于非持久性背景 - 页面 - 相比于 window.setInterval )。

例如:

manifest.json
$ b

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


I have set my website as a chrome extension.Presently when I click on the extension icon,it navigates to website's login page.And it access a server file and set's the color of the icon according to the output of the server file.Now what I need is that,all these action is performed when the extension icon is clicked.I want these actions to be performed just when the extension is installed in chrome.It should be checked periodically whether there is output from the server file,just when the extension is installed. Here is my background.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();
    });
}

Still all the actions are performed on the onClicked event. How can I make it perform on startup event ?

If you want to perform a periodic check, you can utilize the chrome.alarms API (which will also work with non-persistent background-pages - in contrast to window.setInterval).
E.g.:

In manifest.json:

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

In background.js:

...
var checkInterval = 5;
chrome.alarms.create("checkServer", {
    delayInMinutes: checkInterval,
    periodInMinutes: checkInterval
});

chrome.alarms.onAlarm.addListener(function(alarm) {
    if (alarm.name === "checkServer") {
        updateIcon();
    }
});