0

I'm trying to make an chrome extension with the last.fm & plug.dj API's it used to work until I started embedding my scripts in the extension, and sinds then I can't connect to neither of them. This is the setup script:

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

with the following in my 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
}

it errors on the new LastFMCache() and somewhere else in the script where I access the other API. the other scripts get loaded (like lastFMLink.js and lastFMLink.css) and the weir thing is the event listener does work

the setup script get's loaded when pressed on a button and it isn't initialized yet, so normally it isn't erroring due script order.

anyone got any clues what might be going wrong?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Kiwi
  • 2,713
  • 7
  • 44
  • 82

1 Answers1

0

This is probabbly not the best option, but I found out that the reason it didn't work was due the fact that content_script run in the background, and don't have access to eachother.

so I changed my manifest.json to:

{
   "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
}

and then I only had to load the scripts and I'm deffenitly sure this isn't the way to do it, but it works for now, but this is now how I load my last.fm scripts (in script.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);
Kiwi
  • 2,713
  • 7
  • 44
  • 82