Skip to content

Instantly share code, notes, and snippets.

@ceaksan
Created May 15, 2025 15:54
Show Gist options
  • Save ceaksan/2b94ff59b17da0ffaa0f4d1ad160594d to your computer and use it in GitHub Desktop.
Save ceaksan/2b94ff59b17da0ffaa0f4d1ad160594d to your computer and use it in GitHub Desktop.
// Do this VERY EARLY in your <head>
(function() {
if (window.MutationObserver) {
const OriginalMutationObserver = window.MutationObserver;
let observerInstanceCount = 0;
window.MutationObserver = function(callback) {
observerInstanceCount++;
console.warn(`[DETECTOR] MutationObserver #${observerInstanceCount} created. Potential for script interference. Stack:`, new Error().stack);
const observer = new OriginalMutationObserver(callback);
const originalObserve = observer.observe;
const originalDisconnect = observer.disconnect;
observer.observe = function(target, options) {
console.warn(`[DETECTOR] MutationObserver #${observerInstanceCount} is observing:`, target, 'with options:', options);
if (target === document.documentElement || target === document.body || target === document.head) {
console.warn(`[DETECTOR] MutationObserver #${observerInstanceCount} is observing a critical part of the document (html, head, or body). This could impact script loading/execution.`);
}
if (options && (options.childList || options.subtree)) {
console.warn(`[DETECTOR] MutationObserver #${observerInstanceCount} is monitoring childList or subtree changes. This is commonly used to detect added scripts.`);
}
return originalObserve.call(this, target, options);
};
observer.disconnect = function() {
console.log(`[DETECTOR] MutationObserver #${observerInstanceCount} disconnected.`);
return originalDisconnect.call(this);
}
return observer;
};
// Optionally, also wrap the prototype methods if observers were created *before* this script ran
// (though this is less likely to catch the problematic ones if this script is truly first)
// OriginalMutationObserver.prototype.observe = ... (similar wrapping)
} else {
console.log("[DETECTOR] MutationObserver API not found in this browser.");
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment