Methods for preventing unauthorized DOM mutations
JavaScript HTML
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
test
COPYING.md
README.md
changelog.md
mutaprophylaxis-ese.js
mutaprophylaxis.js

README.md

Mutaprophylaxis

Mutaprophylaxis (IPA: /ˌmjutʌproʊfɪˈlæksɪs/) is a JavaScript library that implements methods to prevent unauthorized DOM mutations.

Getting Started

  • Download Mutaprophylaxis.

  • Include Mutaprophylaxis in your webpage with <script type="application/javascript" src="mutaprophylaxis.js"></script>.

  • Call MutationEvent.protect(node) on nodes that you wish to protect from mutations.

    It is recommended that you do this immediately after the node you wish to protect is in the DOM, by placing a script directly after the node. If you wait for onload or DOMContentLoaded, it may already be too late to protect the node, as malicious scripts or add-ons may have already gotten to it.

Usage

Mutaprophylaxis is intended for protecting nodes from DOM mutations triggered by malicious scripts and browser add-ons that interact with websites. To be honest, I predict that the main use of Mutaprophylaxis will be being used in combination with inline display: block !important styles to create ad blocker-blocking ads.

Tested Compatible Browsers

  • Firefox 3.6
  • †Safari 5
  • †Google Chrome 5
  • Opera 10.60
  • ‡Internet Explorer 9 Platform Preview #4

† WebKit doesn't support the DOMAttrModified event or mutation events on nodes not currently in a document.

‡ Internet Explorer 9 Platform Preview #4 only supports reverting DOMCharacterDataModified events.

Enhanced Security Edition

Mutaprophylaxis Enhanced Security Edition (ESE) is a version of Mutaprophylaxis that is impossible to circumvent. Download Mutaprophylaxis ESE and follow the instructions in the file, if you want extra security. Make sure to only include the ESE script directly after the nodes you wish to protect, so you can reference the nodes in your code right away.

API

Strong and emphasized text has titles (which can be viewed by hovering your cursor over them) containing their type. If they are functions, it is their return type.

MutationEvent.protect(node)
Reverts all mutation events that ever occur on node and returns a function that authorizes all mutations done by the function passed to it. For example, refer to the following example code.
var authorizedMutator = MutationEvent.protect(document);
authorizedMutator(function () {
    document.body.appendChild(document.createElement("p"))
        .appendChild(document.createTextNode("These DOM mutations were authorized."))
});
event.revert()
Attempts to revert a MutationEvent. Note that a MutationEvent cannot be reverted during an the execution of a MutationEvent event listener, so you should call it in a zero-timeout. For example, refer to the following example code.
var allowMutations = false;
comments.addEventListener("DOMCharacterDataModified", function (event) {
    if (!allowMutations) {
        setTimeout(function () {
            allowMutations = true;
            event.revert();
            allowMutations = false;
        }, 0);
    }
}, false);
Also note that generic DOMSubtreeModified events cannot be reverted, as they do not contain enough relevant information needed for reverting the event.

![Tracking image](//in.getclicky.com/212712ns.gif =1x1)