如何使用javascript在我的Chrome扩展中获取当前选项卡的URL [英] How to fetch URL of current Tab in my chrome extension using javascript

查看:169
本文介绍了如何使用javascript在我的Chrome扩展中获取当前选项卡的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



所以如果我在谷歌的主页上,点击我的扩展,我需要获得 https://www.google.com/ 作为扩展名中的输出。



我需要使用javascript并且我无法找到我理解的代码以及这项工作。我读了关于使用window.location和document.href和东西,但不会给我只是我的扩展,而不是当前标签的网址?

请帮我开始。

解决方案

试试

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

请注意,您必须拥有标签权限集在您的清单文件中

 权限:[
标签
],

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



activeTab 权限,如果通过点击扩展按钮启动[ Xan ]



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






如果上面的方法不起作用,请尝试使用

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


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.

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.

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?

Please help me get started. Thanks in advance.

解决方案

Try

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

Note you must have the tabs permission set in your manifest file

"permissions": [
    "tabs"
],

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

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

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

这篇关于如何使用javascript在我的Chrome扩展中获取当前选项卡的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆