Javascript 如何使用 chrome.tabs.onUpdated.addListener?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11156479/
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
How do I use chrome.tabs.onUpdated.addListener?
提问by Haider
I am creating an extension for Chrome. I want to show an alert() with the page URL whenever the user moves from one tab to another, or when the user enters a new URL in a tab.
我正在为 Chrome 创建一个扩展程序。每当用户从一个选项卡移动到另一个选项卡时,或者当用户在选项卡中输入新 URL 时,我都想显示带有页面 URL 的 alert()。
This is not working:
这不起作用:
chrome.tabs.onUpdated.addListener(function(integer tabId, object changeInfo, Tab tab) {
alert(changeInfo.url);
});
chrome.tabs.onActivated.addListener(function(object activeInfo) {
// also please post how to fetch tab url using activeInfo.tabid
});
回答by user278064
Remove integer
, object
and Tab
in the functions signature. Also change .onUpdated
to .onActivated
删除integer
,object
并Tab
在函数签名中。也.onUpdated
改为.onActivated
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
alert(changeInfo.url);
});
chrome.tabs.onActivated.addListener(function(activeInfo) {
// how to fetch tab url using activeInfo.tabid
chrome.tabs.get(activeInfo.tabId, function(tab){
console.log(tab.url);
});
});