【问题标题】:How use original jQuery library in other modules in the Thunderbird extension如何在 Thunderbird 扩展的其他模块中使用原始 jQuery 库
【发布时间】:2018-04-06 07:06:51
【问题描述】:

我正在尝试在 javascript 模块 jquery.jsm 中添加 jQuery 库。 我在其他模块中使用 jquery.jsm。 jQuery 需要一个窗口对象,所以我不能使用原始的 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

【问题讨论】:

    标签: javascript jquery thunderbird thunderbird-addon


    【解决方案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;

    【讨论】:

    • +1 的努力,它是否也与较新的版本相关?我正在尝试自己编写一个 add-n,但旧文档有点困难
    猜你喜欢
    • 2017-04-29
    • 2021-09-20
    • 1970-01-01
    • 2018-06-13
    • 1970-01-01
    • 2013-12-01
    • 2015-04-18
    • 1970-01-01
    • 2019-10-19
    相关资源
    最近更新 更多