Skip to content

Instantly share code, notes, and snippets.

@BeyondEvil
Created November 25, 2016 15:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BeyondEvil/9a8e8910252b568c0fb88b65e74d6cf7 to your computer and use it in GitHub Desktop.
Save BeyondEvil/9a8e8910252b568c0fb88b65e74d6cf7 to your computer and use it in GitHub Desktop.
/*
Mutation Observer object
Used to detect and inspect changes to the DOM
See:
https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver
*/
mutationObserver = {
observer: null,
mutationObserverInit: {
childList: true,
attributes: true,
characterData: true,
subtree: true,
attributeOldValue: true,
characterDataOldValue: true,
attributeFilter: []
},
mutationRecords: [],
init: function () {
if (!this.observer) {
this.observer = new MutationObserver(this.mutationRecordCallback.bind(this));
this.isRunning = false;
}
},
hasMutationRecord: function () {
return this.mutationRecords.length > 0;
},
mutationRecordCallback: function (records, theObserver) {
this.mutationRecords = records;
},
deleteMutationRecord: function () {
this.mutationRecords = [];
},
observe: function (node) {
if (!node) {
node = document;
}
this.observer.observe(node, this.mutationObserverInit);
this.isRunning = true;
},
isObserverRunning: function () {
return this.isRunning;
},
disconnect: function (kill) {
this.deleteMutationRecord();
this.observer.disconnect();
this.isRunning = false;
if (kill) {
this.observer = null;
}
}
};
class MutationObserver(object):
"""
Class for observing changes in the DOM.
"""
def __init__(self, driver):
self._driver = driver
def init_observer(self):
"""
Initialize the Mutation Observer class.
:return: None
"""
script = "mutationObserver.init();"
# Inject the Mutation Observer object
self._driver.execute_sync_script(get_mutation_observer()) # get_mutation_observer() reads the mutation_observer.js for the wrapper
# Initialize it
self._driver.execute_sync_script(script) # this errors out with: WebDriverException: Message: ReferenceError: mutationObserver is not defined
@bayandin
Copy link

bayandin commented Nov 25, 2016

@BeyondEvil Try to change mutationObserver = {...} to window.mutationObserver = {...} (in the JS file) and initialize it like this window.mutationObserver.init();.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment