2013-11-26 30 views
0

我已将我的网站设置为Google Chrome扩展程序。目前发生的情况是,当我点击扩展名图标时,它会导航到我的网站的主页。现在我需要的是,当我安装扩展程序时(即扩展程序变为活动状态)本身,它必须检查我的网站的页面是否已在任何其他选项卡中打开。如果未打开,则创建一个新的选项卡。如果它被打开,那么它必须自动执行所有功能,就像它在我点击扩展图标时执行的一样。 这里是我的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(); 
    } 
    }); 
    } 

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

这里正在发生的事情是,当我启动我的扩展,出现近地址的图标bar.when我点击该图标,在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" 
    ] 

    } 

回答

0

HI我得到了自己的答案..

chrome.runtime.onInstalled.addListener(updateIcon);必须在chrome.browserAction.onClicked.addListener(gotopage)之前调用;