Reputation: 3226
I am trying to delete some scripts on page load via the use of TamperMonkey.
I am using MutationObserver to figure out when a script event loads and if so delete it and place my script on it.
// ==UserScript==
// @name Block Scripts Before Execution
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Run a script before any other scripts on the page
// @author You
// @match *://*/*
// @grant none
// @run-at document-start
// ==/UserScript==
(function() {
'use strict';
// Use MutationObserver to watch for script elements being added to the DOM
const observer = new MutationObserver((mutationsList, observer) => {
for (let mutation of mutationsList) {
if (mutation.type === 'childList') {
for (let node of mutation.addedNodes) {
if (node.tagName === 'SCRIPT') {
console.log('Blocked script:', node.src || 'inline script');
node.remove(); // Block the script by removing it
}
}
}
}
});
// Start observing the DOM for new script tags
observer.observe(document.head, {
childList: true,
subtree: true
});
})();
Now my question is that will the callback be called for each and every DOM node insertion ?
Or will it be batched and then called ?
Because it is imperative that I replace the script before the window.DOMContentLoaded
is fired.
Upvotes: 0
Views: 49