Permalink
Browse files

Support for toolbox

  • Loading branch information...
1 parent 9f92211 commit 2076773b1ee8d0408e7d5e59e7bd207adf397291 @paulrouget committed Jan 22, 2013
Showing with 44 additions and 330 deletions.
  1. +28 −277 bootstrap.js
  2. +10 −40 chrome/jsterm.js
  3. +0 −8 chrome/jsterm.xul
  4. +3 −3 install.rdf
  5. BIN jsterm.xpi
  6. +3 −2 locale/en-US/jsterm.properties
View
305 bootstrap.js
@@ -2,298 +2,49 @@ const Cu = Components.utils;
const Cc = Components.classes;
const Ci = Components.interfaces;
+Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
-//Cu.import("resource://jsterm/modules/JSTermManager.jsm");
+Cu.import("resource://gre/modules/Services.jsm");
+Cu.import("resource:///modules/devtools/gDevTools.jsm");
+Cu.import("resource://gre/modules/commonjs/promise/core.js");
-let trackedWindows;
-let wObserver;
+XPCOMUtils.defineLazyGetter(this, "osString",
+ function() Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULRuntime).OS);
-function startup() {
- function isBrowserWindow(aWindow) {
- let winType = aWindow.document.documentElement.getAttribute("windowtype");
- return winType === "navigator:browser";
- }
+const jstermProps = "chrome://jsterm/locale/jsterm.properties";
+let jstermStrings = Services.strings.createBundle(jstermProps);
- wObserver = {
- observe: function(aSubject, aTopic, aData) {
- if (aTopic == "domwindowopened") {
- let window = aSubject.QueryInterface(Ci.nsIDOMWindow);
- window.addEventListener("load", function winWatcherLoad() {
- window.removeEventListener("load", winWatcherLoad, false);
- if (isBrowserWindow(window)) {
- JSTermManager.addControlsToWindow(window);
- JSTermManager.trackTabs(window);
- trackedWindows.add(window);
- }
- }, false);
- } else if (aTopic == "domwindowclosed") {
- let window = aSubject.QueryInterface(Ci.nsIDOMWindow);
- JSTermManager.untrackTabs(window);
- trackedWindows.delete(window);
- }
- },
- }
+let jstermDefinition = {
+ id: "jsterm",
+ key: jstermStrings.GetStringFromName("JSTerm.commandkey"),
+ ordinal: 0,
+ modifiers: osString == "Darwin" ? "accel,alt" : "accel,shift",
+ icon: "chrome://browser/skin/devtools/tool-webconsole.png",
+ url: "chrome://jsterm/content/jsterm.xul",
+ label: jstermStrings.GetStringFromName("JSTerm.label"),
+ tooltip: jstermStrings.GetStringFromName("JSTerm.tooltip"),
- let wWatcher = Cc["@mozilla.org/embedcomp/window-watcher;1"].getService(Ci.nsIWindowWatcher);
- let winEnum = wWatcher.getWindowEnumerator();
- wWatcher.registerNotification(wObserver);
-
- trackedWindows = new Set();
+ isTargetSupported: function(target) {
+ return target.isLocalTab;
+ },
- let winEnum = wWatcher.getWindowEnumerator();
- while (winEnum.hasMoreElements()) {
- let window = winEnum.getNext().QueryInterface(Ci.nsIDOMWindow);
- if (isBrowserWindow(window)) {
- JSTermManager.addControlsToWindow(window);
- JSTermManager.trackTabs(window);
- trackedWindows.add(window);
- }
+ build: function(iframeWindow, toolbox) {
+ iframeWindow.JSTermUI.init(JSTermGlobalHistory, toolbox);
+ return Promise.resolve(iframeWindow.JSTermUI);
}
+};
+
+function startup() {
+ gDevTools.registerTool(jstermDefinition);
}
function shutdown() {
- let wWatcher = Cc["@mozilla.org/embedcomp/window-watcher;1"].getService(Ci.nsIWindowWatcher);
- wWatcher.unregisterNotification(wObserver);
- let winEnum = wWatcher.getWindowEnumerator();
- while (winEnum.hasMoreElements()) {
- let window = winEnum.getNext().QueryInterface(Ci.nsIDOMWindow);
- if (trackedWindows && trackedWindows.has(window)) {
- JSTermManager.removeControlsFromWindow(window);
- JSTermManager.untrackTabs(window);
- for (let b of window.gBrowser.browsers) {
- JSTermManager.closeForBrowser(b);
- }
- }
- }
- trackedWindows = null;
- wObserver = null;
+ gDevTools.unregisterTool(jstermDefinition);
}
function install() {}
function uninstall() {}
-/* ***** resource **** */
-
-let JSTermManager = {
- _map: new WeakMap(),
- _listeners: new WeakMap(),
- where: "in_browser",
-
- addControlsToWindow: function(aWindow) {
- let strings = Services.strings.createBundle("chrome://jsterm/locale/jsterm.properties");
-
- let doc = aWindow.document;
-
- aWindow.JSTermManager = this;
-
- let command = doc.createElement("command");
- command.id = "Tools:JSTerm";
- command.setAttribute("oncommand", "JSTermManager.toggleForBrowser(this)");
- doc.querySelector("#mainCommandSet").appendChild(command);
-
- let broadcaster = doc.createElement("broadcaster");
- broadcaster.id = "devtoolsMenuBroadcaster_JSTerm";
- broadcaster.setAttribute("label", strings.GetStringFromName("JSTerm.menu.label"));
- broadcaster.setAttribute("type", "checkbox");
- broadcaster.setAttribute("autocheck", "false");
- broadcaster.setAttribute("key", "key_JSTerm");
- broadcaster.setAttribute("command", "Tools:JSTerm");
- doc.querySelector("#mainBroadcasterSet").appendChild(broadcaster);
-
- let menubaritem = doc.createElement("menuitem");
- menubaritem.classList.add("jsterm-addon");
- menubaritem.id = "menu_JSTerm";
- menubaritem.setAttribute("observes", "devtoolsMenuBroadcaster_JSTerm");
- let webConsoleMenu = doc.querySelector("#webConsole");
- doc.querySelector("#menuWebDeveloperPopup").insertBefore(menubaritem, webConsoleMenu);
-
- let appmenuPopup = doc.querySelector("#appmenu_webDeveloper_popup");
- if (appmenuPopup) { // no appmenu on Mac
- let appmenuitem = doc.createElement("menuitem");
- appmenuitem.classList.add("jsterm-addon");
- appmenuitem.id = "appmenu_JSTerm";
- appmenuitem.setAttribute("observes", "devtoolsMenuBroadcaster_JSTerm");
- let webConsoleAppMenu = doc.querySelector("#appmenu_webConsole");
- appmenuPopup.insertBefore(appmenuitem, webConsoleAppMenu);
- }
-
- let key = doc.createElement("key");
- key.classList.add("jsterm-addon");
- key.id = "key_JSTerm";
- key.setAttribute("key", strings.GetStringFromName("JSTerm.key"));
- key.setAttribute("command", "Tools:JSTerm");
- key.setAttribute("modifiers", "accel,alt")
- doc.querySelector("#mainKeyset").appendChild(key);
-
- let button = doc.createElement("toolbarbutton");
- button.setAttribute("observes", "devtoolsMenuBroadcaster_JSTerm");
- button.classList.add("developer-toolbar-button");
- button.classList.add("jsterm-addon");
- button.id = "developer-toolbar-jsterm";
- button.setAttribute("style", "-moz-image-region: rect(0, 16px, 16px, 0);");
- let before = doc.querySelector("#developer-toolbar-webconsole");
- doc.querySelector("#developer-toolbar").insertBefore(button, before);
- },
- removeControlsFromWindow: function(aWindow) {
- let elts = aWindow.document.querySelectorAll(".jsterm-addon,#devtoolsMenuBroadcaster_JSTerm");
- for (let e of elts) {
- try{
- e.parentNode.removeChild(e);
- }catch(e){}
- }
- let cmd = aWindow.document.getElementById("Tools:JSTerm");
- cmd.parentNode.removeChild(cmd);
- },
- trackTabs: function(aWindow) {
- let tabs = aWindow.gBrowser.tabContainer;
- let update = this.updateCheckboxStatus.bind(this, aWindow);
- tabs.addEventListener("TabSelect", update, true);
- this._listeners.set(aWindow, update);
- aWindow.addEventListener("unload", function onClose(aEvent) {
- tabs.removeEventListener("TabSelect", update, true);
- aWindow.removeEventListener("unload", onClose, false);
- }, false);
- },
- untrackTabs: function(aWindow) {
- let update = this._listeners.get(aWindow);
- if (update) {
- let tabs = aWindow.gBrowser.tabContainer;
- this._listeners.delete(aWindow);
- tabs.removeEventListener("TabSelect", update, true);
- }
- },
- isOpenForBrowser: function(aBrowser) {
- return this._map.has(aBrowser);
- },
- toggleForBrowser: function(aTarget) {
- let browser = aTarget.ownerDocument.defaultView.gBrowser.selectedBrowser;
- if (this.isOpenForBrowser(browser)) {
- this.closeForBrowser(browser);
- } else {
- this.openForBrowser(browser);
- }
- },
- openForBrowser: function(aBrowser) {
- if (this.isOpenForBrowser(aBrowser))
- return;
- let term = new JSTerm(aBrowser);
- this._map.set(aBrowser, term);
- this.updateCheckboxStatus(aBrowser.ownerDocument.defaultView);
- },
- closeForBrowser: function(aBrowser) {
- JSTermGlobalHistory.saveToPref();
- let term = this._map.get(aBrowser);
- if (!term)
- return;
- term.destroy();
- this._map.delete(aBrowser);
- this.updateCheckboxStatus(aBrowser.ownerDocument.defaultView);
- },
- moveTermTo: function(aBrowser, aWhere) {
- this.where = aWhere;
- let term = this._map.get(aBrowser);
- if (!term)
- return;
- term.rebuildUI();
- },
-
- isTermDocked: function(aBrowser) {
- let term = this._map.get(aBrowser);
- return term.docked;
- },
-
- updateCheckboxStatus: function(aWindow) {
- let selectedBrowser = aWindow.gBrowser.selectedBrowser;
- let checked = this.isOpenForBrowser(selectedBrowser);
- let broadcaster = aWindow.document.querySelector("#devtoolsMenuBroadcaster_JSTerm");
- if (checked)
- broadcaster.setAttribute("checked", "true");
- else
- broadcaster.setAttribute("checked", "false");
- },
-}
-
-function JSTerm(aBrowser) {
- this.browser = aBrowser;
- this.chromeDoc = aBrowser.ownerDocument;
- this.chromeWin = this.chromeDoc.defaultView;
- this.buildUI();
- this.savedContent = null;
-}
-
-JSTerm.prototype = {
- buildUI: function() {
- const CHROME_URL = "chrome://jsterm/content/jsterm.xul";
- const CHROME_WINDOW_FLAGS = "chrome,centerscreen,resizable,dialog=no";
-
- let termWindow;
- let doc = this.chromeDoc;
-
- this.docked = (JSTermManager.where == "in_browser");
-
- if (this.docked) {
- let nbox = this.chromeWin.gBrowser.getNotificationBox(this.browser);
- let splitter = doc.createElement("splitter");
- splitter.className = "devtools-horizontal-splitter jsterm-splitter";
-
- let container = doc.createElement("vbox");
- container.setAttribute("flex", "1");
- container.className = "jsterm-container";
- container.height = 200;
-
- let iframe = doc.createElement("iframe");
- iframe.setAttribute("src", CHROME_URL);
- iframe.setAttribute("flex", "1")
-
- container.appendChild(iframe);
- nbox.appendChild(splitter);
- nbox.appendChild(container);
-
- termWindow = iframe.contentWindow;
- } else {
- termWindow = Services.ww.openWindow(null, CHROME_URL, "_blank", CHROME_WINDOW_FLAGS, {});
- }
-
- termWindow.onload = function() {
- termWindow.JSTermUI.init(JSTermManager,
- JSTermGlobalHistory,
- this.browser,
- this.browser.contentWindow,
- this.chromeWin,
- this.savedContent);
- }.bind(this);
-
- this.termWindow = termWindow;
- },
-
- rebuildUI: function() {
- this.savedContent = this.termWindow.JSTermUI.getContent();
- this.destroyUI();
- this.buildUI();
- },
-
- destroyUI: function() {
- let nbox = this.chromeWin.gBrowser.getNotificationBox(this.browser);
- let container = nbox.querySelector(".jsterm-container");
- if (container) {
- let splitter = nbox.querySelector(".jsterm-splitter");
- splitter.parentNode.removeChild(splitter);
- container.parentNode.removeChild(container);
- } else {
- this.termWindow.close()
- }
- },
-
- destroy: function() {
- this.destroyUI();
- this.termWindow = null;
- this.browser = null;
- this.chromeDoc = null;
- this.chromeWin = null;
- }
-}
-
-
let JSTermGlobalHistory = {
_limit: 100, // Should be a pref
_entries: [],
View
50 chrome/jsterm.js
@@ -24,15 +24,7 @@ let JSTermUI = {
printTimeout: null,
close: function() {
- if (this.closing) return;
- this.closing = true;
- this.manager.closeForBrowser(this.browser);
- },
-
- closeIfInWindow: function() {
- if (!this.closing && !this.manager.isTermDocked(this.browser)) {
- this.close();
- }
+ this.toolbox.destroy();
},
registerCommands: function() {
@@ -47,10 +39,6 @@ let JSTermUI = {
exec: this.switchToContentMode.bind(this)},
{name: ":chrome", help: "switch to Chrome mode",
exec: this.switchToChromeMode.bind(this)},
- {name: ":undock", help: "Move the terminal into its own window",
- exec: this.undock.bind(this)},
- {name: ":dock", help: "Move the terminal in browser",
- exec: this.dock.bind(this)},
{name: ":toggleLightTheme", help: "Toggle the light (white) theme",
exec: this.toggleLightTheme.bind(this)},
{name: "ls", hidden: true, exec: this.ls.bind(this)},
@@ -72,13 +60,14 @@ let JSTermUI = {
this.input.focus();
},
- init: function(aManager, aGlobalHistory, aBrowser, aContent, aChrome, aDefaultContent) {
- this.manager = aManager;
- this.browser = aBrowser;
- this.content = aContent;
- this.chrome = aChrome;
+ //init: function(aManager, aGlobalHistory, aBrowser, aContent, aChrome, aDefaultContent) {
+ init: function(aGlobalHistory, aToolbox) {
+ this.toolbox = aToolbox;
- this.version = "meeh";
+ this.content = this.toolbox.target.tab.linkedBrowser.contentWindow;
+ this.chrome = this.toolbox.target.tab.ownerDocument.defaultView;
+
+ this.version = "n/a";
this.chrome.AddonManager.getAddonByID("jsterm@paulrouget.com", function(addon) {
this.version = addon.version;
}.bind(this));
@@ -90,15 +79,8 @@ let JSTermUI = {
this.focus = this.focus.bind(this);
this.container = document.querySelector("#editors-container");
- let defaultInputText, defaultOutputText;
-
- if (aDefaultContent) {
- defaultInputText = aDefaultContent.input;
- defaultOutputText = aDefaultContent.output;
- } else {
- defaultInputText = "";
- defaultOutputText = "// type ':help' for help\n// Report bug here: https://github.com/paulrouget/firefox-jsterm/issues";
- }
+ let defaultInputText = "";
+ let defaultOutputText = "// type ':help' for help\n// Report bug here: https://github.com/paulrouget/firefox-jsterm/issues";
this.history = new JSTermLocalHistory(aGlobalHistory);
@@ -522,18 +504,6 @@ let JSTermUI = {
};
},
- undock: function() {
- if (this.manager.isTermDocked(this.browser)) {
- this.manager.moveTermTo(this.browser, "own_window");
- }
- },
-
- dock: function() {
- if (!this.manager.isTermDocked(this.browser)) {
- this.manager.moveTermTo(this.browser, "in_browser");
- }
- },
-
ls: function() {
this.print("// Did you just type \"ls\"? You know this is not a unix shell, right?");
},
View
8 chrome/jsterm.xul
@@ -15,18 +15,10 @@
macanimationtype="document"
screenX="4" screenY="4"
width="640" height="480"
- onunload="JSTermUI.closeIfInWindow()"
persist="screenX screenY width height sizemode">
<script type="application/javascript" src="jsterm.js"/>
- <keyset id="jsterm-keyset">
- <key id="jsterm-key-close"
- key="&JSTerm.closeKey;"
- oncommand="JSTermUI.closeIfInWindow()"
- modifiers="accel"/>
- </keyset>
-
<hbox flex="1">
<vbox flex="1">
<vbox id="editors-container" flex="1" onclick="JSTermUI.focus()">
View
6 install.rdf
@@ -5,7 +5,7 @@
<creator>Paul Rouget</creator>
<id>jsterm@paulrouget.com</id>
<name>JavaScript Terminal</name>
- <version>2.1</version>
+ <version>2.2</version>
<bootstrap>true</bootstrap>
<type>2</type>
<iconURL>chrome://jsterm/content/icon.png</iconURL>
@@ -16,8 +16,8 @@
<targetApplication>
<r:Description>
<id>{ec8030f7-c20a-464f-9b0e-13a3a9e97384}</id>
- <minVersion>16.0a1</minVersion>
- <maxVersion>18.0a1</maxVersion>
+ <minVersion>20.0a1</minVersion>
+ <maxVersion>21.0a1</maxVersion>
</r:Description>
</targetApplication>
</r:Description>
View
BIN jsterm.xpi
Binary file not shown.
View
5 locale/en-US/jsterm.properties
@@ -1,2 +1,3 @@
-JSTerm.menu.label=JSTerm
-JSTerm.key=j
+JSTerm.commandkey=l
+JSTerm.label=JSTerm
+JSTerm.tooltip=JavaScript Terminal

0 comments on commit 2076773

Please sign in to comment.