Skip to content

Instantly share code, notes, and snippets.

@emileber
Last active February 18, 2019 16:33
Show Gist options
  • Save emileber/00bd96b411f39cb893142a41e4a7e5cf to your computer and use it in GitHub Desktop.
Save emileber/00bd96b411f39cb893142a41e4a7e5cf to your computer and use it in GitHub Desktop.
Check for globals added by other scripts after the page is loaded.
/**
* Check for globals added by other scripts after the page is loaded.
* Place this at the top of `<head>`.
*
* Uses `getOwnPropertyNames` to get the non-enumerable properties as well.
*/
(function(getGlobalKeys) {
var keys = getGlobalKeys();
window.addEventListener("load", function() {
var diff = getGlobalKeys().filter(function(key) {
return keys.indexOf(key) === -1;
});
console.log(
"Unusual globals (%d): %o",
diff.length,
diff.reduce(function(win, key) {
win[key] = window[key];
return win;
}, {})
);
});
})(function() {
return Object.getOwnPropertyNames(window);
});
/**
* Check for globals added by other scripts after the page is loaded.
* Place this at the top of `<head>`.
*
* Uses `getOwnPropertyNames` to get the non-enumerable properties as well.
*/
((getGlobalKeys) => {
const keys = getGlobalKeys();
window.addEventListener("load", () => {
const diff = getGlobalKeys().filter(key => keys.indexOf(key) === -1);
console.log(
"Unusual globals (%d): %o",
diff.length,
diff.reduce((win, key) => {
win[key] = window[key];
return win;
}, {})
);
});
})(() => Object.getOwnPropertyNames(window));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment