gpt4 book ai didi

javascript - chrome.webrequest.onCompleted 与 chrome.runtime.onMessage 竞赛

转载 作者:行者123 更新时间:2023-11-28 00:21:05 25 4
gpt4 key购买 nike

我有一个带有 background.js 的 Chrome 扩展程序,其中收集和存储有关页面的信息,

chrome.webRequest.onCompleted.addListener(
function(details) {
// compute a page hash etc, store it
tabToHash[details.tabId] = hash;
},
{
urls: ['*://*/*.pdf'],
types: ['main_frame']
}
);

并提供给需要它的扩展程序的任何其他部分(例如,内容脚本),

chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.getInfo) {
sendResponse({
hash: tabToHash[sender.tab.id],
});
}
});

现在,在 onCompleted 完成之前触发 onMessage 时会出现问题。

如何避免这种竞争条件?

<小时/>

编辑:发送初始消息的内容脚本可能包含

chrome.runtime.sendMessage(
{getInfo: true},
function(response) {
// do something with the response
}
);

并且可以插入到manifest.json中作为

"content_scripts": [
{
"matches": ["*://*/*.pdf"],
"css": ["content.css"],
"js": ["scripts/content.js"]
}
]

最佳答案

  • 通过添加跟踪内容的加载状态
chrome.webRequest.onBeforeRequest.addListener(
function(details) {
tabStatus[details.tabId] = 'loading';
},
{
urls: ['*://*/*.pdf'],
types: ['main_frame']
}
);

tabStatus[details.tabId] = 'complete';

chrome.webRequest.onCompleted.addListener 末尾

  • 有条件地延迟 onMessage 监听器中的响应:
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.getInfo) {
if (tabStatus[sender.tab.id] === 'complete') {
// send immediately
sendResponse({
data: myData[sender.tab.id]
});
} else {
// send later
responseSender[sender.tab.id] = sendResponse;
// returning `true` to indicate that we intend to send later,
// cf. <https://developer.chrome.com/extensions/runtime#event-onMessage>
return true;
}
}
});
  • chrome.webRequest.onCompleted.addListener 末尾调用存储的 responseSender[sender.tab.id]:
if (responseSender[details.tabId]) {
responseSender[details.tabId]({
data: myData(details.tabId)
});
responseSender[details.tabId] = null;
}

关于javascript - chrome.webrequest.onCompleted 与 chrome.runtime.onMessage 竞赛,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30018302/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com