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

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

问题描述

我刚刚开始使用 Google Chrome 扩展程序开发,我的项目涉及制作一个扩展程序,单击该扩展程序时会打印当前打开的任何页面/选项卡的 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.

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

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.

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

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.

推荐答案

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

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

"permissions": [
    "tabs"
],

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

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

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

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

代码:

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

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

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