To prevent your friends from tampering with the site's HTML through inspecting the page, you can implement a clever solution using a MutationObserver.
Take a look at this example to see how you can be alerted when changes are made to the page's HTML:
window.addEventListener('load', function() {
const observer = new MutationObserver(function() {
console.log('Stop right there!');
})
observer.observe(document.body, {
characterData: true,
childList: true,
attributes: false,
subtree: true
});
})
<h2>Go ahead and try editing!</h2>
<p>Lists can contain nested lists:</p>
<ul>
<li>Coffee</li>
<li>Tea
<ul>
<li>Black tea</li>
<li>Green tea</li>
</ul>
</li>
<li>Milk</li>
</ul>
If you wish to undo any modifications made to the HTML, you could save the initial body's innerHTML
on load and then revert it upon change.
While this can be amusing, please refrain from using it on an actual website as completely replacing the innerHTML
may result in breaking your site. This is because all elements are essentially destroyed and recreated, leading to loss of previously attached event listeners.
window.addEventListener('load', function() {
const startHTML = document.body.innerHTML;
const observer = new MutationObserver(function() {
if(document.body.innerHTML != startHTML){
alert('Nice attempt, but no.');
document.body.innerHTML = startHTML;
}
})
observer.observe(document.body, {
characterData: true,
childList: true,
attributes: false,
subtree: true
});
})
<h2>Go ahead and try editing!</h2>
<p>Lists can contain nested lists:</p>
<ul>
<li>Coffee</li>
<li>Tea
<ul>
<li>Black tea</li>
<li>Green tea</li>
</ul>
</li>
<li>Milk</li>
</ul>