Skip to content

Instantly share code, notes, and snippets.

@keerts
Created February 16, 2012 22:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keerts/1848285 to your computer and use it in GitHub Desktop.
Save keerts/1848285 to your computer and use it in GitHub Desktop.
detectGlobals Javascript helper
var detectGlobals = (function () {
function getPropertyDescriptors(object) {
var props = {};
for (var prop in object) {
props[prop] = {
type: typeof object[prop],
value: object[prop]
}
}
return props;
}
function getCleanWindow() {
var elIframe = document.createElement("iframe");
elIframe.style.display = "none";
document.body.appendChild(elIframe);
elIframe.src = "about:blank";
return elIframe.contentWindow;
}
function shouldDeleteProperty(propToCheck) {
for (var prop in propSets) {
var isPropInSet = propSets[prop].indexOf(propToCheck) > -1;
if (isPropInSet) {
return true;
}
}
}
function analyze() {
var global = (function() {
return this;
})(),
globalProps = getPropertyDescriptors(global),
cleanWindow = getCleanWindow();
for (var prop in cleanWindow) {
if (globalProps[prop]) {
delete globalProps[prop];
}
}
var exposedGlobals = [];
for (var prop in globalProps) {
if (shouldDeleteProperty(prop)) {
delete globalProps[prop];
}
else {
exposedGlobals.push(prop);
}
}
return exposedGlobals;
}
var propSets = {
"Safari": [],
"Chrome": [],
"Mozilla": ["Components", "XPCNativeWrapper", "XPCSafeJSObjectWrapper", "getInterface", "netscape", "GetWeakReference"],
"DetectGlobals": ["detectGlobals"],
"Prototype": ["$$", "$A", "$F", "$H", "$R", "$break", "$continue", "$w", "Abstract", "Ajax", "Class", "Enumerable", "Element", "Field", "Form", "Hash", "Insertion", "ObjectRange", "PeriodicalExecuter", "Position", "Prototype", "Selector", "Template", "Toggle", "Try"],
"jQuery": ["$", "jQuery"],
"Underscore": ["_"],
"Backbone": ["Backbone"],
"Scriptaculous": ["Autocompleter", "Builder", "Control", "Draggable", "Draggables", "Droppables", "Effect", "Sortable", "SortableObserver", "Sound", "Scriptaculous"],
"Firebug": ["loadFirebugConsole", "console", "_getFirebugConsoleElement", "_FirebugConsole", "_FirebugCommandLine", "_firebug"],
"GoogleAnalytics": ["gaJsHost", "gaGlobal", "_gat", "_gaq", "pageTracker"],
"Jasmine & Sinon": ["jasmine", "it", "xit", "waits", "waitsFor", "describe", "xdescribe", "spyOn", "spyOnEvent", "runs", "afterEach", "beforeEach", "sandbox", "setFixtures", "readFixtures", "loadFixtures", "preloadFixtures", "expect", "sinon", "sinonJasmine", "isCommonJS"],
"Sticky": ["StickyStore"]
};
return {
analyze: analyze
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment