简体   繁体   English

使用jQuery将Chrome扩展程序移植到Firefox SDK插件

[英]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). 我的Chrome扩展程序在后台页面中使用jQuery,用于jQuery.extend()jQuery.ajax()jQuery.deferred() (不是DOM操作的东西,在后台页面中没有意义) 。

Migrating this code to a Firefox SDK Add-on , there's no background window object, which jQuery requires to work, so something like 将此代码迁移到Firefox SDK插件中 ,没有后台window对象(jQuery需要使用该window对象),因此类似

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. 这就是jQuery在类似CommonJS的环境中的工作方式,但由于jQuery本身抛出jQuery requires a window with a document异常jQuery requires a window with a document ,因此失败。

Is there any way to use jQuery in a Firefox SDK-based add-on? 有什么方法可以在基于Firefox SDK的附加组件中使用jQuery? Page Workers seemed promising, but I can't get hold of the underlying window object. Page Workers看起来很有前途,但是我无法掌握底层的window对象。

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 但是,我不得不修补jQuery(2.1.3)本身,将3441行更改为

    window.setTimeout( jQuery.ready );

I'm reasonably confident this is a jQuery bug. 我有理由相信这是一个jQuery错误。

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! 当我得知您必须对jQuery进行一些修改时,我开始提交修复程序,但事实证明有人已经这样做了!

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

It's been shipped in jQuery 3.0.0-alpha1 though. 它已经在jQuery 3.0.0-alpha1中发布了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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