chrome.windows.getAll() 等待迭代和 sendResponse
chrome.windows.getAll() wait iterate and sendResponse
当用户点击按钮时,这将发送一条消息
// content script
document.querySelector('#library').addEventListener('click', function (event) {
chrome.runtime.sendMessage({type: "check if print"}, (response) => {
console.log(response)
});
})
// background
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
chrome.windows.getAll({}, function (windows_list) {
windows_list.forEach(function (window) {
if (window.type === "popup") {
// push array or sendResponse
}
})
})
})
我想检查是否有弹出窗口,如果有弹出窗口,请向内容脚本发送消息。
但是在控制台(内容)上它 return 一个错误
我曾尝试填充一个数组,当在后台接收消息时启动一个数组并 windows.getAll
推送数组,然后检查我的数组是否有值发送响应。当然它会在 getAll 完成之前发送消息。
Unchecked runtime.lastError: The message port closed before a response was received.
在文档上它说只能与 queryParams 一起使用,这将 return 一个承诺,但我只传递 queryParams,我得到错误
// chrome documentation
getAll(queryOptions?: QueryOptions): Promise<object>
getAll(queryOptions?: QueryOptions, callback: function): void
// Error when I don't write callback
Uncaught TypeError: Error in invocation of tabs.query(object queryInfo, function callback): No matching signature.
谢谢,
你写了
//push array or sendResponse
sendResponse 只能用于发送响应一次。
sendMessage 文档说:
Sends a single message to event listeners within your extension/app or
a different extension/app. Similar to runtime.connect but only sends a
single message, with an optional response.
如果你想在 chrome.windows.getAll 迭代后只发送一个响应,你必须添加 return true 作为文档在这里说:https://developer.chrome.com/docs/extensions/reference/runtime/#event-onMessage
chrome.windows.getAll({}, function (windows_list) {
windows_list.forEach(function (window) {
if (window.type === "popup") {
[...]
}
})
//sendResponse
return true
})
如果您的场景预见到多个弹出窗口 windows,那么您应该考虑
另一种方式 long-lived connections
当用户点击按钮时,这将发送一条消息
// content script
document.querySelector('#library').addEventListener('click', function (event) {
chrome.runtime.sendMessage({type: "check if print"}, (response) => {
console.log(response)
});
})
// background
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
chrome.windows.getAll({}, function (windows_list) {
windows_list.forEach(function (window) {
if (window.type === "popup") {
// push array or sendResponse
}
})
})
})
我想检查是否有弹出窗口,如果有弹出窗口,请向内容脚本发送消息。 但是在控制台(内容)上它 return 一个错误
我曾尝试填充一个数组,当在后台接收消息时启动一个数组并 windows.getAll
推送数组,然后检查我的数组是否有值发送响应。当然它会在 getAll 完成之前发送消息。
Unchecked runtime.lastError: The message port closed before a response was received.
在文档上它说只能与 queryParams 一起使用,这将 return 一个承诺,但我只传递 queryParams,我得到错误
// chrome documentation
getAll(queryOptions?: QueryOptions): Promise<object>
getAll(queryOptions?: QueryOptions, callback: function): void
// Error when I don't write callback
Uncaught TypeError: Error in invocation of tabs.query(object queryInfo, function callback): No matching signature.
谢谢,
你写了
//push array or sendResponse
sendResponse 只能用于发送响应一次。
sendMessage 文档说:
Sends a single message to event listeners within your extension/app or a different extension/app. Similar to runtime.connect but only sends a single message, with an optional response.
如果你想在 chrome.windows.getAll 迭代后只发送一个响应,你必须添加 return true 作为文档在这里说:https://developer.chrome.com/docs/extensions/reference/runtime/#event-onMessage
chrome.windows.getAll({}, function (windows_list) {
windows_list.forEach(function (window) {
if (window.type === "popup") {
[...]
}
})
//sendResponse
return true
})
如果您的场景预见到多个弹出窗口 windows,那么您应该考虑 另一种方式 long-lived connections