如何使用 javascript 在我的 chrome 扩展中获取当前 Tab 的 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18436245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 11:55:24  来源:igfitidea点击:

How to fetch URL of current Tab in my chrome extension using javascript

javascripturlgoogle-chrome-extension

提问by Darth Coder

I am just getting started with Google Chrome Extension development and my project involves making an extension which when clicked prints the URL of whichever page/tab is currently open.

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

So if I am on google's home page and I click my extension, I need to get "https://www.google.com/" as my output within the extension.

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

I need to do this using javascript and am unable to find code which I understand and which does the job. I read about using "window.location" and "document.href" and stuff but won't that give me the url of just my extension and not the current tab?

我需要使用 javascript 来做到这一点,但我无法找到我理解的代码并且可以完成这项工作。我阅读了有关使用“window.location”和“document.href”等内容的信息,但这不会给我仅我的扩展程序的网址而不是当前选项卡的网址吗?

Please help me get started. Thanks in advance.

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

回答by Musa

Try

尝试

chrome.tabs.getCurrent(function(tab){
        console.log(tab.url);
});

Note you must have the tabspermission set in your manifest file

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

"permissions": [
    "tabs"
],

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

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

or the activeTabpermission if initiated by a click on the extension button[Xan]

activeTab通过单击扩展按钮 [ Xan]启动的权限

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

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



If the above doesn't work try

如果上述方法不起作用,请尝试

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

回答by saadsaf

Using javascript, it will work if you are not using it in popup because javascript in popup will return url of popup therefore, in popup, you have to use Chrome tab API and set permission in Chrome manifest.

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

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

So best way is to use Chrome tab API

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

回答by Clint Eastwood

You need to be careful with what you mean by "current tab". If the user has more than one window open, each of them with multiple tabs, Chrome defines the "current window" as the one that is running the content script that makes use of the chrome.tabsAPI. That happened to me and I solved it by referencing not the "current" window but the last focused one:

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

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

References:

参考:

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

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

Hope it helps!

希望能帮助到你!

回答by Rich Werden

DO NOTuse getSelected

请勿使用getSelected

As per the Chrome Developer Docs: chrome.tabs.getSelectedhas been deprecatedsince Chrome ver.33

根据Chrome 开发人员文档: 自 Chrome 版本 33 以来chrome.tabs.getSelected已被弃用

Insteaduse:

而是使用:

chrome.tabs.query({active:true}, __someCallbackFunction__)like Musa's second suggestion.

chrome.tabs.query({active:true}, __someCallbackFunction__)就像穆萨的第二个建议。

回答by Prem Santh

Here is the answer your looking for without adding new tabspermissions

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

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

回答by jayant singh

this worked for me give it a try

这对我有用试一试

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

回答by Kamil

UPDATE:this method is now deprecated, so please don't use it

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

In my case any above has not worked. So I used this:

就我而言,以上任何一项都不起作用。所以我用了这个:

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

and it worked great! Hope it helps.

效果很好!希望能帮助到你。