我最近有报道说,我开发了一个铬扩展,在更新或重新安装后停止工作。背景脚本似乎根本就没有开始。
对于从内容脚本发送给它的消息没有响应。在任务manager.
一旦出现此问题,即使在重新加载或卸载/重新安装扩展名之后,它仍将用于铬配置文件。
重新启动铬解决了问题。
这个问题已经在铬v79上看到了。但我不能肯定地说,这是唯一的版本,因为这个问题是难以复制和似乎是随机的。
有没有人见过这样的问题,或者有什么想法要找?我很高兴更新我的问题与任何新的信息,我有或任何信息,你需要。
编辑:
下面是我的webNavigation
监听器,它用于注入内容脚本。此处理程序连接在后台脚本的“root”上下文中(在事件处理程序中不是异步的)。
chrome.webNavigation.onCompleted.addListener((details) ⇒ {
if(details.frameId === 0) {
injectScript(
'js/contentScript.js',
details.tabId,
details.frameId,
details.url
).catch((e) ⇒ {});
}
}
injectScript
函数如下所示
export const injectScript = ƒ (scriptPath,tab,frame,tabUrl) {
return new Promise((res,rej) ⇒ {
let options = {
file : scriptPath,
allFrames : false,
frameId : frame,
matchAboutBlank: false,
runAt : 'document_idle',
};
const cb = ƒ () {
if (chrome.runtime.lastError) {
let err = new Error('Could not inject script');
capture(err,{
...options,
tabUrl,
lastError : chrome.runtime.lastError.message,
});
rej(err);
}else{
res();
}
};
if (tabUrl.indexOf('.salesforce.com') !== -1) {
window.setTimeout(() => {
chrome.tabs.executeScript(tab,options,cb);
},500);
}else{
chrome.tabs.executeScript(tab,options,cb);
}
});
};
注意,上面的capture
函数将错误报告给后端,我也看不到它在那里被报告。无法在代码中添加断点,因为背景页中没有出现任何源,如前所述。
发布于 2021-09-28 04:55:06
后台服务工作人员在需要时加载,在空闲时卸载。
https://developer.chrome.com/docs/extensions/mv3/service_workers/
您可以使用以下方法:
// Keep heartbeat
let heartTimer;
const keepAlive = () => {
heartTimer && clearTimeout(heartTimer);
heartTimer = setTimeout(() => {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
console.info('[heartbeat]')
tabs.length && chrome.tabs.sendMessage(
tabs[0].id,
{ action: "heartbeat" }
);
});
keepAlive();
}, 10000);
};
keepAlive();
https://stackoverflow.com/questions/59551551
复制相似问题