OLD | NEW |
| (Empty) |
1 <script> | |
2 class DOMAgent { | |
3 constructor(delegate) { | |
4 this.enabled = false; | |
5 this.delegate_ = delegate; | |
6 this.nextNodeId_ = 1; | |
7 this.nodeToId_ = new Map(); | |
8 this.idToNode_ = new Map(); | |
9 this.observer_ = null; | |
10 Object.preventExtensions(this); | |
11 } | |
12 | |
13 getIdForNode_(node) { | |
14 if (this.nodeToId_.has(node)) | |
15 return this.nodeToId_.get(node); | |
16 var id = this.nextNodeId_++; | |
17 this.nodeToId_.set(node, id); | |
18 this.idToNode_.set(id, node); | |
19 return id; | |
20 } | |
21 | |
22 getNodeForId(nodeId) { | |
23 return this.idToNode_.get(nodeId); | |
24 } | |
25 | |
26 serializeChildren_(node) { | |
27 var children = []; | |
28 for (var child = node.firstChild; child; child = child.nextSibling) { | |
29 var record = this.serializeNode_(child); | |
30 if (record) | |
31 children.push(record); | |
32 } | |
33 return children; | |
34 } | |
35 | |
36 serializeAttributes_(element) { | |
37 var attributes = []; | |
38 var attrs = element.getAttributes(); | |
39 for (var i = 0; i < attrs.length; ++i) { | |
40 var attr = attrs[i]; | |
41 attributes.push(attr.name); | |
42 attributes.push(attr.value); | |
43 } | |
44 return attributes; | |
45 } | |
46 | |
47 serializeNode_(node) { | |
48 var id = this.getIdForNode_(node); | |
49 | |
50 var record = { | |
51 nodeId: id, | |
52 }; | |
53 | |
54 var isContainer = false; | |
55 | |
56 if (node instanceof Element) { | |
57 isContainer = true; | |
58 record.nodeType = 1; | |
59 record.nodeName = node.tagName; | |
60 record.localName = node.tagName; | |
61 record.nodeValue = ""; | |
62 record.attributes = this.serializeAttributes_(node); | |
63 } else if (node instanceof Text) { | |
64 record.nodeType = 3; | |
65 record.nodeName = "#text"; | |
66 var nodeValue = node.data; | |
67 if (!nodeValue.trim()) | |
68 return null; | |
69 record.nodeValue = nodeValue; | |
70 } else if (node instanceof Document) { | |
71 isContainer = true; | |
72 record.nodeType = 9; | |
73 record.nodeName = "#document"; | |
74 record.localName = ""; | |
75 record.nodeValue = ""; | |
76 record.documentURL = node.URL; | |
77 record.baseURL = node.baseURI; | |
78 } else if (node instanceof DocumentFragment) { | |
79 isContainer = true; | |
80 record.nodeType = 11; | |
81 record.nodeName = "#document-fragment"; | |
82 record.localName = ""; | |
83 record.nodeValue = ""; | |
84 } else { | |
85 console.log("Unknown node type"); | |
86 return null; | |
87 } | |
88 | |
89 if (isContainer) { | |
90 var children = this.serializeChildren_(node); | |
91 if (children.length) { | |
92 record.childNodeCount = children.length; | |
93 record.children = children; | |
94 } | |
95 } | |
96 | |
97 return record; | |
98 } | |
99 | |
100 enable() { | |
101 this.enabled = true; | |
102 this.observer_ = new MutationObserver(this.mutationCallback_.bind(this)); | |
103 this.observer_.observe(document, { | |
104 childList: true, | |
105 attributes: true, | |
106 characterData: true, | |
107 subtree : true, | |
108 }); | |
109 } | |
110 | |
111 getDocument() { | |
112 return { | |
113 root: this.serializeNode_(document), | |
114 }; | |
115 } | |
116 | |
117 hideHighlight() { | |
118 } | |
119 | |
120 highlightNode() { | |
121 } | |
122 | |
123 mutationCallback_(mutationRecords) { | |
124 for (var i = 0; i < mutationRecords.length; ++i) { | |
125 var record = mutationRecords[i]; | |
126 var type = record.type; | |
127 var target = record.target; | |
128 var nodeId = this.getIdForNode_(target); | |
129 if (type == "attributes") { | |
130 var attributeName = record.attributeName; | |
131 if (target.hasAttribute(attributeName)) { | |
132 this.delegate_.sendMessage("DOM.attributeModified", { | |
133 nodeId: nodeId, | |
134 name: attributeName, | |
135 value: target.getAttribute(attributeName), | |
136 }); | |
137 } else { | |
138 this.delegate_.sendMessage("DOM.attributeRemoved", { | |
139 nodeId: nodeId, | |
140 name: attributeName, | |
141 }); | |
142 } | |
143 } else if (type == "characterData") { | |
144 this.delegate_.sendMessage("DOM.characterDataModified", { | |
145 nodeId: nodeId, | |
146 characterData: target.data, | |
147 }); | |
148 } else if (type == "childList") { | |
149 // FIXME: If this subtree isn't expanded, we only need to send across th
e | |
150 // {"method":"DOM.childNodeCountUpdated","params":"nodeId":648,"childNod
eCount":2} | |
151 | |
152 Array.prototype.forEach.call(record.removedNodes, function(node) { | |
153 this.delegate_.sendMessage("DOM.childNodeRemoved", { | |
154 parentNodeId: nodeId, | |
155 nodeId: this.getIdForNode_(node), | |
156 }); | |
157 }.bind(this)); | |
158 | |
159 Array.prototype.forEach.call(record.addedNodes, function(node) { | |
160 var previousNodeId = node.previousSibling ? this.getIdForNode_(node.pr
eviousSibling) : 0; | |
161 this.delegate_.sendMessage("DOM.childNodeInserted", { | |
162 parentNodeId: nodeId, | |
163 previousNodeId: previousNodeId, | |
164 node: this.serializeNode_(node), | |
165 }); | |
166 }.bind(this)); | |
167 } | |
168 } | |
169 } | |
170 } | |
171 | |
172 module.exports = DOMAgent; | |
173 </script> | |
OLD | NEW |