Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome Extension | How to include library in content and background scripts from cdn

my Chrome extension has two files: content and background scripts. I need to add jQuery to content script from cdn and lodash to background script from cdn too.

In my manifest I tried to add lodash from cdn like this:

"background": {
      "scripts": ["background.js", "https://cdn.jsdelivr.net/lodash/4.13.1/lodash.min.js"]
  },

  "content_security_policy": "script-src 'self' https://cdn.jsdelivr.net;     object-src 'self'"

But that didn't help.

My content file is injected to the page from the background file:

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
    if (changeInfo.status == 'complete') {
        chrome.tabs.insertCSS(null, {file: "mainStyles.css"});
        chrome.tabs.executeScript(null, {
            code: 'var config = ' + JSON.stringify(config)
        }, function() {
            chrome.tabs.executeScript(null, { file: "https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js" }, function()     {
                chrome.tabs.executeScript(null, { file: "content.js" })
            });
        });
    }
});

And as you can see I tried to include jQuery from cdn but this way it's not included either.

Maybe someone knows the way how to do this? Many thanks in advance!

like image 251
Ms.Smith Avatar asked Jun 03 '16 08:06

Ms.Smith


People also ask

What is the difference between content script and background script?

Background Script - Provides persistence and handles background events. Content Script - Scripts that run in isolation in the context of the web page. Injected Script - Scripts that are programmatically injected into the web page.

How do I add extensions to Chrome library?

Method 1: Use the scripts key of the background key in the manifest file. Inside the value for the scripts key, add an array which contains reference to the library that you require, and the background script. Note that the order inside the array matters. Script listed first will be loaded first.

What is content script Chrome extension?

A content script is a part of your extension that runs in the context of a particular web page (as opposed to background scripts which are part of the extension, or scripts which are part of the website itself, such as those loaded using the <script> element).

How can background scripts & Content scripts communicate?

Communication between extensions and their content scripts works by using message passing. Either side can listen for messages sent from the other end, and respond on the same channel.


1 Answers

You can get your scripts from external resources, but it's not the preferred way as stated here.

Furthermore, you shouldn't use the background tag like this. Source

You're better off downloading and bundling your extension with required libraries and getting it as a content_script.

Example from my manifest.json for an extension running on youtube:

"content_scripts": [ {
    "matches": ["http://*.youtube.com/*", "https://*.youtube.com/*"],
    "js": ["jquery.js", "content.js"]
}],

If you must use some external scripts, I found that doing it using a little hack works the best.

The general idea is that it executes a piece of JS code in the scope of current webpage and adds a <script> tag with your desired script.

This is a snippet from an extension that required some of our proprietary js to be included:

chrome.tabs.executeScript(tab.id, {code:
    "document.body.appendChild(document.createElement('script')).src = 'https://example.com/script.js';"
});

And chrome being such a good guy just executes it.

like image 64
Somrlik Avatar answered Oct 21 '22 19:10

Somrlik