0

I've built a chrome extension that will rely upon ajax calls from the background script, but for some reason the ajax function is not registered as a function at the time I make my call. I thought first that it was a problem in my manifest, but realized that it would be throwing an error about $ being undefined if jquery wasn't being injected for use. I'm wondering now if it might be some sort of securities issue? I would think that if that was the case, though, that the function would still execute, but throw an error when the transport to say an illegal URL occurred. I'm really at a loss here. Before trying $.ajax() I was using $.getScript(), but it throws the similar error that getScript is not a function.

In my manifest.json, I have this:

"background": {
    "scripts": ["vendor/jquery.js", "background.js"]
},

And this is in my background.js:

chrome.runtime.onInstalled.addListener(function(){
   ...
   var Core = {
      ...
      load: function(name){
         var src = EXTERNAL+name+".js";
         $.ajax({
            url: src,
            type: "script",
            success: function(result) {
               console.log(result);
            }
         });
      },
   };

   function initialize(){
      ...
      Core.load(name);
   }

   chrome.runtime.onMessage.addListener(function(message, sender, sendResponse){
      var request = message.data;
      var type = request.type;
      switch(type){
         case 'popup_handshake':
            sendResponse({data:{domain: domain, seed: seed}});
         break;

         case 'seed':
            domain = request.domain;
            uW = request.uW;
            seed = uW.seed;
            cm = uW.cm;

            delete uW.seed;
            delete uW.cm;

            initialize();
         break;
      }
   });
});

What could the problem possibly be?

chaoskreator
  • 889
  • 1
  • 17
  • 39
  • Are you loading JQuery? Possibly a race condition? – Action Coding Nov 11 '18 at 22:14
  • I was under the impression that the order in which the scripts are defined in manifest.json determined load order. I can view the generated background_page.html and the scripts are included correctly. – chaoskreator Nov 12 '18 at 00:28
  • A common mistake is to include background.js in other places/pages like in content_scripts and see an error in the web page console. If that's not the case and the error is shown in the [real background page console](https://stackoverflow.com/a/10258029), it looks like a buggy jquery.js. – wOxxOm Nov 12 '18 at 06:40
  • @wOxxOm yeah, I've got it running on the background page, and not injected via content_scripts. I'll see if another copy of jquery will work, even though I just downloaded this one from the CDN. – chaoskreator Nov 12 '18 at 18:13
  • Are you by any chance using the jQuery Slim version? It lacks AJAX-related functionality among other things. – Xan Nov 14 '18 at 14:57
  • No, full version. – chaoskreator Nov 18 '18 at 03:57

1 Answers1

0

[Edit][Tested] I just add:

"background": {
    "scripts": ["jquery.js","background.js"]
    },

I called $.ajax on my background.js, and it looks fine, it returns:

ƒ (t,n){"object"==typeof t&&(n=t,t=void 0),n=n||{};var i,o,a,s,u,l,c,f,p,d,h=w.ajaxSetup({},n),g=h.context||h,y=h.context&&(g.nodeType||g.jquery)?w(g):w.event,v=w.Deferred(),m=w.Callbacks("once memory"… 

Take a look on your jquery.js script....

Demian Bibiano
  • 347
  • 1
  • 10