2013-11-23 86 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

相关问题