Created
November 25, 2016 15:35
-
-
Save BeyondEvil/9a8e8910252b568c0fb88b65e74d6cf7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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; | |
} | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@BeyondEvil Try to change
mutationObserver = {...}
towindow.mutationObserver = {...}
(in the JS file) and initialize it like thiswindow.mutationObserver.init();
.