Skip to content

Commit e91e27e

Browse files
committed
Merge mut-observer
2 parents fcd9c68 + 71379dd commit e91e27e

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

packages/selenium-ide/src/content/record-api.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ function Recorder(window) {
2727
}
2828

2929
Recorder.eventHandlers = {};
30+
Recorder.mutationObservers = {};
3031
Recorder.addEventHandler = function(handlerName, eventName, handler, options) {
3132
handler.handlerName = handlerName;
3233
if (!options) options = false;
@@ -37,6 +38,13 @@ Recorder.addEventHandler = function(handlerName, eventName, handler, options) {
3738
this.eventHandlers[key].push(handler);
3839
};
3940

41+
Recorder.addMutationObserver = function(observerName, callback, config) {
42+
const observer = new MutationObserver(callback);
43+
observer.observerName = observerName;
44+
observer.config = config;
45+
this.mutationObservers[observerName] = observer;
46+
};
47+
4048
Recorder.prototype.parseEventKey = function(eventKey) {
4149
if (eventKey.match(/^C_/)) {
4250
return { eventName: eventKey.substring(2), capture: true };
@@ -59,6 +67,10 @@ Recorder.prototype.attach = function() {
5967
this.eventListeners[eventKey].push(handlers[i]);
6068
}
6169
}
70+
for (let observerName in Recorder.mutationObservers) {
71+
const observer = Recorder.mutationObservers[observerName];
72+
observer.observe(this.window.document.body, observer.config);
73+
}
6274
this.attached = true;
6375
}
6476
};
@@ -72,6 +84,10 @@ Recorder.prototype.detach = function() {
7284
this.window.document.removeEventListener(eventName, this.eventListeners[eventKey][i], capture);
7385
}
7486
}
87+
for (let observerName in Recorder.mutationObservers) {
88+
const observer = Recorder.mutationObservers[observerName];
89+
observer.disconnect();
90+
}
7591
this.eventListeners = {};
7692
this.attached = false;
7793
};

packages/selenium-ide/src/content/record.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -389,21 +389,30 @@ let mouseoutLocator = undefined;
389389
// © Shuo-Heng Shih, SideeX Team
390390
Recorder.addEventHandler("mouseOut", "mouseout", function(event) {
391391
if (mouseoutLocator !== null && event.target === mouseoutLocator) {
392-
record("mouseOut", locatorBuilders.buildAll(event.target), '');
392+
record("mouseOut", locatorBuilders.buildAll(event.target), "");
393393
}
394394
mouseoutLocator = undefined;
395395
}, true);
396396
// END
397397

398-
// © Shuo-Heng Shih, SideeX Team
399-
Recorder.addEventHandler("mouseOver", "DOMNodeInserted", function(event) {
398+
Recorder.addMutationObserver("DOMNodeInserted", function(mutations) {
400399
if (pageLoaded === true && window.document.documentElement.getElementsByTagName("*").length > nowNode) {
400+
// Get list of inserted nodes from the mutations list to simulate 'DOMNodeInserted'.
401+
const insertedNodes = mutations.reduce((nodes, mutation) => {
402+
if (mutation.type === "childList") {
403+
nodes.push.apply(nodes, mutation.addedNodes);
404+
}
405+
return nodes;
406+
}, []);
407+
// If no nodes inserted, just bail.
408+
if (!insertedNodes.length) {
409+
return;
410+
}
411+
401412
if (scrollDetector) {
402413
//TODO: fix target
403414
record("runScript", [
404-
[
405-
["window.scrollTo(0," + window.scrollY + ")"]
406-
]
415+
["window.scrollTo(0," + window.scrollY + ")" ]
407416
], "");
408417
pageLoaded = false;
409418
setTimeout(function() {
@@ -419,8 +428,7 @@ Recorder.addEventHandler("mouseOver", "DOMNodeInserted", function(event) {
419428
mouseoverLocator = undefined;
420429
}
421430
}
422-
}, true);
423-
// END
431+
}, { childList: true, subtree: true });
424432

425433
// © Shuo-Heng Shih, SideeX Team
426434
let readyTimeOut = null;

0 commit comments

Comments
 (0)