Components.utils.importGlobalProperties 编辑

Imports various objects into a system scope.

System scopes such as JSMs and frame scripts don't have certain objects, such as indexedDB and XMLHttpRequest, that are available to DOM window globals. Using this API you can import these objects into such a system scope.

This function is not intended for sandboxes but for system-privileged scopes. To import these objects into a sandbox, use the wantGlobalProperties option in the Sandbox constructor.

This function is passed an array of strings. Each string is the name of an object to import, and will be defined on the importing scope's global. The following strings are supported:

String/ObjectXPCOM Component
atob 
Blob 
btoa 
crypto 
CSS 
fetch 
FilensIDOMFile
indexedDB 
NodeFilter Firefox 60nsIDOMNodeFilter Obsolete since Gecko 60
rtcIdentityProvider 
TextDecoder 
TextEncoder 
URL 
URLSearchParams 
XMLHttpRequestnsIXMLHttpRequest Obsolete since Gecko 60

For string/object in table without a minimum firefox version, it is not exactly known since when it was available, however it is guranteed available from Firefox 28 and up.

Syntax

void Components.utils.importGlobalProperties([string1, string2, ...]);

Parameters

[string1, string2, ...]
An array of strings. Each string is the name of an object to import.

Example

Components.utils.import("resource://gre/modules/devtools/Console.jsm");

Components.utils.importGlobalProperties(["atob", "btoa"]);
var encoded = btoa("Hello");
console.log(encoded);               // "SGVsbG8="
console.log(atob(encoded));         // "Hello"

Alternative Methods

If importGlobalPropertiesdoes not support the targeted Firefox version, here are some alternative methods to import these objects.

Import from JSM

This method worked until Blob and File were no longer apart of JSM modules. It is suspected this works up till Firefox 35.

This imports Blob, File, along with the regular Services. The reason this works is because JS Code Modules actually have Blob and File. Cu.import() will return the full global of a code module. Knowing that, we can just get a valid Blob by importing a known module that has the objects, such as Services.jsm

const {Blob, File, Services} = Cu.import("resource://gre/modules/Services.jsm", {});

Reference: Stackoverflow :: Use Blob on firefox add-on

hiddenDOMWindow

The hidden DOMWindow has all of these objects automatically imported. The downside of using hiddenDOMWindow is that on startup of Firefox, the hiddenDOMWindow objects cannot be accessed until it is fully loaded. Therefore readyState must be checked, if it is not complete, then a load listener must be attached. Once readyState is complete then the objects can be used.

For example this is how to ues the File object.

var domfile = Services.appShell.hiddenDOMWindow.File('/path/to/file');

XPCOM Components

Some objects have an XPCOM alternative, which typically allows more flexibility then the DOM version

Here is an example of how to use the DOM XMLHttpRequest through XPCOM interface of nsIXMLHttpRequest:

var oReq = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.nsIXMLHttpRequest);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:86 次

字数:7336

最后编辑:7 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文