Index: lib/aardvark.js |
=================================================================== |
--- a/lib/aardvark.js |
+++ b/lib/aardvark.js |
@@ -10,34 +10,60 @@ let {Prefs} = require("prefs"); |
let messageManager = Cc["@mozilla.org/parentprocessmessagemanager;1"] |
.getService(Ci.nsIMessageListenerManager) |
.QueryInterface(Ci.nsIMessageBroadcaster); |
// To be replaced when selection starts |
function E(id) {return null;} |
+messageManager.addMessageListener("ElemHideHelper:Response", |
+ messageResponse); |
messageManager.addMessageListener("ElemHideHelper:SelectionStarted", |
selectionStarted); |
messageManager.addMessageListener("ElemHideHelper:SelectionSucceeded", |
selectionSucceeded); |
messageManager.addMessageListener("ElemHideHelper:SelectionStopped", |
selectionStopped); |
onShutdown.add(() => |
{ |
+ messageManager.removeMessageListener("ElemHideHelper:Response", |
+ messageResponse); |
messageManager.removeMessageListener("ElemHideHelper:SelectionStarted", |
selectionStarted); |
messageManager.removeMessageListener("ElemHideHelper:SelectionSucceeded", |
selectionSucceeded); |
messageManager.removeMessageListener("ElemHideHelper:SelectionStopped", |
selectionStopped); |
selectionStopped(); |
}); |
+let maxMessageId = 0; |
+let messageCallbacks = new Map(); |
+ |
+function sendMessageWithResponse(messageName, data, callback) |
+{ |
+ if (!data) |
+ data = {}; |
+ data.messageId = ++maxMessageId; |
+ messageCallbacks.set(data.messageId, callback); |
+ messageManager.broadcastAsyncMessage(messageName, data); |
+} |
+ |
+function messageResponse(message) |
+{ |
+ let callback = messageCallbacks.get(message.data.messageId); |
+ if (callback) |
+ { |
+ messageCallbacks.delete(message.data.messageId); |
+ callback(message.data); |
+ } |
+} |
+ |
function selectionStarted(message) |
{ |
Aardvark.selectionStarted(); |
} |
function selectionSucceeded(message) |
{ |
Aardvark.selectionSucceeded(message.data); |
@@ -268,42 +294,43 @@ let Aardvark = exports.Aardvark = |
"lock", |
"quit", |
"blinkElement", |
"viewSource", |
"viewSourceWindow", |
"showMenu" |
], |
- viewSource: function(elem) |
+ viewSource: function() |
{ |
- if (!elem) |
- return false; |
- |
- var sourceBox = E("ehh-viewsource"); |
+ let sourceBox = E("ehh-viewsource"); |
if (sourceBox.state == "open") |
{ |
sourceBox.hidePopup(); |
return true; |
} |
- sourceBox.hidePopup(); |
- |
- while (sourceBox.firstElementChild) |
- sourceBox.removeChild(sourceBox.firstElementChild); |
- this.getOuterHtmlFormatted(elem, sourceBox); |
- let anchor = this.window.document.documentElement; |
- let x = this.mouseX; |
- let y = this.mouseY; |
- this.viewSourceTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer); |
- this.viewSourceTimer.initWithCallback(function() |
+ sendMessageWithResponse("ElemHideHelper:SerializeSelected", null, data => |
{ |
- sourceBox.showPopup(anchor, x, y, "tooltip", "topleft", "topleft"); |
- Aardvark.viewSourceTimer = null; |
- }, 500, Ci.nsITimer.TYPE_ONE_SHOT); |
+ sourceBox.hidePopup(); |
+ |
+ while (sourceBox.firstElementChild) |
+ sourceBox.removeChild(sourceBox.firstElementChild); |
+ this.getOuterHtmlFormatted(data.serialized, sourceBox); |
+ |
+ let anchor = this.window.document.documentElement; |
+ let x = this.mouseX; |
+ let y = this.mouseY; |
+ this.viewSourceTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer); |
+ this.viewSourceTimer.initWithCallback(function() |
+ { |
+ sourceBox.showPopup(anchor, x, y, "tooltip", "topleft", "topleft"); |
+ Aardvark.viewSourceTimer = null; |
+ }, 500, Ci.nsITimer.TYPE_ONE_SHOT); |
+ }); |
return true; |
}, |
viewSourceWindow: function(elem) |
{ |
if (!elem) |
return false; |
@@ -333,92 +360,73 @@ let Aardvark = exports.Aardvark = |
null, null, selection, "selection" |
); |
} |
return true; |
}, |
getOuterHtmlFormatted: function(node, container) |
{ |
- var type = null; |
- switch (node.nodeType) |
+ let type = node.type; |
+ if (type == "element") |
{ |
- case node.ELEMENT_NODE: |
- var box = this.window.document.createElement("vbox"); |
- box.className = "elementBox"; |
- |
- var startTag = this.window.document.createElement("hbox"); |
- startTag.className = "elementStartTag"; |
- if (!node.firstChild) |
- startTag.className += " elementEndTag"; |
- |
- this.appendDescription(startTag, "<", null); |
- this.appendDescription(startTag, node.tagName, "tagName"); |
- |
- for (var i = 0; i < node.attributes.length; i++) |
- { |
- var attr = node.attributes[i]; |
- this.appendDescription(startTag, attr.name, "attrName"); |
- if (attr.value != "") |
- { |
- this.appendDescription(startTag, "=", null); |
- this.appendDescription(startTag, '"' + attr.value.replace(/"/, """) + '"', "attrValue"); |
- } |
- } |
+ let box = this.window.document.createElement("vbox"); |
+ box.className = "elementBox"; |
- this.appendDescription(startTag, node.firstChild ? ">" : " />", null); |
- box.appendChild(startTag); |
- |
- if (node.firstChild) |
- { |
- for (var child = node.firstChild; child; child = child.nextSibling) |
- this.getOuterHtmlFormatted(child, box); |
+ let startTag = this.window.document.createElement("hbox"); |
+ startTag.className = "elementStartTag"; |
+ if (!node.children.length) |
+ startTag.className += " elementEndTag"; |
- var endTag = this.window.document.createElement("hbox"); |
- endTag.className = "elementEndTag"; |
- this.appendDescription(endTag, "<", null); |
- this.appendDescription(endTag, "/" + node.tagName, "tagName"); |
- this.appendDescription(endTag, ">", null); |
- box.appendChild(endTag); |
- } |
- container.appendChild(box); |
- return; |
+ this.appendDescription(startTag, "<", null); |
+ this.appendDescription(startTag, node.tagName, "tagName"); |
- case node.TEXT_NODE: |
- type = "text"; |
- break; |
- case node.CDATA_SECTION_NODE: |
- type = "cdata"; |
- break; |
- case node.COMMENT_NODE: |
- type = "comment"; |
- break; |
- default: |
- return; |
+ for (let {name, value} of node.attributes) |
+ { |
+ this.appendDescription(startTag, name, "attrName"); |
+ if (value != "") |
+ { |
+ this.appendDescription(startTag, "=", null); |
+ this.appendDescription(startTag, `"${value.replace(/"/, """)}"`, |
+ "attrValue"); |
+ } |
+ } |
+ |
+ this.appendDescription(startTag, node.children.length ? ">" : " />", null); |
+ box.appendChild(startTag); |
+ |
+ if (node.children.length) |
+ { |
+ for (let child of node.children) |
+ this.getOuterHtmlFormatted(child, box); |
+ |
+ let endTag = this.window.document.createElement("hbox"); |
+ endTag.className = "elementEndTag"; |
+ this.appendDescription(endTag, "<", null); |
+ this.appendDescription(endTag, "/" + node.tagName, "tagName"); |
+ this.appendDescription(endTag, ">", null); |
+ box.appendChild(endTag); |
+ } |
+ container.appendChild(box); |
+ return; |
} |
- var text = node.nodeValue.replace(/\r/g, '').replace(/^\s+/, '').replace(/\s+$/, ''); |
+ let text = node.text.replace(/\r/g, "").trim(); |
if (text == "") |
return; |
- if (type != "cdata") |
- { |
- text = text.replace(/&/g, "&") |
- .replace(/</g, "<") |
- .replace(/>/g, ">"); |
- } |
- text = text.replace(/\t/g, " "); |
- if (type == "cdata") |
- text = "<![CDATA[" + text + "]]>"; |
- else if (type == "comment") |
+ text = text.replace(/&/g, "&") |
+ .replace(/</g, "<") |
+ .replace(/>/g, ">") |
+ .replace(/\t/g, " "); |
+ if (type == "comment") |
text = "<!--" + text + "-->"; |
- var lines = text.split("\n"); |
- for (var i = 0; i < lines.length; i++) |
- this.appendDescription(container, lines[i].replace(/^\s+/, '').replace(/\s+$/, ''), type); |
+ for (let line of text.split("\n")) |
+ this.appendDescription(container, line.trim(), type); |
}, |
showMenu: function() |
{ |
var helpBox = E("ehh-helpbox"); |
if (helpBox.state == "open") |
{ |
helpBox.hidePopup(); |