JavaScript code modules
https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules
Non-standard
This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.
Note: These are not the same thing as standard JavaScript modules. See export
and import
to learn more about how to use standard modules.
JavaScript code modules let multiple privileged JavaScript scopes share code. For example, a module could be used by Firefox itself as well as by extensions, in order to avoid code duplication.
Using JavaScript code modules
JavaScript code modules are a concept introduced in Gecko 1.9 and can be used for sharing code between different privileged scopes. Modules can also be used to create global JavaScript singletons that previously required using JavaScript XPCOM objects. A JavaScript code module is simply some JavaScript code located in a registered location. The module is loaded into a specific JavaScript scope, such as XUL script or JavaScript XPCOM script, using Components.utils.import()
or Components.utils["import"]()
.
Creating a JavaScript code moduleSection
A very simple JavaScript module looks like this:
var EXPORTED_SYMBOLS = ["foo", "bar"]; function foo() {
return "foo";
} var bar = {
name : "bar",
size : 3
}; var dummy = "dummy";
Notice that the module uses normal JavaScript to create functions, objects, constants, and any other JavaScript type. The module also defines a special Array named EXPORTED_SYMBOLS
. Any JavaScript item named in EXPORTED_SYMBOLS
will be exported from the module and injected into the importing scope. For example:
Components.utils.import("resource://app/my_module.jsm"); alert(foo()); // displays "foo"
alert(bar.size + 3); // displays "6"
alert(dummy); // displays "dummy is not defined" because 'dummy' was not exported from the module
Note: When you're testing changes to a code module, be sure to change the application's build ID (e.g., the version) before your next test run; otherwise, you may find yourself running the previous version of your module's code.
The URL for a code moduleSection
As you can see from the example above, you need a URL to import a code module. (The URL in the example is "resource://app/my_module.jsm".)
Code modules can only be loaded using a chrome: (), resource:, or file: URL.
- If you're writing an extension for Firefox 4 and already have a chrome.manifest with a
content
instruction in it, you can put the code module in your content folder and reference it like your other content files viachrome://<yourextension>/content/<yourmodule>.jsm.
- If your extension or application needs to support Mozilla 1.9.x (Firefox 3.x), you should register a new resource URL. Details on doing this are in the "Extending resource: URLs" section below.
Sharing objects using code modulesSection
An extremely important behavior of Components.utils.import()
is that modules are cached when loaded and subsequent imports do not reload a new version of the module, but instead use the previously cached version. This means that a given module will be shared when imported multiple times. Any modifications to data, objects, or functions will be available in any scope that has imported the module. For example, if the simple module were imported into two different JavaScript scopes, changes in one scope can be observed in the other scope.
Scope1:
Components.utils.import("resource://app/my_module.jsm"); alert(bar.size + 3); // displays "6" bar.size = 10;
Scope2:
Components.utils.import("resource://app/my_module.jsm"); alert(foo()); // displays "foo"
alert(bar.size + 3); // displays "13"
This sharing behavior can be used to create singleton objects that can share data across windows and between XUL script and XPCOM components.
Note: Each scope that imports a module receives a by-value copy of the exported symbols in that module. Changes to the symbol's value will not propagate to other scopes (though an object's properties will be manipulated by reference).
Scope1:
Components.utils.import("resource://app/my_module.jsm"); bar = "foo";
alert(bar); // displays "foo"
Scope2:
Components.utils.import("resource://app/my_module.jsm"); alert(bar); // displays "[object Object]"
The main effect of the by-value copy is that global variables of simple types won't be shared across scopes. Always put variables in a wrapper class and export the wrapper (such as bar
in the above example).
Importing CommonJS modulesSection
The JavaScript code modules described here are not the same thing as CommonJS modules, but you can import CommonJS modules into any scope where you can use Components.utils.import. Just call the following:
const { require } = Cu.import("resource://gre/modules/commonjs/toolkit/require.js", {})
This will import require()
into your scope.
You can then use that to import CommonJS modules. You can import Add-on SDK modules in just the same way you could from an SDK add-on:
// import the SDK's base64 module var base64 = require("sdk/base64");
base64.encode("hello"); // "aGVsbG8="
You can import other CommonJS modules, too, as long as you know the path to them:
// import my module var myModule = require("resource://path/to/my/module.js");
In this case, though, you might be better off creating your own loader, so you can specify the paths
property yourself.
JavaScript code modules的更多相关文章
- Eloquent JavaScript #10# Modules
索引 Notes 背景问题 模块Modules 软件包Packages 简易模块 Evaluating data as code CommonJS modules ECMAScript modules ...
- Javascript Code Style Guide
本指南采用的Airbnb发布的基于ES5的JavaScript Code Style. ES5 英文版:https://github.com/airbnb/javascript/tree/es5-de ...
- jshint-eclipse: JavaScript Code Quality Plugin for Eclipse
https://blog.oio.de/2012/03/26/jshint-eclipse-javascript-code-quality-plugin-for-eclipse/ techscou ...
- Random Javascript code snippets
MollyPages.org"You were wrong case.To live here is to live." Home Pages / Database / Forms ...
- javascript code snippet -- Forwarding Mouse Events Through Layers
Anyone who has worked with web apps has likely created a masking element at some point, and the grea ...
- javaScript Code 用javascript确定每月第二个星期五
废话少说只就上Code: 说明:getDay()方法获取星期(这里的星期是从0到6).参见:http://www.w3school.com.cn/js/js_obj_date.asp 中的ge ...
- [javascript]Three parts of javascript code snippet
<script> (function(){ /* if (navigator.userAgent.toLowerCase().indexOf("iphone") == ...
- Javascript code for soft keyboard
<style> BODY { SCROLLBAR-FACE-COLOR: #f0f0f6; FONT-SIZE: 9pt; BACKGROUND-ATTACHMENT: f ...
- JavaScript code 性能优化
1 1 1 JavaScript 性能优化 prototype 闭包 Closure 内存泄漏 event system 1 定义类方法以下是低效的,因为每次构建baz.Bar的实例时,都会为foo创 ...
随机推荐
- 补充[BNDSOJ]小p的数列
强烈安利gjz的题解,看一遍即可ac:传送门 进入重点: 为啥$to=(dp[i][k][ii]+dp[k+1][j][jj])/2$ 位运算重点:a&b=a+b-a|b 为啥呢? 例子: a ...
- 提高CUI测试稳定性技术
GUI自动化测试稳定性,最典型的表现形式就是,同样的测试用例在同样的环境上,时而测试通 过,时而测试失败. 这也是影响GUI测试健康发展的一个重要障碍,严重降低了GUI测试的可信性. 五种造成GUI测 ...
- SQL性能优化概要
基本概要 1.查询的模糊匹配时,避免使用Like '%开头',使得索引失效 2.索引问题 ◆ 避免对索引字段进行运算操作和使用函数 ◆ 避免在索引字段上使用not,<>,!= ◆ 避免在索 ...
- IBM公司的面试题,看看你能做出多少。
进入IBM差不多是每一个IT人的梦想.IBM公司向来以高素质人才作为企业持续竞争力的保证,所以经常出一些千奇百怪的面试题,来考验一个人的综合能力,以下是5道IBM曾经出过的面试题,看看你能作出几道: ...
- Android 开发环境部署
引言 在windows安装Android的开发环境不简单也说不上算复杂,本文写给第一次想在自己Windows上建立Android开发环境投入Android浪潮的朋友们,为了确保大家能顺利完成开发环 ...
- Python 中的Lock与RLock
摘要 由于多线程共享进程的资源和地址空间,因此,在对这些公共资源进行操作时,为了防止这些公共资源出现异常的结果,必须考虑线程的同步和互斥问题. 为什么加锁:1.用于非线程安全, 2.控制一段代码,确保 ...
- Auth主件的(RBAC) 六表
1.RBAC 和Auth的区别 基于RBAC一般Djagno 会用 和Auth 相对来说高级一点 2.RBAC( role Based Accsess Control)的六表之间的数据传输 2.1 D ...
- 一、.net Core bundleconfig.json
一.bundleconfig.json [ { "outputFileName": "wwwroot/css/site.min.css", "inpu ...
- pam模块
main 循环监控 独立的程序 根据配置防护 登陆 ca cert 私有口令 openssl 证书口令??
- CTF各种资源:题目、工具、资料
目录 题目汇总 Reverse 签到题 Web Web中等难度 Crypto 基础网站 各类工具 综合 Web Payloads 逆向 Pwn 取证 题目汇总 这里收集了我做过的CTF题目 Rever ...