XUL の script タグで読み込む

<script type="application/javascript" src="sample.js"/>

Sub-Script Loader で読み込む

Cu.import("resource://gre/modules/Services.jsm");
var tmp = {};
Services.scriptloader.loadSubScript("chrome://sample/content/sample.js", tmp, "UTF-8");
var sample_function = tmp.sample_function;
var sample_array = tmp.sample_array; 
loadSubScript の第2、第3引数は省略可能。

サンドボックスを使用して読み込む

例としてローカルファイルをサンドボックスで読み込んでみる。
Cu.import("resource://gre/modules/osfile.jsm");
const filePath = "C:\\temp\\sample.js";
let promise = OS.File.read(filePath, { encoding: "utf-8" });
promise.then(text => {
    var sandbox = new Cu.Sandbox(this, {wantComponents:false});
    try {
        Cu.evalInSandbox(text, sandbox);
    } catch (e) {}
    var sample_function = sandbox.sample_function;
    var sample_array = sandbox.sample_array;
}); 

タグ:

+ タグ編集
  • タグ:

このサイトはreCAPTCHAによって保護されており、Googleの プライバシーポリシー利用規約 が適用されます。

最終更新:2014年11月16日 16:00