刷新窗口时获取URL;不适用于Chrome扩展

Getting URL when window refreshed doesn't work in Chrome Extension

本文关键字:不适用 适用于 Chrome 扩展 URL 窗口 获取 刷新      更新时间:2023-09-26

我想在Chrome扩展中刷新网页时获得当前窗口的URL。。以下是我所做的:

我的宣言的一部分:

  "content_scripts": [
{
  "matches": ["http://*/*","https://*/*"],
  "js": ["temp.js"]
}
]

我的temp.js:

        chrome.tabs.getSelected(null, function(tab) 
    {      var tabId = tab.id;
           tabUrl = tab.url;
           alert(tabUrl);
    });

但这行不通。

请帮忙。。我还是个初学者

chrome.tabs在内容脚本中不可用。你应该创建一个这样的背景脚本:

将其包含在您的manifest.js:中

"background": {
  "scripts": ["background.js"]
},
"permissions": [
  "tabs"
]

然后在后台.js:

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
    // This will give you the url if it's changed
    alert(changeInfo.url);
    // Or to always get the tab's url even when it's unchanged
    alert(tab.url);
}); 

更多信息