Google chrome extension 关注/切换到运行内容脚本的选项卡

Google chrome extension 关注/切换到运行内容脚本的选项卡,google-chrome-extension,Google Chrome Extension,我有一个内容脚本,可以在特定时间通过相应地调整window.location.href加载新网站 在加载新URL之前,我想自动切换到运行内容脚本的选项卡 我想我可以使用激活选项卡,但是如何才能获得正确的tabId var updateProperties = {"active": true}; chrome.tabs.update(tabId, updateProperties, function(tab){ }); 在此感谢您的帮助。以下是我解决问题的方法(谢谢您的建议,xOxxOm): 在

我有一个内容脚本,可以在特定时间通过相应地调整
window.location.href
加载新网站

在加载新URL之前,我想自动切换到运行内容脚本的选项卡

我想我可以使用激活选项卡,但是如何才能获得正确的
tabId

var updateProperties = {"active": true};
chrome.tabs.update(tabId, updateProperties, function(tab){ });

在此感谢您的帮助。

以下是我解决问题的方法(谢谢您的建议,xOxxOm):

在后台脚本中:

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse){
  // Set focus on window
  chrome.windows.update(sender.tab.windowId, {"focused": true}, function(window){ });
  // Set focus on tab
  chrome.tabs.update(sender.tab.id, {"active": true}, function(tab){ });
});
在内容脚本中:

chrome.runtime.sendMessage("Do something");

由于chrome.tabs是一个内容脚本,所以只需向后台/事件页面脚本发送一条消息,您就可以在其中使用listener.Thx。我目前没有使用背景/事件页面,但我将包括它并尝试您的方法。