簡體   English   中英

popup.html中的chrome.tabs.create正在打開無限數量的標簽。 如何打開一個標簽頁?

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

顯然,這會使用popup.html打開無限多個選項卡。 如何更改它,以便僅打開一個選項卡。

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

    });

編輯:(基本的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" ]


}

這是特定標簽的煎烤示例。 如果該選項卡不存在,則該功能將打開它,否則將激活現有的選項卡。

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

另外,您可以在創建新標簽頁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