2013-11-23 85 views
0

您好我已經設置我的網站作爲擴展鉻。問題是,我點擊多個標籤打開的擴展圖標的次數。我需要當我點擊擴展圖標我必須檢查該選項卡是否已經打開。如果它已經打開,那麼只顯示該選項卡。否則創建另一個選項卡。如何才能做到這一點?請幫助我。我知道這是一個簡單的問題。但是我我無法弄清楚。請幫助我。 這裏是我的background.js如何避免在Chrome擴展中的選項卡重複

function getGmailUrl() { 
    return "http://calpinemate.com/"; 
    } 
    function isGmailUrl(url) { 

     return url.indexOf(getGmailUrl()) == 0; 
     } 
     chrome.browserAction.onClicked.addListener(function(tab) { 
    chrome.tabs.getAllInWindow(undefined, function(tabs) { 
for (var i = 0, tab; tab = tabs[i]; i++) { 
    if (tab.url && isGmailUrl(tab.url)) { 
    console.log('Found Gmail tab: ' + tab.url + '. ' + 
       'Focusing and refreshing count...'); 
    chrome.tabs.update(tab.id, {selected: true});   
    return; 
    } 
    } 
console.log('Could not find Gmail tab. Creating one...'); 
chrome.tabs.create({url: getGmailUrl()}); 
    }); 

}); 

我無法弄清楚什麼是problem.Please幫我

回答

0

的問題是,在你的for循環被搞砸的條件,所以它沒有循環遍歷所有選項卡。
BTW,chrome.tabs.getAllInWindow已棄用。改用chrome.tabs.query(見下文)。

chrome.browserAction.onClicked.addListener(function(tab) { 
    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 }); 
     } else { 
      console.log("Could not find Gmail tab. Creating one..."); 
      chrome.tabs.create({ url: getGmailUrl() }); 
     } 
    }); 
}); 
+0

嗨..非常感謝您的回覆。我會嘗試並讓您知道。 – user1991

+0

非常感謝你..工作好.. – user1991

相關問題