1

I'm trying to add the jQuery library in the javascript module jquery.jsm. I'm using jquery.jsm in other modules. jQuery requires a window object, so I can not use the original jQuery code. I need to define a window object.

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;
}

How use original jQuery file in other modules? I was guided by this post - https://forum.jquery.com/topic/jquery-1-4-2-inside-a-firefox-extension

Vitaliy Demchuk
  • 180
  • 1
  • 1
  • 9

1 Answers1

1

My solution:

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;
Vitaliy Demchuk
  • 180
  • 1
  • 1
  • 9
  • +1 for the effort, Is it relevant as well to the newer versions? I am trying to write an add-n my self and It is kind of hard with the old documentation – Green Oct 20 '18 at 14:18