简体   繁体   中英

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. 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)

<!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

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:

{
  "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 ).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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