Jquery 定期检查选项卡的打开情况
我已经将我的网站设置为google chrome扩展。目前的情况是,当我点击扩展图标时,它会导航到我的网站主页。现在我需要的是,当我安装扩展时,在那一刻(即,当扩展激活时),它必须检查我的网站页面是否已在任何其他选项卡中打开。如果未打开,则创建一个新选项卡。如果已打开,然后它必须自动执行所有功能,就像我单击扩展图标时执行的一样。 这是我的background.jsJquery 定期检查选项卡的打开情况,jquery,google-chrome-extension,Jquery,Google Chrome Extension,我已经将我的网站设置为google chrome扩展。目前的情况是,当我点击扩展图标时,它会导航到我的网站主页。现在我需要的是,当我安装扩展时,在那一刻(即,当扩展激活时),它必须检查我的网站页面是否已在任何其他选项卡中打开。如果未打开,则创建一个新选项卡。如果已打开,然后它必须自动执行所有功能,就像我单击扩展图标时执行的一样。 这是我的background.js function getGmailUrl() { return "http://calpinemate.com/
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();
}
});
}
if (chrome.runtime && chrome.runtime.onStartup) {
chrome.runtime.onStartup.addListener(function() {
updateIcon();
});
} else {
chrome.windows.onCreated.addListener(function() {
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);
}
这里发生的事情是,当我激活扩展时,地址栏附近会出现一个图标。当我单击该图标时,会调用updateIcon(),它读取一个名为index.php的服务器文件,并检索index.php的输出,然后根据该输出设置图标的颜色。现在我需要的是,不点击图标本身(即,当扩展被激活时),所有上述功能都应该定期执行。这意味着当扩展被激活时,它应该检查是否为我的网站打开了任何选项卡,如果是,它必须相应地更改图标颜色。请任何人帮助我。我该怎么做
这是我的manifest.json
{
"name": "Calpine Extension",
"version": "1.0",
"description": "Log on to calpinemate",
"manifest_version": 2,
"browser_action": {
"default_icon": "icon_128.png"
},
"background": {
"persistent": false,
"scripts": ["background.js"]
},
"browser_action": {
"default_title": "Test Extension",
"default_icon": "calpine_not_logged_in.png"
},
"permissions": [
"*://blog.calpinetech.com/test/index.php",
"alarms",
"notifications"
]
}
嗨,我自己得到了答案 chrome.runtime.onInstalled.addListener(updateIcon);必须在chrome.browserAction.onClicked.addListener(gotopage)之前调用