4

I loaded jQuery on a Firefox addon, which causes it to load using XUL's window.

I want to change the default context so it will use the correct window object.

The classic example I always see is:

var $ = function(selector,context){
   return new  jQuery.fn.init(selector,context|| correctWindow );
};
$.fn = $.prototype = jQuery.fn;

But doing it I kill a lot of functions, like .ajax, .browser, ... I need these methods.

AMO won't let me create an wrapper to jQuery to send the correct window object.

So my only option is to somehow change jQuery's default context without changing its source code.

What options do I have?

BrunoLM
  • 97,872
  • 84
  • 296
  • 452
  • possible duplicate of [Override default jQuery selector context](http://stackoverflow.com/questions/3690447/override-default-jquery-selector-context) – BrunoLM Apr 03 '12 at 23:09
  • Previous answer that should help:
    http://stackoverflow.com/a/3690529/437226
    – ramblinjan Apr 03 '12 at 23:05

2 Answers2

1

On the first content script I added

var loader = Components
    .classes["@mozilla.org/moz/jssubscript-loader;1"]
    .getService(Components.interfaces.mozIJSSubScriptLoader);

loader.loadSubScript("chrome://extensions/js/jquery-1.7.2.min.js");

var initjQuery = jQuery.fn.init;
$.fn.init = function(s,c,r) {
    c = c || window.document;
    return new initjQuery(s,c,r);
};

This way it works.

BrunoLM
  • 97,872
  • 84
  • 296
  • 452
0

You change the default context with this wrapper:

jQuery.fn.init2 = jQuery.fn.init;

jQuery.prototype.init = function (selection, context, root) {
    return new jQuery.fn.init2(selection, context, jQuery.context || root);
}

$(document).ready(function () {
    jQuery.context = $(newContextSelector);
    $("*").css("color", "red");
});

Look at context that is a jquery object.