问题遇到的现象和发生背景
开发了一个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.重装过插件,也不行
我想要达到的结果
脚本可以长期稳定运行