开发了一个chrome插件监控一个通告网页的新通告,在3台电脑上都安装了这个插件,但运行一个月左右两台电脑的插件都不工作了(电脑每日都正常开关机),也不报错,但另一台电脑可以正常运行。
content_scripts代码:
var plaintext
async function print() {
var frame2 = window.frames["main-frame"].frames["DomNotams"].document
var spytext = frame2.evaluate("/html/body/form/div[3]/text()[1]",frame2, null, XPathResult.ANY_TYPE, null).iterateNext();
var spytext2 =spytext.textContent
await readLocalStorage('spytextval')
if( spytext2==plaintext ){
console.log('相同')
}else{
console.log('不同')
var ret = setLocalStorage({ spytextval: spytext2 })
chrome.runtime.sendMessage({greeting: '你好,我是content-script呀,我主动发消息给后台!'}, function() {
console.log('收到来自后台的回复');
});
}
}
const readLocalStorage = async (key) => {
return new Promise((resolve, reject) => {
chrome.storage.local.get([key], function (result) {
if (result[key] === undefined) {
reject();
} else {
resolve(result[key]);
}
});
}).then((result) =>{
console.log(result)
plaintext = result
}).catch(err=>{
})
};
const setLocalStorage = async (kv) => {
return new Promise((resolve, reject) => {
chrome.storage.local.set(kv)
};
setInterval("print()", 15000)
background代码:
function desktop_notification(){
chrome.notifications.create("id", {
type : 'basic',
title : '您有新的通告',
message : '使用须知请点击浏览器右上角黄色标签',
iconUrl : 'img/xx.png'
});
}
function audioNotification(){
var yourSound = new Audio('/video/guonei.mp3');
yourSound.play();
}
chrome.runtime.onMessage.addListener(function (request) {
if(request){
desktop_notification()
audioNotification()
}
});
1.清理过浏览器缓存但仍无效
2.重装过插件,也不行
脚本可以长期稳定运行
看下操作系统日志,报什么错误
三台主机浏览器版本都一样吗?
三个电脑的网络是否不同,监控的是别人的通告网页吗?
如果这两样都符合,有可能是被被监控方给屏蔽了。因为你代码啥的都没变,浏览器也都一样。
两台无法使用的主机在重装chrome后可以使用插件了,但我还是没想明白为啥会出现这样的状况,最一开始我觉得localstorage可能重复存储了长时间运行导致内存空间占满,所以清理了插件存储但还是不行。
先在chrome拓展程序管理里面点一下错误,看下具体报错
f12控制台一条条命令输进去调试
既然用不了就换个浏览器,双核浏览器也很好用的 https://blog.csdn.net/qq_37215621/article/details/126684315