我写了一件代码,每2秒钟后就提醒TAB URL。但是,我无法为弹出窗口这样做。每当我打开弹出窗口时;标签URL是背景页面,而不是T ...

问题描述 投票:0回答:3
<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
而不是
javascript google-chrome google-chrome-extension popupwindow
3个回答
1
投票

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);
   });
};

0
投票

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


0
投票

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.