简体   繁体   English

popup.html中的chrome.tabs.create正在打开无限数量的标签。 如何打开一个标签页?

[英]chrome.tabs.create in popup.html is opening infinite number of tabs. How to open one tab?

This opens infinite number of tabs with popup.html, quite obviously. 显然,这会使用popup.html打开无限多个选项卡。 How do I change it so that only one tab is opened. 如何更改它,以便仅打开一个选项卡。

chrome.tabs.create({'url': chrome.extension.getURL('popup.html')}, function(tab) {
        // Tab opened.

    });

Edit: (A basic popup.html) 编辑:(基本的popup.html)

<!DOCTYPE html>
    <html lang="en">


    <!--<script src="script.js"></script>-->
    <script src="jquery.min.js"></script>
    <script src="popup.js"></script>
      <head>
          <meta charset="utf-8">
          <title> Chrome Extension</title>
          <!--<link rel="stylesheet" href="style.css" />-->
      </head>
      <body id="container">
        <div id="left">
          <div class="input-wrapper">

        hello

          </div>


    </div> <!-- end #left -->
      </body>
    </html>

popup.js popup.js

function openTab()
{
    filename = "popup.html"

  var myid = chrome.i18n.getMessage("@@extension_id");
  chrome.windows.getCurrent(
  function(win)
  {
    chrome.tabs.query({'windowId': win.id},
    function(tabArray)
    {
      for(var i in tabArray)
      {
        if(tabArray[i].url == "chrome-extension://" + myid + "/" + filename)
        {
          // console.log("already opened");
          chrome.tabs.update(tabArray[i].id, {active: true});
          return;
        }
      }
      chrome.tabs.create({url:chrome.extension.getURL(filename)});
    });
  });
}

openTab();

Manifest.json: Manifest.json:

{
  "name": "App",
    "version": "1.1",
  "manifest_version": 2,
  "description": "Test",
  "background_page": "background.html",
  "browser_action": {
    "name": "App",
    "icons": ["icon.png"],
    "default_icon": "icon.png",
   "default_popup":"popup.html"
  },
  "content_scripts": [ {
    "js": [ "jquery.min.js", "background.js" ],
    "matches": [ "http://*/*", "https://*/*"]
  }],
   "permissions": [ "<all_urls>", 
                  "storage",
                  "tabs",
               "contextMenus" ]


}

Here is an example of searhing for specific tab. 这是特定标签的煎烤示例。 If the tab is not present, then the function will open it, otherwise the existing tab will be made active. 如果该选项卡不存在,则该功能将打开它,否则将激活现有的选项卡。

function openTab(filename)
{
  var myid = chrome.i18n.getMessage("@@extension_id");
  chrome.windows.getCurrent(
  function(win)
  {
    chrome.tabs.query({'windowId': win.id},
    function(tabArray)
    {
      for(var i in tabArray)
      {
        if(tabArray[i].url == "chrome-extension://" + myid + "/" + filename)
        {
          // console.log("already opened");
          chrome.tabs.update(tabArray[i].id, {active: true});
          return;
        }
      }
      chrome.tabs.create({url:chrome.extension.getURL(filename)});
    });
  });
}

Alternatively you can store new tab id during its creation in background page (something like a draft below) and clean up the variable when the tab is closed (by means of chrome.tabs.onRemoved.addListener ). 另外,您可以在创建新标签页ID时将其存储在后台页面中(类似于下面的草稿),并在标签页关闭时清理变量(通过chrome.tabs.onRemoved.addListener )。

if(chrome.extension.getBackgroundPage().savedTabId != undefined)
{
  chrome.tabs.create({url:chrome.extension.getURL(filename)},
  function(tab){
    chrome.extension.getBackgroundPage().savedTabId = tab.id;
  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM