1

I have a chrome extension that needs jquery loaded as a content script for each site the user is browsing.

Problem is that some sites already use jquery (a very big majority of them).

I don't want my jquery (from the extension) enter in conflict with the existing jquery (used by the site).

What is the best way to avoid this ?

I was thinking to modify the jquery I use and wrap the code in a object. Is there another option ?

Andrei Ciobanu
  • 12,500
  • 24
  • 85
  • 118
  • Have you checked the `noConflict` function from jquery ? http://api.jquery.com/jquery.noconflict/ – Kirween Nov 20 '14 at 14:14
  • @Kirween unfortunately this doesn't help. My jquery is loaded no matter what. This is how chrome extensions works. I wanted to know more if there is a hidden feature in the chome api. – Andrei Ciobanu Nov 20 '14 at 14:19

1 Answers1

0

Can you try the following:

if (window.jQuery) {  
    /* Use the existing jquery library already used by the site */  
} else {
    /* Load your jQuery here */

var script = document.createElement("SCRIPT");
script.src = 'jquery.min.js';
script.type = 'text/javascript';
document.getElementsByTagName("head")[0].appendChild(script);

}
ArinCool
  • 1,720
  • 1
  • 13
  • 24
  • I doesn't really work like that. The jquery I am injecting is local and shpuld be included in the manifest.json of the chrome extension. – Andrei Ciobanu Nov 20 '14 at 14:08
  • 1
    This is an old answer, but the suggested solution doesn't work because of sandboxing for Chrome extensions. You can't dynamically create script blocks. You can insert a as long as it's just text and not running from JS. – Scott Gartner Feb 02 '17 at 07:26
  • Google doesn't give me any details, but my extension was taken down from chrome store repeatedly until I took out the conditional jQuery injection to page and included it via manifest instead. Problem is the same though, conflicts with some sites jQuery's even if using .noConflict. For example, jQuery deprecated .size() and using it can break the app, so some sites have versions that have that, some don't, etc. and there are other issues as well that will prevent Gmail or UpWork etc from ever loading, probably because they have extended $ or jQuery in some ways that get overwritten by mine.. – OG Sean Sep 17 '19 at 18:10