forked from jslint-org/jslint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
intercept.js
102 lines (82 loc) · 3.28 KB
/
intercept.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
// intercept.js
// 2012-05-09
// This file makes it possible for JSLint to run as an ADsafe widget by
// adding lib features.
// It provides a JSON cookie facility. Each widget is allowed to create a
// single JSON cookie.
// It also provides a way for the widget to call JSLint. The widget cannot
// call JSLint directly because it is loaded as a global variable. I don't
// want to change that because other versions of JSLint depend on that.
// And it provides access to the syntax tree that JSLint constructed.
/*jslint nomen: true, unparam: true */
/*global ADSAFE, document, JSLINT */
/*properties
___nodes___, _intercept, cookie, data, edition, error_report, get, getTime,
indexOf, innerHTML, jslint, length, now, parse, properties_report, property,
replace, report, set, setTime, slice, stringify, toGMTString, value
*/
ADSAFE._intercept(function (id, dom, lib, bunch) {
'use strict';
// Give every widget access to a JSON cookie. The name of the cookie will be
// the same as the id of the widget.
lib.cookie = {
get: function () {
// Get the raw cookie. Extract this widget's cookie, and parse it.
var c = ' ' + document.cookie + ';',
s = c.indexOf((' ' + id + '=')),
v;
try {
if (s >= 0) {
s += id.length + 2;
v = JSON.parse(c.slice(s, c.indexOf(';', s)));
}
} catch (ignore) {}
return v;
},
set: function (value) {
// Set a cookie. It must be under 2000 in length. Escapify equal sign
// and semicolon if necessary.
var d,
j = JSON.stringify(value)
.replace(/[=]/g, '\\u003d')
.replace(/[;]/g, '\\u003b');
if (j.length < 2000) {
d = new Date();
d.setTime(d.getTime() + 1e9);
document.cookie = id + "=" + j + ';expires=' + d.toGMTString();
}
}
};
});
ADSAFE._intercept(function (id, dom, lib, bunch) {
'use strict';
// Give only the JSLINT_ widget access to the JSLINT function.
// We add a jslint function to its lib that calls JSLINT and
// then gets the reports, and stuffs the results into nodes
// provided by the widget. We do not trust a widget to stuff
// just any HTML content.
// We also add an edition function to the lib that gives the
// widget access to the current edition string.
var now = Date.now || function () {
return new Date().getTime();
};
if (id === 'JSLINT_') {
lib.jslint = function (source, options, errors, report, properties, edition) {
var after, before = now(), data, errtext, protext, retext;
JSLINT(source, options);
data = JSLINT.data();
errtext = JSLINT.error_report(data);
retext = JSLINT.report(data);
protext = JSLINT.properties_report(JSLINT.property);
after = now();
edition.value(((after - before) / 1000) + ' seconds.');
errors.___nodes___[0].innerHTML = errtext;
report.___nodes___[0].innerHTML = retext;
properties.value(protext);
return errtext !== '';
};
lib.edition = function () {
return JSLINT.edition;
};
}
});