Google chrome extension 未捕获错误:在mousemove事件中调用chrome.storage.local.set时扩展上下文无效

Google chrome extension 未捕获错误:在mousemove事件中调用chrome.storage.local.set时扩展上下文无效,google-chrome-extension,Google Chrome Extension,你能再帮我解决这个问题吗?我收到错误Uncaught错误:扩展上下文无效。inchrome://extensions. 我正在尝试制作一个计时器,每当你空闲的时候,比如说5秒,警报就会触发,你需要移动鼠标来停止警报,我需要将它与其他打开的选项卡同步,这就是为什么我使用chrome.storage.onChanged.addListener,根据wOxxOm的建议,谢谢。它工作得非常好,但我得到了这个错误 *manifest.json* { "manifest_version": 2,

你能再帮我解决这个问题吗?我收到错误
Uncaught错误:扩展上下文无效。
inchrome://extensions.

我正在尝试制作一个计时器,每当你空闲的时候,比如说5秒,警报就会触发,你需要移动鼠标来停止警报,我需要将它与其他打开的选项卡同步,这就是为什么我使用
chrome.storage.onChanged.addListener
,根据wOxxOm的建议,谢谢。它工作得非常好,但我得到了这个错误

*manifest.json*

{
  "manifest_version": 2,

  "name": "Toool",
  "description": "Description ....",
  "version": "1.0.0",
  "icons": { "128": "icon_128.png" },
  "permissions": ["activeTab", "storage"],
  "content_scripts": [
    {
      "matches": [
        "*://*.google.com/*"
      ],
      "css":[
          "bootstrap/css/bootstrap-iso.css",
          "css/animate.min.css", 
        "css/all.css",
        "css/style.css"
      ],
      "js": [
          "js/jquery.min.js",
          "js/contentscript.js"
       ]
    }
  ],
  "web_accessible_resources": [
    "index.html",
    "audio/*",
    "webfonts/*"
  ]
}

*contentscript.js*
//这就是我到目前为止一直在做的。。
//我没有弹出窗口,而是使用它将html直接注入DOM。
$('body')。前缀(“”);
$.get(chrome.extension.getURL('index.html'),函数(数据){
$(数据).appendTo('.idletimer元素');
});
var audio_url=chrome.runtime.getURL('audio/bells.mp3');
var audio=新音频(音频url);
var-timerStartIn=6;
变量计时器=-1;
var idle=null;
var超时=null;
var MouseMoved=0;
//初始化鼠标存储值
chrome.storage.local.set({“mouseStorage”:MouseMoved},function(){
log('Mouse Moved:'+MouseMoved);
});
$(document).on('mousemove',function(){
//我在这里使用验证只是在必要时执行它,但问题根本没有解决:(
clearTimeout(超时);
如果(MouseMoved==0){
MouseMoved=1;
chrome.storage.local.set({“mouseStorage”:MouseMoved},function(){
log('Mouse Moved:'+MouseMoved);
});
}
timeOut=setTimeout(函数(){
MouseMoved=0;
chrome.storage.local.set({“mouseStorage”:MouseMoved},function(){
log('Mouse Moved:'+MouseMoved);
});
}, 1000);
});
chrome.storage.onChanged.addListener(函数(更改,命名空间){
if(更改['mouseStorage']){
它();
}
});
函数IT(){
清除间隔(空闲);
定时器=-1;
audio.pause();//停止播放
audio.currentTime=0;//重置时间
timerStartIn=6;
$('.timer').html('用户处于活动状态');
$('.timer content i').removeClass('animated shake infinite');
$('.it status').html('审阅者状态:Active');
空闲=设置间隔(函数(){
定时器++;
//控制台日志(计时器);
timerStartIn--;
$('.timer').html('用户处于活动状态。
空闲从'+timerStartIn+'second/s'开始'); 如果(计时器>=5){ //闲散 //console.log('Idle'); $('.timer').html('您空闲了大约
'+secondsToHms(timer-5)+'); //控制台日志(计时器); 音频播放(); $('.timer content i').addClass('animated shake infinite'); $('.it status').html('审阅者状态:Idle'); } }, 1000); }
“上下文无效”表示您已在
chrome://extensions
page,但没有重新加载web选项卡。我还好吗?只是我总是看到这个错误。再次感谢@wOxxOmI的响应。我想你经常重新加载扩展。当你这样做时,你需要重新加载所有匹配的选项卡。谢谢你uuu@wOxxOm“上下文无效”表示您已在
chrome://extensions
page,但没有重新加载web选项卡。我还好吗?只是我总是看到这个错误。再次感谢@wOxxOmI的响应。我想你经常重新加载扩展。当你这样做时,你需要重新加载所有匹配的选项卡。谢谢你uuu@wOxxOm
*contentscript.js*


// So here is what I've been working so far..
//Instead of popup, I am using this to inject html directly to the DOM.

$('body').prepend('<div class="idletimer-element bootstrap" style="display: block"></div>');

$.get(chrome.extension.getURL('index.html'), function(data) {
    $(data).appendTo('.idletimer-element');
});


    var audio_url = chrome.runtime.getURL('audio/bells.mp3');
    var audio = new Audio(audio_url);

    var timerStartIn = 6;
    var timer = -1;
    var idle = null;
    var timeOut = null;
    var MouseMoved = 0;

    //Initialize the mouse storage value
    chrome.storage.local.set({"mouseStorage": MouseMoved}, function() {
        console.log('Mouse Moved: ' + MouseMoved);
    });


    $(document).on('mousemove', function(){

 // I used validation here to just execute it when necessary but the problem didn't fix it at all :(

        clearTimeout(timeOut);

        if(MouseMoved == 0){
            MouseMoved = 1;

            chrome.storage.local.set({"mouseStorage": MouseMoved}, function() {
                console.log('Mouse Moved: ' + MouseMoved);
            });
        }

        timeOut = setTimeout(function(){

            MouseMoved = 0;
            chrome.storage.local.set({"mouseStorage": MouseMoved}, function() {
                console.log('Mouse Moved: ' + MouseMoved);
            });

        }, 1000);

    });

    chrome.storage.onChanged.addListener(function(changes, namespace) {

        if(changes['mouseStorage']){
             IT();
        }
    });

    function IT(){

        clearInterval(idle);
        timer = -1;

        audio.pause(); // Stop playing
        audio.currentTime = 0; // Reset time

        timerStartIn = 6;
        $('.timer').html('User is active');
        $('.timer-content i').removeClass('animated shake infinite');
        $('.it-status').html('<span> <strong>Reviewer Status: </strong> <span class="active m-r-10">Active</span></span>');

        idle = setInterval(function(){

            timer++;
            //console.log(timer);

             timerStartIn--;
            $('.timer').html('User is active. <br> Idle starts in ' + timerStartIn + ' second/s');

            if(timer >= 5){
                //Idle
                //console.log('Idle');

                $('.timer').html('You are idle for about <br> ' + secondsToHms(timer - 5) +'.');
                //console.log(timer);
                audio.play();
                $('.timer-content i').addClass('animated shake infinite');
                $('.it-status').html('<span> <strong>Reviewer Status: </strong> <span class="idle m-r-10">Idle</span></span>');

            }

        }, 1000);
    }