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

£可爱£侵袭症+ 提交于 2019-12-13 07:55:41

问题


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 ?


回答1:


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


来源:https://stackoverflow.com/questions/20186771/how-to-periodically-check-whether-a-tab-is-created-or-not-just-when-the-chrome

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!