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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 04:42:37  来源:igfitidea点击:

How do I use chrome.tabs.onUpdated.addListener?

javascriptgoogle-chrome-extension

提问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, objectand Tabin the functions signature. Also change .onUpdatedto .onActivated

删除integer,objectTab在函数签名中。也.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);
  });
});