Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google chrome extension 如何避免chrome扩展中的选项卡重复_Google Chrome Extension - Fatal编程技术网

Google chrome extension 如何避免chrome扩展中的选项卡重复

Google chrome extension 如何避免chrome扩展中的选项卡重复,google-chrome-extension,Google Chrome Extension,您好,我已将我的网站设置为chrome扩展。问题是,我单击扩展图标的次数会导致许多选项卡打开。我需要在单击扩展图标时,检查选项卡是否已打开。如果已打开,则只显示该选项卡。否则,创建另一个选项卡。我如何操作?请帮助我。我知道这是一个简单的问题。我想不出来。请帮帮我。 这是我的background.js function getGmailUrl() { return "http://calpinemate.com/"; } function isGmailUrl(url

您好,我已将我的网站设置为chrome扩展。问题是,我单击扩展图标的次数会导致许多选项卡打开。我需要在单击扩展图标时,检查选项卡是否已打开。如果已打开,则只显示该选项卡。否则,创建另一个选项卡。我如何操作?请帮助我。我知道这是一个简单的问题。我想不出来。请帮帮我。 这是我的background.js

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

 });

我无法找出问题所在。请帮助我。

问题是for循环中的条件混乱,因此它不会在所有选项卡上循环。
顺便说一句,不推荐使用。改用(见下文)


您好,非常感谢您的回复。我会尝试一下并让您知道。
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() });
        }
    });
});