Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use jQuery from jquery.com's CDN into a Chrome Extension

I'd like to have jQuery accessible both in content scripts and in the console (ie, from the web page -- I believe, but am not sure, this is why you use web_accessible_resources).

note: I agree with Zig Mandel below, who states you shouldnt use CDN to load jquery, because it only saves a small amount of space and leaves open the possibility that the CDN might be down. At this point, I just want to know why this doesn't work.

Why doesn't this work:

manifest.json

  "content_scripts": [
    {
  ...
      "js": ["foo.js", "https://code.jquery.com/jquery-1.10.1.min.js", "https://code.jquery.com/jquery-1.10.1.min.map"],
      "run_at": "document_idle",
      "all_frames": true
    }
  ],
  "content_security_policy": "script-src 'self' https://code.jquery.com; object-src 'self'",
  "web_accessible_resources": [ "https://code.jquery.com/jquery-1.10.1.min.js", "https://code.jquery.com/jquery-1.10.1.min.map"],

the error I receive when loading my extension is:

--------------------------- Extension error
--------------------------- Could not load extension from 'C:\Users\[me]\Documents\GitHub\foo'. Could not load
javascript '' for content script.
--------------------------- OK   
---------------------------

and when do I need jQuery (or some custom debug library, etc) in web_accessible_resources versus when in content_scripts ?

Console use answered by ExpertSystem

You must both include a javascript file such as jQuery in the web_accessible_resources and then inject it. Including jQuery in the content_scripts is only for use by the other content scripts in the extension. An example of how to inject the code (whether local or not):

content script

function inject(script) {
    if (script.match(/^http\:\/\//)){
        var ssrc = document.createElement("script");
        ssrc.setAttribute("src", script);
        ssrc.addEventListener('load', function() {
            var ssrc = document.createElement("script");
            ssrc.textContent = "(" + callback.toString() + ")();";
            document.body.appendChild(script);
        }, false);
        document.body.appendChild(script);
    }
    else {
        var s = document.createElement('script');
        s.src = chrome.extension.getURL(script);
        s.onload = function () {
            this.parentNode.removeChild(this);
        };
        (document.head || document.documentElement).appendChild(s);
    }
}

[path_to_javascript, ...].forEach(inject) // put the javascript filename you'd like to inject in this array.
like image 934
roberto tomás Avatar asked Nov 29 '13 14:11

roberto tomás


People also ask

Can we use jQuery in Chrome extension?

JQuery: jQuery isn't necessary, But it will be useful in chrome extension at some points like logging in the URL or etc. First, download a version of jQuery from the jQuery CDN and put it in your extension's folder, and add the name of the minified version of jquery. To load it, add it to manifest.

Can jQuery be accessed via CDN?

Officially there are two ways of using jQuery: CDN Based Version - You can include jQuery library into your HTML code directly from Content Delivery Network (CDN). Local Installation - You can download jQuery library on your local machine and include it in your HTML code.

What is jQuery CDN used for?

jQuery CDN is a quick and concise JavaScript library that simplifies event handling, animation, AJAX interactions, and HTML document traversing for fast website development. jQuery UI CDN simplifies the client-side scripting of HTML; therefore, it simplifies the development of Web 2.0 applications.

Does jQuery work in chrome console?

noConflict(); It may take a bit of time for jQuery to load. But, once jQuery is loaded, you can run your custom jQuery functions in chrome console. There may be some conflicts with other jQuery versions that may have been loaded on the page.


1 Answers

You need to read more about web accesible resources, they are not for that at all. The recommended way is to include it as part of your extension source. The help pages show you how to do that. Now if for some odd reason you really want to import it from cdn, it's possible. You already added the necessary 'self' modify permissions. Now you need to modify the content page html and manually inject the script in the page. Not worth it as you gain nothing except w slightly smaller and slower extension .

like image 55
Zig Mandel Avatar answered Sep 26 '22 02:09

Zig Mandel