我在使用Google Chrome扩展程序时感到非常有趣,我想知道如何将当前标签页的URL存储在变量中?
我在使用Google Chrome扩展程序时感到非常有趣,我想知道如何将当前标签页的URL存储在变量中?
chrome.tabs.query()
的方法如下:chrome.tabs.query({active: true, lastFocusedWindow: true}, tabs => {
let url = tabs[0].url;
// use `url` here inside the callback because it's asynchronous!
});
tabs
或activeTab
权限:"permissions": [ ...
"tabs"
]
或者
"permissions": [ ...
"activeTab"
]
其他答案认为你想要从弹出窗口或后台脚本中了解它。
如果您想要从内容脚本中了解当前URL,则适用标准JS方法:
window.location.toString()
您可以使用window.location
的属性来访问URL的各个部分,例如host、protocol或path。
window.location.host
来查看我是否在 "stackoverflow.com" 上。 - Salyangozmatch:"<all_urls>"
,但 Chrome 不建议使用 <all_urls>。 - mcancontentScript.js
中运行此代码,而不是在background.js
中运行。只需将您从URL中过滤出的任何数据传递到消息中以发送到background.js
。例如:
let message = { type: "fetchVideoDate", id: getVideoId() };
其中getVideoId()
包含对window.location.toString()
的执行。
否则,您将获得一些chrome://temp_background_file.html
数据。
还要注意,有时将message
称为request
。 - DavidHulsman警告!chrome.tabs.getSelected
已经被弃用,请使用其他答案中展示的chrome.tabs.query
。
首先,您需要在manifest.json中设置API权限:
"permissions": [
"tabs"
]
用于存储URL:
chrome.tabs.getSelected(null,function(tab) {
var tablink = tab.url;
});
main.js
中获取当前 URL,有什么指导吗? - Tahir Yasin问题在于chrome.tabs.getSelected是异步的。以下代码通常不会按预期工作。当写入到控制台时,'tablink'的值仍将是未定义的,因为getSelected尚未调用重置该值的回调函数:
var tablink; chrome.tabs.getSelected(null,function(tab) { tablink = tab.url; }); console.log(tablink);
尝试将代码包装在一个函数中,在该函数中使用该值,并由getSelected调用该函数。这样可以确保在执行代码之前始终设置一个值,因为您的代码必须等待提供该值。尝试如下所示:
chrome.tabs.getSelected(null, function(tab) {
myFunction(tab.url);
});
function myFunction(tablink) {
// do stuff here
console.log(tablink);
}
这是一种非常简单的方法
window.location.toString();
你可能需要在内容脚本中完成这个操作,因为它具有网页上的js文件可以拥有的所有功能以及更多。
你好,这是一份Google Chrome示例,可以将当前页面发送给朋友。其基本思路是获取页面内容(对您来说不是很有趣),然后获取URL。此外,它还是一个良好的工作代码示例,我更喜欢这种方式而不是阅读文档。
可以在这里找到: 发送此页电子邮件
async function getCurrentTabUrl () {
const tabs = await chrome.tabs.query({ active: true })
return tabs[0].url
}
"permissions": ["tabs"]
。"permissions": [ ...
"tabs",
"activeTab",
"<all_urls>"
]
在首次加载时调用函数。https://developer.chrome.com/extensions/tabs#event-onActivated
chrome.tabs.onActivated.addListener((activeInfo) => {
sendCurrentUrl()
})
当发生变化时调用函数。https://developer.chrome.com/extensions/tabs#event-onSelectionChanged
chrome.tabs.onSelectionChanged.addListener(() => {
sendCurrentUrl()
})
function sendCurrentUrl() {
chrome.tabs.getSelected(null, function(tab) {
var tablink = tab.url
console.log(tablink)
})
对于那些使用上下文菜单API
的人来说,文档并不立即清楚如何获取选项卡信息。
chrome.contextMenus.onClicked.addListener(function(info, tab) {
console.log(info);
return console.log(tab);
});
你需要检查一下this。
HTML
<button id="saveActionId"> Save </button>
manifest.json
"permissions": [
"activeTab",
"tabs"
]
JavaScript
以下代码将在按钮点击时将当前窗口中所有网址保存到JSON对象中。
var saveActionButton = document.getElementById('saveActionId');
saveActionButton.addEventListener('click', function() {
myArray = [];
chrome.tabs.query({"currentWindow": true}, //{"windowId": targetWindow.id, "index": tabPosition});
function (array_of_Tabs) { //Tab tab
arrayLength = array_of_Tabs.length;
//alert(arrayLength);
for (var i = 0; i < arrayLength; i++) {
myArray.push(array_of_Tabs[i].url);
}
obj = JSON.parse(JSON.stringify(myArray));
});
}, false);