Chrome扩展程序:后台页面间隔运行,但没有看到jQuery插件 [英] Chrome Extension: Background page interval runs, but sees no jQuery plugin

查看:216
本文介绍了Chrome扩展程序:后台页面间隔运行,但没有看到jQuery插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个具有背景的chrome扩展,该扩展每秒运行一次,并更新chrome.storage ...,它被称为 artistSetter.js 。这是它:

$ p $ chrome.storage.local.get(function(items){
$ .each($ ('#supplySong> a> span.sidebar-heading'),function(){
if($(this).css('background-color')=='rgb(22,156,217 )'){
chrome.storage.local.set({currentArtist:$(this)[0] .innerText});
}
});
} );

后台页面是这样的:



<$ p $ ($ {code> setInterval(function(){
chrome.tabs.executeScript(null,{file:'js / artistSetter.js'});
},1000);

错误是:


Error for storage.get:ReferenceError:$ is not defined


事情是这样的在页面刷新后。我明白,这是有效的,因为我 jQuery.js 开始运行。但是当我点击重新加载按钮时,它会停止工作,我开始每秒都会收到错误信息。再次,刷新后,它的作品。因为jQuery.js开始工作。



我该如何解决这个问题?真的找不到方法。

解决方案

为什么不把 setInterval 逻辑内容脚本,只保留artistSetter.js和删除后台页面?



Manifest.json



<$ p $ content_scripts:[
{
matches:[
*:// * / *

js:[
jquery.js,
artistSetter.js
],
run_at:document_end,
all_frames: true


code
$ b

artistSetter.js

  setInterval(function(){
chrome.storage.local.get(function(items){
$ .each($ ('#supplySong> a> span.sidebar-heading'),function(){
if($(this).css('background-color')=='rgb(22,156,217 )'){
chrome.storage.local.set({currentArtist:$(this)[0] .innerText});
}
});
} );
},1000);


I am creating a chrome extension that has background, which runs every second and updates chrome.storage... and it is called artistSetter.js. Here it is:

chrome.storage.local.get(function(items){
    $.each($('#supplySong > a > span.sidebar-heading'), function(){
        if ($(this).css('background-color') == 'rgb(22, 156, 217)'){
            chrome.storage.local.set({"currentArtist": $(this)[0].innerText});
        }
    });
});

Background page is this:

setInterval(function () {
    chrome.tabs.executeScript(null, {file: 'js/artistSetter.js'});
}, 1000);

The error is this:

Error in response to storage.get: ReferenceError: $ is not defined

The thing is that this is working after page refreshes. I understand, that this works because I jQuery.js starts running. But when I click Reload button, it stops working and I start getting the error every second. Again, after refreshing, it works. Because jQuery.js starts working.

How can I fix this? Really couldn't find a way.

解决方案

Why not put the setInterval logic to content scripts, keep artistSetter.js only and remove background page?

Manifest.json

"content_scripts": [
    {
      "matches": [
        "*://*/*"
      ],
      "js": [
        "jquery.js",
        "artistSetter.js"
      ],
      "run_at": "document_end",
      "all_frames": true
    }
  ],

artistSetter.js

setInterval(function () {
    chrome.storage.local.get(function(items){
        $.each($('#supplySong > a > span.sidebar-heading'), function(){
            if ($(this).css('background-color') == 'rgb(22, 156, 217)'){
                chrome.storage.local.set({"currentArtist": $(this)[0].innerText});
            }
        });
    });
}, 1000);

这篇关于Chrome扩展程序:后台页面间隔运行,但没有看到jQuery插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆