A simple library to stop <script> tags from being added to your
website.
Why would you want to do that?
- Because YOU can.
- Because it's YOUR website and YOU decide what runs.
- Because YOU don't want these pesky browser extensions to inject scripts into YOUR website and hog all the precious resources like CPU.
- etc.
Copy and paste the contents of deny.js to the top of your HTMl file in
a <script> tag. Here's the whole thing for you.
<!DOCTYPE html>
<html lang="en">
<head>
<script>
(function deny(config = {}) {
const log = config.log || function() {}
const allow = config.allow || function () { return false }
const observer = new MutationObserver(function (records, observer) {
for (const record of records) {
record.addedNodes.forEach(function (node) {
if (!(node instanceof HTMLScriptElement)) {
return
}
if (allow(node)) {
log('allowing script', node)
return
}
log('denying script', node)
node.remove()
})
}
})
/** @type {MutationObserverInit} */
const options = {
subtree: true,
childList: true,
}
observer.observe(document, options)
})()
</script>
<title>MY Website</title>
<script>
// a cheeky little test
document.addEventListener('DOMContentLoaded', function() {
document.getElementById('textfield').value = 'No'
})
</script>
</head>
<body>
<label>
Did it work?
<input id="textfield" type="text" value="Yes" readonly>
</label>
<script>
// Another test
document.getElementById('textfield').value = 'Maybe not?'
</script>
</body>
</html>Remember, it must be the FIRST script your website loads, so that it can decide the fate of every other script.
Only two optional configuration options are available:
log- A function used for loggingallow- A function to filter out which<script>s you want. If it returns false then the<script>is not loaded.
