Chrome 选项卡查询返回空选项卡数组
Chrome tabs query returning empty tab array
我正在尝试将我在 Firefox 中使用的 Web 扩展移植到 chrome,但我遇到了一些问题。我需要将消息从后台脚本发送到内容脚本。当我第一次从 Firefox 构建它时,我正在使用一个端口,但我将其切换为使用 chrome.tabs.query()
,因为 chrome 一直在发现错误。但是现在使用 query()
,它在 Firefox 中仍然可以正常工作,但是现在 chrome 说找不到当前选项卡:
Error handling response: TypeError: Cannot read property 'id' of undefined
at chrome-extension://hhfioopideeaaehgbpehkmjjhghmaaha/DubedAniDL_background.js:169:11
returns tab 参数传递的是长度 == 0。console.log(tabs)
:
[]
这是 Chrome 抱怨的功能。
var browser = chrome; // I set this only for compatibility with chrome; not set in Firefox.
function sendToTab(thing) {
browser.tabs.query(
{active: true, currentWindow: true},
function(tabs) {
console.log(tabs);
browser.tabs.sendMessage(
tabs[0].id, // The inspector identifies an error on this line
thing
);
}
);
}
相同的功能在 Firefox 中运行良好,并且可以毫无问题地访问该选项卡。但它在 Chrome.
中不起作用
更新 2020-01-30
@wOxxOm:
Show the code that calls sendToTab
这是调用 sendToTab 的地方:
function logURL(requestDetails) {
var l = requestDetails.url;
if (l.includes(test_url)) {
if (logOn) { console.log(l); }
sendToTab({dl_url: l});
}
}
browser.webRequest.onBeforeRequest.addListener(
logURL,
{urls: ["<all_urls>"]}
);
听起来你是 运行 这个代码,而你的 window 是 devtools,它是 known bug.
另一个问题出现在您的代码中:它总是访问聚焦(活动)选项卡,即使请求可能发生在后台(非活动)选项卡中。
解法:
使用监听器中提供的选项卡 ID function parameter 作为 tabId
属性.
在你的情况下是 requestDetails.tabId
如果出于某种原因您真的想要活动选项卡,请确保在运行此代码时真正的 window 处于活动状态,或者使用以下解决方法,即使 devtools window 被聚焦也会成功:
chrome.windows.getCurrent(w => {
chrome.tabs.query({active: true, windowId: w.id}, tabs => {
const tabId = tabs[0].id;
// use tabId here...
});
});
我正在尝试将我在 Firefox 中使用的 Web 扩展移植到 chrome,但我遇到了一些问题。我需要将消息从后台脚本发送到内容脚本。当我第一次从 Firefox 构建它时,我正在使用一个端口,但我将其切换为使用 chrome.tabs.query()
,因为 chrome 一直在发现错误。但是现在使用 query()
,它在 Firefox 中仍然可以正常工作,但是现在 chrome 说找不到当前选项卡:
Error handling response: TypeError: Cannot read property 'id' of undefined
at chrome-extension://hhfioopideeaaehgbpehkmjjhghmaaha/DubedAniDL_background.js:169:11
returns tab 参数传递的是长度 == 0。console.log(tabs)
:
[]
这是 Chrome 抱怨的功能。
var browser = chrome; // I set this only for compatibility with chrome; not set in Firefox.
function sendToTab(thing) {
browser.tabs.query(
{active: true, currentWindow: true},
function(tabs) {
console.log(tabs);
browser.tabs.sendMessage(
tabs[0].id, // The inspector identifies an error on this line
thing
);
}
);
}
相同的功能在 Firefox 中运行良好,并且可以毫无问题地访问该选项卡。但它在 Chrome.
中不起作用更新 2020-01-30
@wOxxOm:
Show the code that calls sendToTab
这是调用 sendToTab 的地方:
function logURL(requestDetails) {
var l = requestDetails.url;
if (l.includes(test_url)) {
if (logOn) { console.log(l); }
sendToTab({dl_url: l});
}
}
browser.webRequest.onBeforeRequest.addListener(
logURL,
{urls: ["<all_urls>"]}
);
听起来你是 运行 这个代码,而你的 window 是 devtools,它是 known bug.
另一个问题出现在您的代码中:它总是访问聚焦(活动)选项卡,即使请求可能发生在后台(非活动)选项卡中。
解法:
使用监听器中提供的选项卡 ID function parameter 作为 tabId
属性.
在你的情况下是 requestDetails.tabId
如果出于某种原因您真的想要活动选项卡,请确保在运行此代码时真正的 window 处于活动状态,或者使用以下解决方法,即使 devtools window 被聚焦也会成功:
chrome.windows.getCurrent(w => {
chrome.tabs.query({active: true, windowId: w.id}, tabs => {
const tabId = tabs[0].id;
// use tabId here...
});
});