Skip to content

Commit

Permalink
Refactored
Browse files Browse the repository at this point in the history
Fixed some commands to work with older Firefox
  • Loading branch information
LouCypher committed Oct 8, 2013
1 parent e78fb22 commit 96dd3ba
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 81 deletions.
6 changes: 6 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Auto detect text files and perform LF normalization
* text eol=lf

*.gif binary
*.png binary
*.jpg binary
4 changes: 2 additions & 2 deletions cssreload.mozcmd
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
// @url https://github.com/auchenberg/css-reloader
// @license Creative Commons Attribution 3.0 Unported License

let document = context.environment.document || context.environment.contentDocument;
let elements = document.querySelectorAll("link[rel=stylesheet][href]");
let contentDoc = context.environment.document || context.environment.contentDocument;
let elements = contentDoc.querySelectorAll("link[rel=stylesheet][href]");
for (let i = 0; i < elements.length; i++) {
let element = elements[i];
let h = element.href.replace(/[?&]cssReloader=([^&$]*)/, "");
Expand Down
1 change: 0 additions & 1 deletion darken.mozcmd
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
License under Public Domain Dedication
http://creativecommons.org/publicdomain/zero/1.0/
*/

[
{
name: "darken",
Expand Down
4 changes: 2 additions & 2 deletions escape.mozcmd
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
],
returnType: "string",
exec: function(args, context) {
let win = context.environment.window || context.environment.contentDocument.defaultView;
let contentWin = context.environment.window || context.environment.contentDocument.defaultView;
let string = args.string;
if (!string)
string = win.location.href;
string = contentWin.location.href;
let escaped = encodeURIComponent(string);
let clipboardHelper = Cc["@mozilla.org/widget/clipboardhelper;1"].
getService(Ci.nsIClipboardHelper);
Expand Down
76 changes: 35 additions & 41 deletions extension/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
Cu.import("resource:///modules/devtools/gcli.jsm");
Cu.import("resource://gre/modules/Services.jsm");

/** unused for now
/** for future use
Cu.import("resource://gre/modules/AddonManager.jsm");
Cu.import("resource://gre/modules/FileUtils.jsm");
Cu.import("resource://gre/modules/NetUtil.jsm");
Expand Down Expand Up @@ -66,8 +66,8 @@ var mozcmd = {
// @url https://github.com/auchenberg/css-reloader
// @license Creative Commons Attribution 3.0 Unported License

let document = context.environment.document || context.environment.contentDocument;
let elements = document.querySelectorAll("link[rel=stylesheet][href]");
let contentDoc = context.environment.document || context.environment.contentDocument;
let elements = contentDoc.querySelectorAll("link[rel=stylesheet][href]");
for (let i = 0; i < elements.length; i++) {
let element = elements[i];
let h = element.href.replace(/[?&]cssReloader=([^&$]*)/, "");
Expand Down Expand Up @@ -136,10 +136,10 @@ var mozcmd = {
],
returnType: "string",
exec: function(args, context) {
let win = context.environment.window || context.environment.contentDocument.defaultView;
let contentWin = context.environment.window || context.environment.contentDocument.defaultView;
let string = args.string;
if (!string)
string = win.location.href;
string = contentWin.location.href;
let escaped = encodeURIComponent(string);
let clipboardHelper = Cc["@mozilla.org/widget/clipboardhelper;1"].
getService(Ci.nsIClipboardHelper);
Expand Down Expand Up @@ -181,13 +181,13 @@ var mozcmd = {
],
returnType: "string",
exec: function(args, context) {
let win = context.environment.window || context.environment.contentDocument.defaultView;
let contentWin = context.environment.window || context.environment.contentDocument.defaultView;
let prefname = "javascript.enabled";
let prefs = Services.prefs;
let jsEnabled = prefs.getBoolPref(prefname);
prefs.setBoolPref(prefname, !jsEnabled);
if (args.reload)
win.location.reload();
contentWin.location.reload();
return "JavaScript is " + (!jsEnabled ? "enabled" : "disabled") + ".";
}
},
Expand Down Expand Up @@ -247,19 +247,19 @@ var mozcmd = {
],
exec: function(args, context) {
let environment = context.environment;
let document = environment.document || environment.contentDocument;
let chromeWindow = environment.chromeWindow || environment.chromeDocument.defaultView;
let contentDoc = environment.document || environment.contentDocument;
let chromeWin = environment.chromeWindow || environment.chromeDocument.defaultView;

let url;
if (args.url)
url = args.url;
else
url = document.URL;
url = contentDoc.location.href;

if (!/^[a-z0-9]+:/i.test(url))
url = "http://" + url; // Use 'http' by default

chromeWindow.switchToTabHavingURI("view-source:" + url, true);
chromeWin.switchToTabHavingURI("view-source:" + url, true);
}
},

Expand All @@ -268,23 +268,17 @@ var mozcmd = {
description: "View rendered source of current page.",
exec: function(args, context) {
let environment = context.environment;
let document = environment.document || environment.contentDocument;
let chromeWindow = environment.chromeWindow || environment.chromeDocument.defaultView;

let doctypeElem = "";
let doctype = document.doctype;
if (doctype) {
doctypeElem += "<!DOCTYPE " + doctype.name;
doctypeElem += doctype.publicId ? (' PUBLIC "' + doctype.publicId + '"') : "";
doctypeElem += doctype.systemId ? (' "' + doctype.systemId + '"') : "";
doctypeElem += ">\n";
}
let source = doctypeElem + document.documentElement.outerHTML;
let isHTML = document.createElement("div").tagName === "DIV";
let contentDoc = environment.document || environment.contentDocument;
let chromeWin = environment.chromeWindow || environment.chromeDocument.defaultView;

let domSerializer = Cc["@mozilla.org/xmlextras/xmlserializer;1"].
createInstance(Ci.nsIDOMSerializer);
let source = domSerializer.serializeToString(contentDoc);
let isHTML = contentDoc.createElement("div").tagName === "DIV";
let contentType = isHTML ? "text/html" : "application/xml";
let dataURI = "data:" + contentType + ";charset=utf-8" + ","
+ encodeURIComponent(source);
chromeWindow.switchToTabHavingURI("view-source:" + dataURI, true);
chromeWin.switchToTabHavingURI("view-source:" + dataURI, true);
}
},

Expand All @@ -299,20 +293,20 @@ var mozcmd = {
description: "Domain name to lookup. If no domain specified, lookup current web site."
}
],
returnType: "string",
//returnType: "string",
exec: function(args, context) {
let environment = context.environment;
let content = environment.window || environment.contentDocument.defaultView;
let window = environment.chromeWindow || environment.chromeDocument.defaultView;
let contentWin = environment.window || environment.contentDocument.defaultView;
let chromeWin = environment.chromeWindow || environment.chromeDocument.defaultView;

let hostname;
if (!args.domain)
hostname = content.location.hostname;
hostname = contentWin.location.hostname;
else
hostname = args.domain;

if (!hostname) {
return content.location.protocol + " scheme is not supported.";
return contentWin.location.protocol + " scheme is not supported.";
}

let eTLDsvc = Services.eTLD;
Expand All @@ -322,7 +316,7 @@ var mozcmd = {
} catch (ex) {
eTLD = URI.asciiHost;
}
window.switchToTabHavingURI("http://whois.domaintools.com/" + eTLD, true);
chromeWin.switchToTabHavingURI("http://whois.domaintools.com/" + eTLD, true);
//return eTLD;
}
},
Expand Down Expand Up @@ -352,19 +346,19 @@ var mozcmd = {
returnType: "string",
exec: function(args, context) {
let environment = context.environment;
let window = environment.chromeWindow || environment.chromeDocument.defaultView;
let document = environment.chromeDocument;
let browserWin = document.documentElement;
let screen = window.screen;
let chromeWin = environment.chromeWindow || environment.chromeDocument.defaultView;
let chromeDoc = environment.chromeDocument;
let browserWin = chromeDoc.documentElement;
let screen = chromeWin.screen;

function centerScreen() {
window.moveTo((screen.availWidth - browserWin.width) / 2,
(screen.availHeight - browserWin.height) / 2);
chromeWin.moveTo((screen.availWidth - browserWin.width) / 2,
(screen.availHeight - browserWin.height) / 2);
}

let width = args.width, height = args.height, center = args.center;
if (width && height) {
window.resizeTo(width, height);
chromeWin.resizeTo(width, height);
if (center)
centerScreen();
return "Resized to " + width + "x" + height;
Expand All @@ -373,7 +367,7 @@ var mozcmd = {
if (center)
centerScreen();

switch (window.windowState) {
switch (chromeWin.windowState) {
case 3: // normal
return browserWin.width + "x" + browserWin.height;
case 1: // maximized
Expand All @@ -384,12 +378,12 @@ var mozcmd = {
}

function startup(data, reason) {
for (var i in mozcmd)
for (let i in mozcmd)
gcli.addCommand(mozcmd[i]);
}

function shutdown(data, reason) {
for (var i in mozcmd)
for (let i in mozcmd)
gcli.removeCommand(mozcmd[i]);
}

Expand Down
2 changes: 1 addition & 1 deletion extension/install.rdf
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<em:type>2</em:type>
<em:bootstrap>true</em:bootstrap>
<em:name>mozcmd</em:name><!-- Temporary name -->
<em:version>0.2.1</em:version>
<em:version>0.3</em:version>
<em:description>Add more commands for Developer Toolbar (GCLI).</em:description>
<em:creator>LouCypher</em:creator>
<em:homepageURL>https://github.com/LouCypher/mozcmd</em:homepageURL>
Expand Down
4 changes: 2 additions & 2 deletions jsenabled.mozcmd
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
],
returnType: "string",
exec: function(args, context) {
let win = context.environment.window || context.environment.contentDocument.defaultView;
let contentWin = context.environment.window || context.environment.contentDocument.defaultView;
let prefname = "javascript.enabled";
let prefs = Services.prefs;
let jsEnabled = prefs.getBoolPref(prefname);
prefs.setBoolPref(prefname, !jsEnabled);
if (args.reload)
win.location.reload();
contentWin.location.reload();
return "JavaScript is " + (!jsEnabled ? "enabled" : "disabled") + ".";
}
}
Expand Down
10 changes: 9 additions & 1 deletion locale.mozcmd
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,15 @@
],
returnType: "string",
exec: function(args, context) {
let prefname = "general.useragent.locale";
let prefname = "general.useragent.locale"; // See AMO warning below
/*
AMO warning from add-on validator:
---
Potentially unsafe preference branch referenced
Warning: Extensions should not alter preferences in this preference branch
---
I couldn't find another way to change browser locale without using preferences
*/
let prefs = Services.prefs;
let msgPrefix = "Browser language: '";
let msgSuffix = "'";
Expand Down
28 changes: 11 additions & 17 deletions view-source.mozcmd
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@
],
exec: function(args, context) {
let environment = context.environment;
let document = environment.document || environment.contentDocument;
let chromeWindow = environment.chromeWindow || environment.chromeDocument.defaultView;
let contentDoc = environment.document || environment.contentDocument;
let chromeWin = environment.chromeWindow || environment.chromeDocument.defaultView;

let url;
if (args.url)
url = args.url;
else
url = document.URL;
url = contentDoc.location.href;

if (!/^[a-z0-9]+:/i.test(url))
url = "http://" + url; // Use 'http' by default

chromeWindow.switchToTabHavingURI("view-source:" + url, true);
chromeWin.switchToTabHavingURI("view-source:" + url, true);
}
},

Expand All @@ -43,23 +43,17 @@
description: "View rendered source of current page.",
exec: function(args, context) {
let environment = context.environment;
let document = environment.document || environment.contentDocument;
let chromeWindow = environment.chromeWindow || environment.chromeDocument.defaultView;
let contentDoc = environment.document || environment.contentDocument;
let chromeWin = environment.chromeWindow || environment.chromeDocument.defaultView;

let doctypeElem = "";
let doctype = document.doctype;
if (doctype) {
doctypeElem += "<!DOCTYPE " + doctype.name;
doctypeElem += doctype.publicId ? (' PUBLIC "' + doctype.publicId + '"') : "";
doctypeElem += doctype.systemId ? (' "' + doctype.systemId + '"') : "";
doctypeElem += ">\n";
}
let source = doctypeElem + document.documentElement.outerHTML;
let isHTML = document.createElement("div").tagName === "DIV";
let domSerializer = Cc["@mozilla.org/xmlextras/xmlserializer;1"].
createInstance(Ci.nsIDOMSerializer);
let source = domSerializer.serializeToString(contentDoc);
let isHTML = contentDoc.createElement("div").tagName === "DIV";
let contentType = isHTML ? "text/html" : "application/xml";
let dataURI = "data:" + contentType + ";charset=utf-8" + ","
+ encodeURIComponent(source);
chromeWindow.switchToTabHavingURI("view-source:" + dataURI, true);
chromeWin.switchToTabHavingURI("view-source:" + dataURI, true);
}
}
]
12 changes: 6 additions & 6 deletions whois.mozcmd
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@
description: "Domain name to lookup. If no domain specified, lookup current web site."
}
],
returnType: "string",
//returnType: "string",
exec: function(args, context) {
let environment = context.environment;
let content = environment.window || environment.contentDocument.defaultView;
let window = environment.chromeWindow || environment.chromeDocument.defaultView;
let contentWin = environment.window || environment.contentDocument.defaultView;
let chromeWin = environment.chromeWindow || environment.chromeDocument.defaultView;

let hostname;
if (!args.domain)
hostname = content.location.hostname;
hostname = contentWin.location.hostname;
else
hostname = args.domain;

if (!hostname) {
return content.location.protocol + " scheme is not supported.";
return contentWin.location.protocol + " scheme is not supported.";
}

let eTLDsvc = Services.eTLD;
Expand All @@ -42,7 +42,7 @@
} catch (ex) {
eTLD = URI.asciiHost;
}
window.switchToTabHavingURI("http://whois.domaintools.com/" + eTLD, true);
chromeWin.switchToTabHavingURI("http://whois.domaintools.com/" + eTLD, true);
//return eTLD;
}
}
Expand Down
16 changes: 8 additions & 8 deletions winsize.mozcmd
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@
returnType: "string",
exec: function(args, context) {
let environment = context.environment;
let window = environment.chromeWindow || environment.chromeDocument.defaultView;
let document = environment.chromeDocument;
let browserWin = document.documentElement;
let screen = window.screen;
let chromeWin = environment.chromeWindow || environment.chromeDocument.defaultView;
let chromeDoc = environment.chromeDocument;
let browserWin = chromeDoc.documentElement;
let screen = chromeWin.screen;

function centerScreen() {
window.moveTo((screen.availWidth - browserWin.width) / 2,
(screen.availHeight - browserWin.height) / 2);
chromeWin.moveTo((screen.availWidth - browserWin.width) / 2,
(screen.availHeight - browserWin.height) / 2);
}

let width = args.width, height = args.height, center = args.center;
if (width && height) {
window.resizeTo(width, height);
chromeWin.resizeTo(width, height);
if (center)
centerScreen();
return "Resized to " + width + "x" + height;
Expand All @@ -54,7 +54,7 @@
if (center)
centerScreen();

switch (window.windowState) {
switch (chromeWin.windowState) {
case 3: // normal
return browserWin.width + "x" + browserWin.height;
case 1: // maximized
Expand Down

0 comments on commit 96dd3ba

Please sign in to comment.