扩展找不到类

时间:2013-02-26 16:25:21

标签: javascript jquery api last.fm

我正在尝试使用last.fm和amp;进行chrome扩展。 plug.dj API以前一直工作,直到我开始在扩展中嵌入我的脚本,然后sinds然后我无法连接到它们。这是设置脚本:

function Setup(){
    console.log('Setup');
    API.addEventListener(API.DJ_ADVANCE, callback);
    cache = new LastFMCache();
    lastfm = new LastFM({
        apiKey: '<key>',
        apiSecret: '<secret>',
        cache: cache
    });
}

我的manifest.json

中有以下内容
{
   "content_scripts": [ {
      "js": [ "jquery.js","lastfm.api.md5.js", "lastfm.api.cache.js", "lastfm.api.js","lastFMLink.js", "script.js"],
      "css": [ "LastFMLink.css" ],
      "matches": [ "http://plug.dj/*", "http://plug.dj/*/*" ],
      "run_at": "document_end"
   } ],

   "name": "Plug.Dj VS last.Fm",
   "description": "Implement information about the artist",
   "icons": { "16": "cookie.png", "48": "cookie.png", "128": "cookie.png" },
   "permissions": [ "http://plug.dj/*", "http://plug.dj/*/*" ],
   "version": "0.0.1",
   "web_accessible_resources": [ "lastFMLink.js"],
   "manifest_version": 2
}

新的LastFMCache()和我访问其他API的脚本中的其他位置出现错误。其他脚本被加载(如lastFMLink.js和lastFMLink.css),而堰是事件监听器确实有效

按下按钮时,安装脚本会被加载并且尚未初始化,所以通常不会因为脚本顺序错误。

任何人都有任何可能出错的线索?

1 个答案:

答案 0 :(得分:0)

这可能不是最好的选择,但我发现它不起作用的原因是因为content_script在后台运行,并且无法相互访问。

所以我将manifest.json改为:

{
   "content_scripts": [ {
      "js": ["script.js"],
      "css": [ "LastFMLink.css" ],
      "matches": [ "http://plug.dj/*", "http://plug.dj/*/*" ],
      "run_at": "document_end"
   } ],

   "name": "Plug.Dj VS last.Fm",
   "description": "Implement information about the artist",
   "icons": { "16": "cookie.png", "48": "cookie.png", "128": "cookie.png" },
   "permissions": [ "http://plug.dj/*", "http://plug.dj/*/*" ],
   "version": "0.0.1",
   "web_accessible_resources": [ "jquery.js","lastfm.api.md5.js", "lastfm.api.cache.js", "lastfm.api.js","lastFMLink.js","script.js"],
   "manifest_version": 2
}

然后我只需要加载脚本,我确定这不是这样做的方法,但它现在可以工作,但现在我加载我的last.fm脚本(在脚本中)。 JS):

var s = document.createElement('script');
s.src = chrome.extension.getURL("lastFMLink.js");
var s2 = document.createElement('script');
s2.src = chrome.extension.getURL("lastfm.api.md5.js");
var s3 = document.createElement('script');
s3.src = chrome.extension.getURL("lastfm.api.cache.js");
var s4 = document.createElement('script');
s4.src = chrome.extension.getURL("lastfm.api.js");

s.onload = function() {
    this.parentNode.removeChild(this);
};
(document.head||document.documentElement).appendChild(s);
(document.head||document.documentElement).appendChild(s2);
(document.head||document.documentElement).appendChild(s3);
(document.head||document.documentElement).appendChild(s4);
相关问题