Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple JS files in Chrome Extension - how to load them?

I've wrote a Chrome Extension. My background.js file is quite large, so I want to split it to smaller parts and load specified methods when required (some kind of lazy-loading).

I've done this with Firefox:

// ( call for load specified lib ) var libPath = redExt.basePath + 'content/redExt/lib/' + redExt.browser + '/' + libName + '.js'; var service = Components.classes["@mozilla.org/moz/jssubscript-loader;1"].getService(Components.interfaces.mozIJSSubScriptLoader); service.loadSubScript("chrome://" + libPath); // ( executing loaded file ) 

Is there any possiblity to do it similar way in Webkit-based browsers? I've found solutions for how to inject multiple JS files into matching pages (using manifest.json) but cannot find way to include JS file just for extension.

like image 695
Tomasz Banasiak Avatar asked Sep 06 '13 06:09

Tomasz Banasiak


People also ask

Can you have multiple JS files?

You can write your JS in separate files, but when it comes to deploying, it's more efficient to minify them all into a single file. For each script you load in your browser, you make a round-trip to the server, so it makes sense to minimize those.

How load all JavaScript file in HTML?

To include an external JavaScript file, we can use the script tag with the attribute src . You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file. This script tag should be included between the <head> tags in your HTML document.

How do I link two JavaScript files?

Answer: Use the export and import Statement Since ECMAScript 6 (or ES6) you can use the export or import statement in a JavaScript file to export or import variables, functions, classes or any other entity to/from other JS files.

How to create your first Chrome extension?

Go to chrome://extensions/ and enable the developer mode. Then, click on the Load unpacked button and choose the directory where you have placed the files. And that’s it! You have successfully created your first chrome extension. Clicking on the extension icon will pop up the contents of popup.html.

What is an extension in chrome?

The concept was originally introduced with the initial launch of Chrome, providing isolation for browser tabs. An extension may run in a web page with code similar to the example below. alert(greeting + button.person_name + ".")

How do I access the URL of a Chrome extension?

They can also access the URL of an extension's file with chrome.runtime.getURL () and use the result the same as other URLs. Additionally, content scripts can access the following chrome APIs directly: Content scripts are unable to access other APIs directly.

How do I set up Chrome extensions to read files?

These files may contain data or configuration information to help the extension function. This short guide will show you how you can set up your Chrome extension to read files. Firstly, you must add the file paths to the web_accessible_resources property in the manifest.json file.


Video Answer


2 Answers

UPDATE : This solution works only if you are using manifest V2.

You can also do this the extremely easy way that is described here: https://developer.chrome.com/extensions/background_pages#manifest

{   "name": "My extension",   ...   "background": {     "scripts": [       "lib/fileone.js",       "lib/filetwo.js",       "background.js"     ]   },   ... } 

You won't be doing lazy loading, but it does allow you to break your code up into multiple files and specify the order in which they are loaded onto the background page.

like image 79
Omn Avatar answered Sep 21 '22 07:09

Omn


If you want to load a javascript file in the context of your background page and want to avoid using eval, you can just add a script tag to your background page's DOM. For instance, this works if your files are present in the lib folder of your extension:

function loadScript(scriptName, callback) {     var scriptEl = document.createElement('script');     scriptEl.src = chrome.extension.getURL('lib/' + scriptName + '.js');     scriptEl.addEventListener('load', callback, false);     document.head.appendChild(scriptEl); } 
like image 34
rsanchez Avatar answered Sep 23 '22 07:09

rsanchez