Skip to content

Instantly share code, notes, and snippets.

@fodra
Created June 7, 2018 05:52
Show Gist options
  • Save fodra/ea61d42ca8289137e762166816b78568 to your computer and use it in GitHub Desktop.
Save fodra/ea61d42ca8289137e762166816b78568 to your computer and use it in GitHub Desktop.
A MutationObserver example.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>title</title>
</head>
<body>
<div id="target">
<div id="inner">
stuff
</div>
</div>
<script type="text/javascript">
(() => {
console.log("starting the script");
let pagination = this.document.getElementById('target');
let config = {childList: true, subtree: true};
let handleSearchResults = (param) => {
console.log("search results done", param, this);
}
let observer = new MutationObserver(handleSearchResults.bind(this));
observer.observe(pagination, config);
})();
</script>
</body>
</html>
<!--
1. Open this page in a web browser preferably chrome.
2. Open the console.
3. Type into the console.
inner = document.getElementById('inner');
inner.textContent = "modified"; // this should trigger the handleSearchResults function
-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment