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 m
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.