forked from kangax/detect-global
-
Notifications
You must be signed in to change notification settings - Fork 1
/
detect-global.src.js
129 lines (107 loc) · 4.18 KB
/
detect-global.src.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
(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 appendControl(el, name) {
var elCheckbox = document.createElement('input');
elCheckbox.type = 'checkbox';
elCheckbox.checked = true;
elCheckbox.id = '__' + name;
var elLabel = document.createElement('label');
elLabel.htmlFor = '__' + name;
elLabel.innerHTML = 'Exclude ' + name + ' properties?';
elLabel.style.marginLeft = '0.5em';
var elWrapper = document.createElement('p');
elWrapper.style.marginBottom = '0.5em';
elWrapper.appendChild(elCheckbox);
elWrapper.appendChild(elLabel);
el.appendChild(elWrapper);
}
function appendAnalyze(el) {
var elAnalyze = document.createElement('button');
elAnalyze.id = '__analyze';
elAnalyze.innerHTML = 'Analyze';
elAnalyze.style.marginTop = '1em';
el.appendChild(elAnalyze);
}
function appendCancel(el) {
var elCancel = document.createElement('a');
elCancel.href = '#';
elCancel.innerHTML = 'Cancel';
elCancel.style.cssText = 'color:#eee;margin-left:0.5em;';
elCancel.onclick = function() {
el.parentNode.removeChild(el);
return false;
};
el.appendChild(elCancel);
}
function initConfigPopup() {
var el = document.createElement('div');
el.style.cssText = 'position:fixed; left:10px; top:10px; width:300px; background:rgba(50,50,50,0.9);' +
'-moz-border-radius:10px; padding:1em; color: #eee; text-align: left;' +
'font-family: "Helvetica Neue", Verdana, Arial, sans serif; z-index: 99999;';
for (var prop in propSets) {
appendControl(el, prop);
}
appendAnalyze(el);
appendCancel(el);
document.body.appendChild(el);
}
function getPropsCount(object) {
var count = 0;
for (var prop in object) {
count++;
}
return count;
}
function shouldDeleteProperty(propToCheck) {
for (var prop in propSets) {
var elCheckbox = document.getElementById('__' + prop);
var isPropInSet = propSets[prop].indexOf(propToCheck) > -1;
if (elCheckbox.checked && 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];
}
}
for (var prop in globalProps) {
if (shouldDeleteProperty(prop)) {
delete globalProps[prop];
}
}
console.dir(globalProps);
console.log('Total number of properties: ' + getPropsCount(globalProps));
}
var propSets = {
'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'.split(' '),
'Scriptaculous': 'Autocompleter Builder Control Draggable Draggables Droppables Effect Sortable SortableObserver Sound Scriptaculous'.split(' '),
'Firebug': 'loadFirebugConsole console _getFirebugConsoleElement _FirebugConsole _FirebugCommandLine _firebug'.split(' '),
'Mozilla': 'Components XPCNativeWrapper XPCSafeJSObjectWrapper getInterface netscape GetWeakReference'.split(' '),
'GoogleAnalytics': 'gaJsHost gaGlobal _gat pageTracker'.split(' ')
};
initConfigPopup();
document.getElementById('__analyze').onclick = analyze;
})();