Index: lib/child/commands.js |
=================================================================== |
--- a/lib/child/commands.js |
+++ b/lib/child/commands.js |
@@ -9,29 +9,73 @@ |
let messageManager = require("messageManager"); |
let {getNodeInfo} = require("./nodeInfo"); |
let { |
state, selectElement, setAnchorElement, stopSelection |
} = require("./selection"); |
let {getParentElement} = require("./utils"); |
messageManager.addMessageListener("ElemHideHelper:Command", onCommand); |
+messageManager.addMessageListener("ElemHideHelper:SerializeSelected", |
+ serializeSelected); |
onShutdown.add(() => |
{ |
messageManager.removeMessageListener("ElemHideHelper:Command", onCommand); |
+ messageManager.removeMessageListener("ElemHideHelper:SerializeSelected", |
+ serializeSelected); |
}); |
function onCommand(message) |
{ |
let command = message.data; |
if (typeof exports[command] == "function") |
exports[command](); |
} |
+function serializeNode(node) |
+{ |
+ let result = null; |
+ switch (node.nodeType) |
+ { |
+ case node.ELEMENT_NODE: |
+ result = { |
+ type: "element", |
+ tagName: node.localName, |
+ attributes: [], |
+ children: [] |
+ }; |
+ for (let {name, value} of node.attributes) |
+ result.attributes.push({name, value}); |
+ for (let child = node.firstChild; child; child = child.nextSibling) |
+ { |
+ let serialized = serializeNode(child); |
+ if (serialized) |
+ result.children.push(serialized); |
+ } |
+ break; |
+ case node.TEXT_NODE: |
+ case node.COMMENT_NODE: |
+ result= { |
+ type: node.nodeType == node.TEXT_NODE ? "text" : "comment", |
+ text: node.textContent |
+ }; |
+ break; |
+ } |
+ return result; |
+} |
+ |
+function serializeSelected(message) |
+{ |
+ messageManager.sendAsyncMessage("ElemHideHelper:Response", { |
+ messageId: message.data.messageId, |
+ serialized: serializeNode(state.selectedElement) |
+ }); |
+} |
+ |
function quit() |
{ |
stopSelection(); |
} |
exports.quit = quit; |
function select() |
{ |