Skip to content
Browse files

* support for enabling/disable on a tab level

  • Loading branch information...
1 parent 82f7439 commit 79a70a52fd04306cde07b66301f93cf229ba706d @benmmurphy committed
Showing with 159 additions and 29 deletions.
  1. +92 −22 chrome/content/module.js
  2. +67 −7 chrome/content/overlay.js
View
114 chrome/content/module.js
@@ -1,23 +1,34 @@
var EXPORTED_SYMBOLS = ["jsbeautifier"];
-var jsbeautifier = {active:false, listeners: []};
-jsbeautifier.addListener = function(l) {
- this.listeners.push(l);
-};
-
-jsbeautifier.removeListener = function(l) {
- var i = this.listeners.indexOf(l);
- if (i < 0) {
- return;
- }
- this.listeners.splice(i, 1);
-};
-
-jsbeautifier.toggle = function() {
- this.active = !this.active;
- for (var i = 0; i < this.listeners.length; ++i) {
- this.listeners[i]();
+jsbeautifier = {
+ windows : [],
+
+ add: function(browser, worker) {
+ this.windows.push({browser: browser, cfg: {active:false}, worker: worker});
+ },
+
+ getConfig: function(browser) {
+ for (var i = 0; i < this.windows.length; ++i) {
+ if (this.windows[i].browser == browser) {
+ return this.windows[i].cfg;
+ }
+ }
+ },
+
+ remove: function(browser) {
+ var idx = -1;
+ /* when tabs are migrated we get a TabOpen followed by a TabClose */
+ for (var i = 0; i < this.windows.length; ++i) {
+ if (this.windows[i].browser == browser) {
+ idx = i;
+ break;
+ }
+ }
+
+ if (idx >= 0) {
+ this.windows.splice(idx, 1);
+ }
}
};
@@ -64,15 +75,74 @@ var jsb = function() {
var httpRequestObserver = {
observe: function(subject, topic, data) {
- if (jsbeautifier.active && (topic == 'http-on-examine-response' || topic == 'http-on-examine-cached-response')) {
- if (subject instanceof Components.interfaces.nsIHttpChannel) {
- var newListener = new JSBeautifierListener();
+ if ((topic == 'http-on-examine-response' || topic == 'http-on-examine-cached-response')) {
+ if (subject instanceof Ci.nsIHttpChannel) {
+
+
subject.QueryInterface(Ci.nsITraceableChannel);
- newListener.originalListener = subject.setNewListener(newListener);
+ subject.QueryInterface(Ci.nsIHttpChannel);
+
+ var context = this.getContext(this.getWindowFromChannel(subject));
+
+ if (context != null && context.cfg.active) {
+ var newListener = new JSBeautifierListener();
+ newListener.worker = context.worker;
+ newListener.originalListener = subject.setNewListener(newListener);
+ }
}
}
},
+
+ getWindowFromChannel: function(aChannel) {
+ var ctx = this.getLoadContext(aChannel);
+ if (ctx) {
+ return ctx.associatedWindow;
+ }
+
+ return null;
+ },
+
+ getContext : function(win)
+ {
+ for (; win; win = win.parent) {
+ for (var i = 0; i < jsbeautifier.windows.length; ++i) {
+ var entry = jsbeautifier.windows[i];
+ if (entry.browser.contentWindow == win) {
+ return entry;
+ }
+ }
+
+ if (win.parent == win) {
+ return null;
+ }
+ }
+ return null;
+ },
+
+
+ getLoadContext: function (aChannel) {
+ try {
+ if (aChannel.notificationCallbacks) {
+ return aChannel.notificationCallbacks.getInterface(Ci.nsILoadContext);
+ }
+ } catch (e) {
+
+ }
+
+ try {
+ if (aChannel && aChannel.loadGroup && aChannel.loadGroup.notificationCallbacks) {
+ return aChannel.loadGroup.notificationCallbacks.getInterface(Ci.nsILoadContext);
+ }
+ } catch (e) {
+
+ }
+
+
+ return null;
+ },
+
+
register: function() {
var observerService = Cc["@mozilla.org/observer-service;1"]
@@ -156,7 +226,7 @@ var jsb = function() {
};
JSBeautifierListener.prototype.spawnWorker = function(request, context, statusCode) {
- var worker = new jsbeautifier.Worker("chrome://jsbeautifier/content/worker.js");
+ var worker = new this.worker("chrome://jsbeautifier/content/worker.js");
worker.postMessage(this.receivedData);
this.receivedData = null;
View
74 chrome/content/overlay.js
@@ -1,28 +1,88 @@
(function() {
Components.utils.import("resource://modules/module.js");
- jsbeautifier.Worker = Worker;
+ function selected_config() {
+
+ if (gBrowser.selectedBrowser == null) {
+ return null;
+ }
+ var browser = gBrowser.selectedBrowser;
+ if (browser != null) {
+ var cfg = jsbeautifier.getConfig(browser);
+ return cfg;
+ }
+
+ return null;
+ };
+
function update_text() {
- document.getElementById("jsbStatus").label = jsbeautifier.active ? "JSB ON" : "JSB OFF";
+
+
+
+ var label = "JSB OFF";
+
+ var cfg = selected_config();
+ if (cfg != null) {
+ label = cfg.active ? "JSB ON" : "JSB OFF";
+ }
+
+
+ document.getElementById("jsbStatus").label = label;
};
function toggle() {
- jsbeautifier.toggle();
+
+ var cfg = selected_config();
+ if (cfg != null) {
+ cfg.active = !cfg.active;
+ update_text();
+ }
+
};
function load() {
- update_text();
- jsbeautifier.addListener(update_text);
+
document.getElementById("jsbStatus").addEventListener("click", toggle, false);
+ update_text();
};
-
+
function unload() {
- jsbeautifier.removeListener(update_text);
+ for (var i = 0; i < container.childNodes.length; ++i) {
+ jsbeautifier.remove(gBrowser.getBrowserForTab(container.childNodes[i]));
+ }
+ container.removeEventListener("TabOpen", tabOpen, false);
+ container.removeEventListener("TabClose", tabClose, false);
+ container.removeEventListener("TabSelect", tabClose, false);
+ };
+
+ function tabOpen(event) {
+ var browser = gBrowser.getBrowserForTab(event.target);
+ jsbeautifier.add(browser, window.Worker);
};
+ function tabClose(event) {
+ var browser = gBrowser.getBrowserForTab(event.target);
+ jsbeautifier.remove(browser);
+ };
+
+ function tabSelect(event) {
+ update_text();
+ };
+
+
+ var container = gBrowser.tabContainer;
+ container.addEventListener("TabOpen", tabOpen, false);
+ container.addEventListener("TabClose", tabClose, false);
+ container.addEventListener("TabSelect", tabSelect, false);
+
+ for (var i = 0; i < container.childNodes.length; ++i) {
+ jsbeautifier.add(gBrowser.getBrowserForTab(container.childNodes[i]), window.Worker);
+ }
+
window.addEventListener("load", load, false);
window.addEventListener("unload", unload, false);
+
})();

0 comments on commit 79a70a5

Please sign in to comment.
Something went wrong with that request. Please try again.