我如何使用chrome.tabs.onUpdated.addListener?

Hai*_*der 9 javascript google-chrome-extension

我正在为Chrome创建扩展程序.我希望每当用户从一个选项卡移动到另一个选项卡时,或者当用户在选项卡中输入新URL时,都会显示带有页面URL的alert().

这不起作用:

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
});
Run Code Online (Sandbox Code Playgroud)

use*_*064 30

删除integer,objectTab在函数签名中.

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);
  });
}); 
Run Code Online (Sandbox Code Playgroud)

  • 这段代码不再适用吗?我使用它作为popup.js包含在我的popup.html中.也不会在我的后台或内容脚本中工作. (4认同)
  • 对于Rohit来说,这可能为时已晚,但是请确保扩展清单的`permissions`数组中有“ tabs”。否则,您将不会获得标签的URL或标题。 (3认同)