Last active
April 6, 2023 22:24
-
-
Save prmichaelsen/c5ce3a77480629312c1e917a83db2b2c 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
function hashString(s) { | |
let hashValue = 0; | |
for (let i = 0; i < s.length; i++) { | |
hashValue = (hashValue * 31 + s.charCodeAt(i)) % 4294967296; | |
} | |
let bucket = Math.floor((hashValue / 4294967296) * 8); | |
return bucket; | |
} | |
// Select the target node | |
const targetNode = document.body; | |
// Options for the observer (which mutations to observe) | |
const observerOptions = { | |
childList: true, | |
attributes: true, | |
subtree: true | |
}; | |
// Create a new observer instance | |
const observer = new MutationObserver(function(mutationsList, observer) { | |
// Handle mutations here | |
for (let mutation of mutationsList) { | |
if (mutation.type === 'childList') { | |
// Call a function to update the DOM, but only if the mutation | |
// wasn't caused by this same function in a previous iteration | |
if (!mutation.target.classList.contains('dom-updated')) { | |
updateDOM(mutation); | |
} | |
} | |
} | |
}); | |
// Start observing the target node for configured mutations | |
observer.observe(targetNode, observerOptions); | |
// Function to update the DOM | |
function updateDOM(mutation) { | |
// Perform custom DOM updates here | |
// ... | |
// Add a class to the mutated element to mark it as updated | |
mutation.target.classList.add('dom-updated'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment