-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelmer.js
335 lines (252 loc) · 7.47 KB
/
elmer.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
;(function(){
var registrations = [],
cookieLabel = 'Elmer';
/**
* @constructor
*/
function Elmer(funcName) {
fixTraces(); // Try to add back lines if possible
arrayRemove(); // add remove to Array
checkIndexOf(); // add indexOf support in IE
this.name = funcName || 'global'; // should be override by each function that uses it
this.register( this.name );
this.registerCookieObjects();
if( registrations.indexOf( 'ElmerOff' ) == -1 && !this.inCookie( 'ElmerOff' ) ) {
this.overrideConsole();
}
}
/**
* Check cookies to see if new registrations have been made
* @public
*/
Elmer.prototype.registerCookieObjects = function() {
var i, str, length;
str = this.getCookie(cookieLabel);
length = str.length;
str.split(',');
for( var i=0; i<=length; i++ ) {
if(str.indexOf(str) == -1) {
this.register( this.name );
}
}
}
/**
* @private
* @credit https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/IndexOf
*/
function checkIndexOf() {
var t, n, k, len;
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function (searchElement /*, fromIndex */ ) {
"use strict";
if (this == null) {
throw new TypeError();
}
t = Object(this);
len = t.length >>> 0;
if (len === 0) {
return -1;
}
n = 0;
if (arguments.length > 1) {
n = Number(arguments[1]);
if (n != n) { // shortcut for verifying if it's NaN
n = 0;
} else if (n != 0 && n != Infinity && n != -Infinity) {
n = (n > 0 || -1) * Math.floor(Math.abs(n));
}
}
if (n >= len) {
return -1;
}
k = n >= 0 ? n : Math.max(len - Math.abs(n), 0);
for (; k < len; k++) {
if (k in t && t[k] === searchElement) {
return k;
}
}
return -1;
}
}
}
/**
* Add method to Array to remove by item value
* @private
* @credit http://stackoverflow.com/questions/3954438/remove-item-from-array-by-value
*/
function arrayRemove() {
if( !Array.prototype.remove ) {
Array.prototype.remove = function() {
var what, a = arguments, L = a.length, ax;
while (L && this.length) {
what = a[--L];
while ((ax = this.indexOf(what)) !== -1) {
this.splice(ax, 1);
}
}
return this;
};
}
}
/**
* Unfortunately, with abstraction of console, we lose line/number traces. We try to correct them with this by
* passing __line and __file as params to console[function]. If you really need to see the numbers, turn off
* Elmer by adding ElmerOff to setCookie()
*
* @private
*/
function fixTraces() {
if( !window.__stack ) {
Object.defineProperty(window, '__stack', {
get: function(){
var orig, err, stack;
orig = Error.prepareStackTrace;
Error.prepareStackTrace = function(_, stack){ return stack; };
err = new Error;
Error.captureStackTrace(err, arguments.callee);
stack = err.stack;
Error.prepareStackTrace = orig;
return stack;
}
});
}
if( !window.__file ) {
Object.defineProperty(window, '__file', {
get: function(){
return '[[ on: ' +__stack[1].getFileName();
}
});
}
if( !window.__line ) {
Object.defineProperty(window, '__line', {
get: function(){
return 'line: ' + __stack[1].getLineNumber() + ' ]]';
}
});
}
}
/**
* @public
*/
Elmer.prototype.register = function(funcName) {
if( registrations.indexOf(funcName) == -1 ) {
registrations.push(funcName);
this.name = funcName;
} else {
console.info('already registered');
}
}
/**
* @public
*/
Elmer.prototype.log = function() {
var args, name = this.name;
if( registrations.indexOf( name ) > -1 && !this.inCookie(name) ) {
args = Array.prototype.splice.call(arguments,0);
args.unshift('[' + name + ']');
window.console.on = true;
window.console.log.apply(console, args);
}
};
/**
* @public
*/
Elmer.prototype.overrideConsole = function() {
var _log = window.console.log;
if( !window.console.overriden ) {
window.console.log = function(str) {
if( window.console.on ) {
window.console.on = false;
return _log.apply(this, arguments);
}
};
window.console.overriden = true;
}
}
/**
* See if funcName is in cookie or not
* @public
*/
Elmer.prototype.inCookie = function(funcName) {
var str = this.getCookie(cookieLabel);
str.split(',');
return (str.indexOf(funcName) > -1) ? true : false;
}
/**
* Enable Elmer. Requires refresh to see it enabled.
* @public
*/
Elmer.prototype.enable = function(funcName) {
var value, cookie = this.getCookie(cookieLabel);
if( typeof funcName == 'undefined' ) {
value = cookie.replace(' ElmerOff,','');
this.setCookie(cookieLabel, value);
registrations.remove('ElmerOff');
} else {
this.appendToCookie(funcName);
}
}
/**
* Disable Elmer. Good for when you need accurate line numbers.
* Requires refresh.
* @private
*/
Elmer.prototype.disable = function() {
this.appendToCookie('ElmerOff');
console.info("Don't forget to refresh the page");
}
Elmer.prototype.appendToCookie = function(str) {
var value = 'ElmerOff',
cookie = this.getCookie(cookieLabel);
if( !this.inCookie(str) ) {
cookie += str+',';
this.setCookie(cookieLabel, value);
registrations.push(value);
} else {
console.warn('value already in cookie');
}
}
/**
* @public
* @param {String} name Name of the cookie
* @param {String} value Value of the cookie
* @param {String} expire Expiration in days
*/
Elmer.prototype.setCookie = function (name, value, expire) {
var exdate, cookiesValue;
exdate = new Date();
exdate.setDate( exdate.getDate() + expire );
cookiesValue = escape(value) + ((expire==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie = name + "=" + cookiesValue;
}
/**
* Get the value of the Elmer cookie
* @public
*/
Elmer.prototype.getCookie = function () {
var i,x,y,cookies, name = cookieLabel;
cookies = document.cookie.split(";");
for (i=0; i<cookies.length; i++) {
x = cookies[i].substr( 0,cookies[i].indexOf("=") );
y = cookies[i].substr( cookies[i].indexOf("=")+1 );
x = x.replace(/^\s+|\s+$/g,"");
if(x == name) {
return unescape(y);
}
}
}
/**
* Get the name given to this instance
* @public
*/
Elmer.prototype.getName = function() {
return this.name;
}
window.Elmer = Elmer;
})();
/*
TODO:
+ if cookie Elmer.off = true, use default console (elmer.log=console.log) to keep line numbers
+ create override console via a loop for less code
+ enable/disable via hashes
*/