Created
May 15, 2025 15:54
-
-
Save ceaksan/2b94ff59b17da0ffaa0f4d1ad160594d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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