1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
/* Copyright (c) 2012 The Chromium Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*
* Helper javascript injected whenever a DomMutationEventObserver is created.
*
* This script uses MutationObservers to watch for changes to the DOM, then
* reports the event to the observer using the DomAutomationController. An
* anonymous namespace is used to prevent conflict with other Javascript.
*
* Args:
* automation_id: Automation id used to route DomAutomationController messages.
* observer_id: Id of the observer who will be receiving the messages.
* observer_type: One of 'add', 'remove', 'change', or 'exists'.
* xpath: XPath used to specify the DOM node of interest.
* attribute: If |expected_value| is provided, check if this attribute of the
* DOM node matches |expected value|.
* expected_value: If not null, regular expression to match with the value of
* |attribute| after the mutation.
*/
function(automation_id, observer_id, observer_type, xpath, attribute,
expected_value) {
/* Raise an event for the DomMutationEventObserver. */
function raiseEvent() {
if (window.domAutomationController) {
console.log("Event sent to DomEventObserver with id=" +
observer_id + ".");
window.domAutomationController.sendWithId(
automation_id, "__dom_mutation_observer__:" + observer_id);
}
}
/* Calls raiseEvent if the expected node has been added to the DOM.
*
* Args:
* mutations: A list of mutation objects.
* observer: The mutation observer object associated with this callback.
*/
function addNodeCallback(mutations, observer) {
for (var j=0; j<mutations.length; j++) {
for (var i=0; i<mutations[j].addedNodes.length; i++) {
var node = mutations[j].addedNodes[i];
if (xpathMatchesNode(node, xpath) &&
nodeAttributeValueEquals(node, attribute, expected_value)) {
raiseEvent();
observer.disconnect();
delete observer;
return;
}
}
}
}
/* Calls raiseEvent if the expected node has been removed from the DOM.
*
* Args:
* mutations: A list of mutation objects.
* observer: The mutation observer object associated with this callback.
*/
function removeNodeCallback(mutations, observer) {
var node = firstXPathNode(xpath);
if (!node) {
raiseEvent();
observer.disconnect();
delete observer;
}
}
/* Calls raiseEvent if the given node has been changed to expected_value.
*
* Args:
* mutations: A list of mutation objects.
* observer: The mutation observer object associated with this callback.
*/
function changeNodeCallback(mutations, observer) {
for (var j=0; j<mutations.length; j++) {
if (nodeAttributeValueEquals(mutations[j].target, attribute,
expected_value)) {
raiseEvent();
observer.disconnect();
delete observer;
return;
}
}
}
/* Calls raiseEvent if the expected node exists in the DOM.
*
* Args:
* mutations: A list of mutation objects.
* observer: The mutation observer object associated with this callback.
*/
function existsNodeCallback(mutations, observer) {
if (findNodeMatchingXPathAndValue(xpath, attribute, expected_value)) {
raiseEvent();
observer.disconnect();
delete observer;
return;
}
}
/* Return true if the xpath matches the given node.
*
* Args:
* node: A node object from the DOM.
* xpath: An XPath used to compare with the DOM node.
*/
function xpathMatchesNode(node, xpath) {
var con = document.evaluate(xpath, document, null,
XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null);
var thisNode = con.iterateNext();
while (thisNode) {
if (node == thisNode) {
return true;
}
thisNode = con.iterateNext();
}
return false;
}
/* Returns the first node in the DOM that matches the xpath.
*
* Args:
* xpath: XPath used to specify the DOM node of interest.
*/
function firstXPathNode(xpath) {
return document.evaluate(xpath, document, null,
XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
}
/* Returns the first node in the DOM that matches the xpath.
*
* Args:
* xpath: XPath used to specify the DOM node of interest.
* attribute: The attribute to match |expected_value| against.
* expected_value: A regular expression to match with the node's
* |attribute|. If null the match always succeeds.
*/
function findNodeMatchingXPathAndValue(xpath, attribute, expected_value) {
var nodes = document.evaluate(xpath, document, null,
XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
var node;
while ( (node = nodes.iterateNext()) ) {
if (nodeAttributeValueEquals(node, attribute, expected_value))
return node;
}
return null;
}
/* Returns true if the node's |attribute| value is matched by the regular
* expression |expected_value|, false otherwise.
*
* Args:
* node: A node object from the DOM.
* attribute: The attribute to match |expected_value| against.
* expected_value: A regular expression to match with the node's
* |attribute|. If null the test always passes.
*/
function nodeAttributeValueEquals(node, attribute, expected_value) {
return expected_value == null ||
(node[attribute] && RegExp(expected_value, "").test(node[attribute]));
}
/* Watch for a node matching xpath to be added to the DOM.
*
* Args:
* xpath: XPath used to specify the DOM node of interest.
*/
function observeAdd(xpath) {
window.domAutomationController.send("success");
if (findNodeMatchingXPathAndValue(xpath, attribute, expected_value)) {
raiseEvent();
console.log("Matching node in DOM, assuming it was previously added.");
return;
}
var obs = new MutationObserver(addNodeCallback);
obs.observe(document,
{ childList: true,
attributes: true,
characterData: true,
subtree: true});
}
/* Watch for a node matching xpath to be removed from the DOM.
*
* Args:
* xpath: XPath used to specify the DOM node of interest.
*/
function observeRemove(xpath) {
window.domAutomationController.send("success");
if (!firstXPathNode(xpath)) {
raiseEvent();
console.log("No matching node in DOM, assuming it was already removed.");
return;
}
var obs = new MutationObserver(removeNodeCallback);
obs.observe(document,
{ childList: true,
attributes: true,
subtree: true});
}
/* Watch for the textContent of a node matching xpath to change to
* expected_value.
*
* Args:
* xpath: XPath used to specify the DOM node of interest.
*/
function observeChange(xpath) {
var node = firstXPathNode(xpath);
if (!node) {
console.log("No matching node in DOM.");
window.domAutomationController.send(
"No DOM node matching xpath exists.");
return;
}
window.domAutomationController.send("success");
var obs = new MutationObserver(changeNodeCallback);
obs.observe(node,
{ childList: true,
attributes: true,
characterData: true,
subtree: true});
}
/* Watch for a node matching xpath to exist in the DOM.
*
* Args:
* xpath: XPath used to specify the DOM node of interest.
*/
function observeExists(xpath) {
window.domAutomationController.send("success");
if (findNodeMatchingXPathAndValue(xpath, attribute, expected_value)) {
raiseEvent();
console.log("Node already exists in DOM.");
return;
}
var obs = new MutationObserver(existsNodeCallback);
obs.observe(document,
{ childList: true,
attributes: true,
characterData: true,
subtree: true});
}
/* Interpret arguments and launch the requested observer function. */
function installMutationObserver() {
switch (observer_type) {
case "add":
observeAdd(xpath);
break;
case "remove":
observeRemove(xpath);
break;
case "change":
observeChange(xpath);
break;
case "exists":
observeExists(xpath);
break;
}
console.log("MutationObserver javscript injection completed.");
}
/* Ensure the DOM is loaded before attempting to create MutationObservers. */
if (document.body) {
installMutationObserver();
} else {
window.addEventListener("DOMContentLoaded", installMutationObserver, true);
}
}
|