Porting Chrome extension using jQuery to Firefox SDK Add-on

依然范特西╮ 提交于 2019-12-08 04:37:30

问题


My Chrome extension makes use of jQuery in the background page, for things like jQuery.extend(), jQuery.ajax(), jQuery.deferred(), (not DOM manipulation stuff, which doesn't make sense in a background page).

Migrating this code to a Firefox SDK Add-on, there's no background window object, which jQuery requires to work, so something like

var $ = require('../3rdparty/jquery.min')(window);

which is how jQuery works in a CommonJS-like environment, fails, with jQuery itself throwing a jQuery requires a window with a document exception.

Is there any way to use jQuery in a Firefox SDK-based add-on? Page Workers seemed promising, but I can't get hold of the underlying window object.

Andy


回答1:


This works:

    var {Cc, Ci} = require("chrome");
    _window = Cc["@mozilla.org/appshell/appShellService;1"]
        .getService(Ci.nsIAppShellService).‌​hiddenDOMWindow;

    $ = require('../3rdparty/jquery')(_window);

However, I had to patch jQuery (2.1.3) itself, changing line 3441 to

    window.setTimeout( jQuery.ready );

I'm reasonably confident this is a jQuery bug.




回答2:


Thanks Andy! It seems to be working beautifully!

When I read that you had to hack jQuery a bit, I set out to submit a fix but it turned out that somebody did just that already!

https://github.com/jquery/jquery/commit/842958e7aecd0d75a7ee9e2aaec83457701aa2f3

It's been shipped in jQuery 3.0.0-alpha1 though.



来源:https://stackoverflow.com/questions/28932362/porting-chrome-extension-using-jquery-to-firefox-sdk-add-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!