我正在尝试在javascript模块jquery.jsm中添加jQuery库。 我在其他模块中使用jquery.jsm。 jQuery需要一个window对象,所以我不能使用原始的jQuery代码。我需要定义一个窗口对象。
jquery.jsm:
const EXPORTED_SYMBOLS = ['jQuery'];
var xjQuery = null;
var window = null;
var location = null;
var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator);
var enumerator = wm.getEnumerator(null);
if (enumerator.hasMoreElements()) {
var win = enumerator.getNext();
window = win;
location = win.location;
}
如何在其他模块中使用原始jQuery文件? 我受到了这篇文章的指导 - https://forum.jquery.com/topic/jquery-1-4-2-inside-a-firefox-extension
答案 0 :(得分:1)
我的解决方案:
jquery.jsm
"use strict";
/**
* Wrapper module to load jQuery library
*/
const EXPORTED_SYMBOLS = ['jQuery'];
// Window object
let scope = {
window: null,
document: null
};
// Scope object (required for jQuery)
let wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator);
let enumerator = wm.getEnumerator(null);
if (enumerator.hasMoreElements()) {
let win = enumerator.getNext();
scope.window = win;
scope.document = win.document;
}
// Add jQuery original library
let url = "chrome://esign/content/script/jquery-3.3.1.min.js";
let loader = Components.classes["@mozilla.org/moz/jssubscript-loader;1"]
.getService(Components.interfaces.mozIJSSubScriptLoader);
loader.loadSubScript(url, scope);
let jQuery = scope.window.jQuery;