Skip to content

Instantly share code, notes, and snippets.

@mindplay-dk
Created September 7, 2020 13:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mindplay-dk/767a5313b0052d6daf2b135fdecd775f to your computer and use it in GitHub Desktop.
Save mindplay-dk/767a5313b0052d6daf2b135fdecd775f to your computer and use it in GitHub Desktop.
Monkey-patching/polyfill detector

This script tries to detect monkey-patching, polyfills and other hacks/overrides in the browser.

Paste it into the Chrome (or Edge) console and press ENTER.

Note that this may give false positives for window.location, window.fetch and window.length - this appears to be because these properties aren't correctly reflected by the native browser implementations, but if you know how to fix that, please post a comment.

(() => {
const iframe = document.createElement('iframe');
iframe.src = 'javascript:;';
document.getElementsByTagName('body')[0].appendChild(iframe);
nativeWindow = iframe.contentWindow;
const seen = new Set();
function check(parent, nativeParent, prefix, deep) {
for (const name of Object.getOwnPropertyNames(parent)) {
const key = `${prefix}.${name}`;
if (seen.has(key)) continue;
seen.add(key);
try { parent[name].toString(); nativeParent[name].toString(); } catch { continue }
const getter = Object.getOwnPropertyDescriptor(parent, name).get;
const nativeGetter = Object.getOwnPropertyDescriptor(nativeParent, name).get;
if (nativeGetter ? getter.toString() !== nativeGetter.toString() : parent[name].toString() !== nativeParent[name].toString()) {
console.log(`⚠ ${key}`, nativeGetter ? { getter, nativeGetter } : { value: parent[name], nativeValue: nativeParent[name] });
if (deep && parent[name].prototype) {
check(parent[name].prototype, nativeParent[name].prototype, name);
}
}
}
}
check(window, nativeWindow, "window", true);
iframe.parentNode.removeChild(iframe);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment