十二月309 2022-09-23 10:34 采纳率: 33.3%
浏览 127
已结题

chrome插件无法运行

问题遇到的现象和发生背景

开发了一个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.重装过插件,也不行

我想要达到的结果

脚本可以长期稳定运行

  • 写回答

8条回答 默认 最新

  • H5周 2022-09-23 10:51
    关注

    看下操作系统日志,报什么错误

    评论

报告相同问题?

问题事件

  • 系统已结题 10月1日
  • 赞助了问题酬金10元 9月23日
  • 创建了问题 9月23日

悬赏问题

  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码