【问题标题】:How to check URLs of all tabs in all windows? (Google Chrome Extension)如何检查所有窗口中所有选项卡的 URL? (谷歌浏览器扩展)
【发布时间】:2023-03-06 03:18:01
【问题描述】:

我想检查所有窗口中所有选项卡的 URL,如果有与“http://example.com/foo/bar.html”匹配的选项卡,我想关注该选项卡。 (如果没有,我想在新标签中打开“http://example.com/foo/bar.html”。)
但是,我不知道如何检查 URL。

我该怎么做?

【问题讨论】:

    标签: javascript google-chrome url google-chrome-extension


    【解决方案1】:

    我不打算完全概述实现,但请看一下这个条目:

    http://developer.chrome.com/extensions/tabs.html#method-query

    如您所见,它接受“url”检查。

    【讨论】:

      【解决方案2】:

      正如 jshthornton 所说,您可以使用chrome.tabs.query 方法:

      chrome.tabs.query("http://example.com/foo/bar.html", function(tabs)
      {
          // if no tab found, open a new one
          if (tabs.length == 0)
              chrome.tabs.create({ url: "http://example.com/foo/bar.html" });
      
          // otherwise, focus on the first one
          else
              chrome.tabs.update(tabs[0].id, { active: true });
      });
      

      请注意,如果第一个找到的选项卡不在当前活动窗口上,则此代码不会将焦点放在选项卡的窗口上。

      您可以查看以下链接了解更多详情:

      【讨论】:

        猜你喜欢
        • 2013-04-17
        • 2011-10-31
        • 2012-12-15
        • 2015-08-01
        • 2021-01-23
        • 2014-03-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多