【问题标题】:How to fetch URL of current Tab in my chrome extension using javascript如何使用 javascript 在我的 chrome 扩展中获取当前选项卡的 URL
【发布时间】:2013-08-28 11:27:37
【问题描述】:

我刚刚开始使用 Google Chrome 扩展程序开发,我的项目涉及制作一个扩展程序,单击该扩展程序会打印当前打开的任何页面/选项卡的 URL。

因此,如果我在 google 的主页上单击我的扩展程序,我需要在扩展程序中获取“https://www.google.com/”作为我的输出。

我需要使用 javascript 来执行此操作,但我无法找到我理解并且可以完成这项工作的代码。我读过关于使用“window.location”和“document.href”之类的东西,但这不会给我我的扩展名而不是当前选项卡的url吗?

请帮助我开始。提前致谢。

【问题讨论】:

标签: javascript url google-chrome-extension


【解决方案1】:

请注意,您必须在清单文件中设置 tabs 权限

"permissions": [
    "tabs"
],

http://developer.chrome.com/extensions/tabs.html

activeTab 权限(如果通过单击扩展按钮[Xan] 启动)

https://developer.chrome.com/extensions/activeTab


代码:

chrome.tabs.query({currentWindow: true, active: true}, function(tabs){
    console.log(tabs[0].url);
});

【讨论】:

  • 嗯,我尝试了代码,但它不起作用。这是我在控制台上遇到的错误: Uncaught TypeError: Cannot read property 'url' of undefined
  • 太棒了 :D 第二个解决方案很好用谢谢。你能解释一下答案吗?我的意思是当我们已经在使用 currentWindow:true 时,active:true 表示什么?另外, tabs[] 的内容到底是什么?
  • 注意: 这不再需要tabs 权限。如果通过点击扩展按钮启动,activeTab 权限就足够了。
  • tabs 是这么重的权限!考虑使用:activeTab
【解决方案2】:

使用 javascript,如果您不在弹出窗口中使用它,它将起作用,因为弹出窗口中的 javascript 将返回弹出窗口的 url,因此,在弹出窗口中,您必须使用 Chrome 选项卡 API 并在 Chrome 清单中设置权限。

chrome.tabs.query({currentWindow: true, active: true}, function(tabs){
    console.log(tabs[0].url);
});

所以最好的方法是使用 Chrome tab API

【讨论】:

  • 这是最相关的答案,应该更新为最佳答案。
  • @saadsaf 我很难弄清楚如何从这个回调中分叉 tabs[0].url 。它应该/只能在查询的回调函数中使用吗?
  • @A13X 请详细说明您要实现的目标并在此处粘贴一些代码,以便我们一起有效地解决它。
  • 没关系。我试图将已经在我的 popup.js 中声明的全局变量设置为tab[0].url。它一直返回未定义。相反,我选择直接将 query() 放入我的 window.onload 并注入我想要的 HTML 文档(以显示 url)。它有效。
【解决方案3】:

您需要注意“当前标签”的含义。如果用户打开了多个窗口,每个窗口都有多个选项卡,Chrome 将“当前窗口”定义为运行使用chrome.tabs API 的内容脚本的窗口。这发生在我身上,我通过引用不是“当前”窗口而是最后一个焦点来解决它:

chrome.tabs.query({ active: true, lastFocusedWindow: true }, function (tabs) {
    // Do something
});

参考资料:

https://developer.chrome.com/extensions/windows#current-window https://developer.chrome.com/extensions/tabs#method-query

希望对你有帮助!

【讨论】:

    【解决方案4】:

    更新: 如果您在清单 版本 3 上,请继续以下操作,

     async function getCurrentTab() {
     let queryOptions = { active: true, currentWindow: true };
    
      let [tab] = await browser.tabs.query(queryOptions);
      localStorage.setItem('tabname' , tab);
       return tab;
      }
    
     getCurrentTab()
     .then((data) => { console.log('newdata',data)})
     .then(() => { console.log('error')});
    

    【讨论】:

      【解决方案5】:

      请勿使用getSelected

      根据Chrome Developer Docschrome.tabs.getSelected 自 Chrome 版本 33 起已弃用

      改为使用:

      chrome.tabs.query({active:true}, __someCallbackFunction__) 喜欢 Musa 的第二个建议。

      【讨论】:

        【解决方案6】:

        这是您在不添加新的tabs 权限的情况下寻找的答案

        chrome.browserAction.onClicked.addListener(function(e){
             console.log(e.url); 
             //give you the url of the tab on which you clicked the extension
        })
        

        【讨论】:

        • 这似乎是最新的实现方式。
        【解决方案7】:

        此处发布的解决方案不知何故对我不起作用,但在 sn-p 以下工作没有任何问题-

        let currentURL = window.location.href;
        console.log(currentURL)
        

        【讨论】:

        • 有趣,当上述其他解决方案都没有时,这对我也有效。不过我的问题;这对生产来说是不好的做法吗?这可能会返回不准确的值吗?
        【解决方案8】:

        请注意chrome.tabs.query()chrome.tabs.getCurrent 在从多个内容脚本短时间调用时会遇到竞争条件。

        返回的标签不一定是请求者的标签。

        更简单的方法是向后台线程发送消息。此消息包含作为tab 参数的发送选项卡。

        后台线程可以取这个参数,直接把它包含在它对消息的响应中。

        这不仅可以防止竞争条件,而且速度更快,因为它不使用异步操作(与对 chrome.tabs.query() 的调用不同)

        例子:

        内容脚本

        var myTab;
        chrome.runtime.sendMessage({ message: "get_tab_msg" }, (response) => {
            myTab = response.tab;
        });
        

        后台脚本

        chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
            if (request.message === "get_tab_msg") {
                // The content script has asked for the tab.
                sendResponse({tab: sender.tab});
            }
        });
        
        

        【讨论】:

          【解决方案9】:

          更新: 此方法现已弃用,因此请不要使用它

          在我的情况下,以上任何一项都没有奏效。所以我用了这个:

          chrome.tabs.getSelected(null, function(tab) {
              console.log(tab.url)
          })
          

          而且效果很好!希望对您有所帮助。

          【讨论】:

          • getSelected() 已被弃用,不应使用。 chrome.tabs.query({active: true, currentWindow: true}, function (arrayOfTabs) { console.log(tabs[0].url); });是获取当前选项卡(和 URL)的当前方式。
          • 对 manifest 使用 'activeTab' 权限并尝试 chrome.tabs.query({active: true},function(tab){console.log(tab)}) 它应该可以工作 不建议使用getSelected() 已弃用
          【解决方案10】:

          这对我有用,试试看

          chrome.tabs.query({
          active: true,
          lastFocusedWindow: true
          }, function(tabs) {
          // and use that tab to fill in out title and url
          var tab = tabs[0];
          console.log(tab.url);
          alert(tab.url);
           });
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多