<script>
var seconds = 2*1000;
setInterval(function(){
chrome.tabs.getSelected(null, function(tab) {
tabId = tab.id;
tabUrl = tab.url;
alert(tabUrl);
});
},seconds);
</script>
</head>
当您通过
null
而不是chrome.tabs.getSelected()
时,它默认为“当前”窗口,这不是必需的,如所解释的HERHE:
:
当前窗口是包含当前执行的代码的窗口。重要的是要意识到这可能与最上方或集中的窗口不同。 因此,您需要首先找到集中的窗口,然后获取其选定的选项卡:
var seconds = 2*1000; setInterval(function(){ chrome.windows.getLastFocused(function(window) { chrome.tabs.getSelected(window.id, function(tab) { tabId = tab.id; tabUrl = tab.url; alert(tabUrl); }); }); },seconds);
InContent_Script.js或popup.html:
function get_urlInfo() {
var d = {
'action' : 'getUrl'
};
chrome.extension.sendRequest(d, function(response) {
alert(response.url);
});
};
function onRequest(request, sender, sendResponse) {
if (request.action == 'getUrl') {
sendResponse({'url' : sender.tab.url});
}
};
chrome.extension.onRequest.addListener(onRequest);
应该起作用!
也是 - 设置Chrome Developer工具以自动打开弹出窗口,似乎是一个选项,请参阅Https://stackoverflow.com/a/65639084/10061651