简体   繁体   中英

Porting Chrome extension using jQuery to Firefox SDK Add-on

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM