-
-
Notifications
You must be signed in to change notification settings - Fork 141
/
scope.js
334 lines (273 loc) · 8.28 KB
/
scope.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
/**
* phantomas browser "scope" with helper code
*
* Code below is executed in page's "scope" (injected by the scope.injectJs() helper)
*/
/* istanbul ignore next */
(function coreScope(scope) {
"use strict";
// create a scope
var phantomas = (scope.__phantomas = scope.__phantomas || {});
// keep the original JSON functions (#482)
phantomas.JSON = {
parse: JSON.parse,
stringify: JSON.stringify,
};
// NodeRunner
var nodeRunner = function () {
// "Beep, Beep"
};
nodeRunner.prototype = {
// call callback for each child of node
walk: function (node, callback, depth) {
if (this.isSkipped(node)) {
return;
}
var childNode,
childNodes = (node && node.childNodes) || [];
depth = depth || 1;
for (var n = 0, len = childNodes.length; n < len; n++) {
childNode = childNodes[n];
// callback can return false to stop recursive
if (callback(childNode, depth) !== false) {
this.walk(childNode, callback, depth + 1);
}
}
},
// override this function when you create an object of class phantomas.nodeRunner
// by default only iterate over HTML elements
isSkipped: function (node) {
return !node || node.nodeType !== Node.ELEMENT_NODE;
},
};
// for backtraces
(function () {
// @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/Stack
function getStackFromError(e) {
var stack = e.stack
.trim()
.split("\n")
.map(function (item) {
return item.replace(/^(\s+at\s|@)/, "").trim();
})
.filter(function (item) {
return /:\d+\)?$/.test(item);
});
//console.log(stack);
return stack;
}
function getBacktrace() {
var stack = [];
try {
throw new Error("backtrace");
} catch (e) {
stack = getStackFromError(e).slice(3);
}
return stack.join(" / ");
}
function getCaller(stepBack) {
var caller = false;
stepBack = stepBack || 0;
try {
throw new Error("backtrace");
} catch (e) {
caller = getStackFromError(e)[3 + stepBack];
}
return caller;
}
phantomas.getBacktrace = getBacktrace;
phantomas.getCaller = getCaller;
})();
// communication with phantomas core
(function () {
var stringify = JSON.stringify,
origConsoleLog = console.log;
// overrride console.log (issue #69)
console.log = function () {
// pass all arguments as an array, let phantomas format them
// @see https://developer.mozilla.org/en-US/docs/Web/API/console
// avoid 'TypeError: JSON.stringify cannot serialize cyclic structures.'
try {
origConsoleLog.call(
console,
"log:" + stringify(Array.prototype.slice.call(arguments))
);
// eslint-disable-next-line no-empty
} catch (e) {}
};
function sendMsg(type, data) {
scope.__phantomas_emit("scopeMessage", type, data);
}
function log() {
sendMsg("log", Array.prototype.slice.apply(arguments));
}
function setMetric(name, value, isFinal) {
sendMsg("setMetric", [
name,
typeof value !== "undefined" ? value : 0,
isFinal === true,
]);
}
function incrMetric(name, incr /* =1 */) {
sendMsg("incrMetric", [name, incr || 1]);
}
function addToAvgMetric(name, value) {
sendMsg("addToAvgMetric", {
name: name,
value: value,
});
}
function setMarkerMetric(name) {
sendMsg("setMarkerMetric", {
name: name,
});
}
function addOffender(/*metricName, msg, ...*/) {
sendMsg("addOffender", Array.prototype.slice.apply(arguments));
}
// see lib/index.js code that injects __phantomas_options into page scope
function getParam(param, _default) {
return scope.__phantomas_options[param] || _default;
}
// exports
phantomas.log = log;
phantomas.setMetric = setMetric;
phantomas.incrMetric = incrMetric;
phantomas.addToAvgMetric = addToAvgMetric;
phantomas.setMarkerMetric = setMarkerMetric;
phantomas.addOffender = addOffender;
phantomas.emit = scope.__phantomas_emit.bind(scope);
phantomas.getParam = getParam;
})();
/**
* Proxy function to be used to track calls to native DOM functions
*
* Callback is provided with arguments original function was called with
*
* Example:
*
* window.__phantomas.proxy(window.document, 'getElementById', function() {
* // ...
* });
*/
(function () {
var enabled = true;
// turn off spying to not include internal phantomas actions
function spyEnabled(state, reason) {
enabled = state === true;
phantomas.log(
"Spying " +
(enabled ? "enabled" : "disabled") +
(reason ? " - " + reason : "")
);
}
// pass reportResults = true to prepend arguments passed to callback
// with the result of call to the original function - issue #420
function spy(obj, fn, callback, reportResults) {
var origFn = obj && obj[fn];
if (typeof origFn !== "function") {
return false;
}
phantomas.log(
'spy: attaching to "%s" function%s',
fn,
reportResults ? " with results reporting" : ""
);
obj[fn] = function () {
var args = Array.prototype.slice.call(arguments),
results = origFn.apply(this, args);
if (enabled && typeof callback === "function") {
callback.apply(
this,
reportResults === true ? [results].concat(args) : args
);
}
return results;
};
// copy custom properties of original function to the mocked one
Object.keys(origFn).forEach(function (key) {
obj[fn][key] = origFn[key];
});
obj[fn].prototype = origFn.prototype;
return true;
}
var spiedGlobals = {};
// call given callback when "varName" global variable is being defined
// used by jQuery module
function spyGlobalVar(varName, callback) {
phantomas.log("spy: attaching to %s global variable", varName);
window.__defineSetter__(varName, function (val) {
phantomas.log("spy: %s global variable has been defined", varName);
spiedGlobals[varName] = val;
callback(val);
});
window.__defineGetter__(varName, function () {
return spiedGlobals[varName] || undefined;
});
}
// exports
phantomas.spy = spy;
phantomas.spyGlobalVar = spyGlobalVar;
phantomas.spyEnabled = spyEnabled;
})();
/**
* Returns "DOM path" to a given node (starting from <body> down to the node)
*
* Example: body.logged_out.vis-public.env-production > div > div
*/
function getDOMPath(node, dontGoUpTheDom /* = false */) {
var path = [],
entry = "";
if (node === window) {
return "window";
}
while (node instanceof Node) {
// div
entry = node.nodeName.toLowerCase();
// shorten the path a bit
if (["body", "head", "html"].indexOf(entry) > -1) {
path.push(entry);
break;
}
if (node instanceof DocumentFragment) {
entry = "DocumentFragment";
}
// div#foo
if (node.id && node.id !== "") {
entry += "#" + node.id;
}
// div#foo.bar.test
else if (typeof node.className === "string" && node.className !== "") {
entry += "." + node.className.trim().replace(/\s+/g, ".");
}
// div[0] <- index of child node
else if (node.parentNode instanceof Node) {
entry +=
"[" +
Math.max(
0,
Array.prototype.indexOf.call(
node.parentNode.children || node.parentNode.childNodes,
node
)
) +
"]";
}
path.push(entry);
if (dontGoUpTheDom === true) {
break;
}
// go up the DOM
node = node && node.parentNode;
}
return path.length > 0 ? path.reverse().join(" > ") : false;
}
// exports
phantomas.getDOMPath = getDOMPath;
phantomas.nodeRunner = nodeRunner;
phantomas.log(
"phantomas page scope initialized for <%s> (is an iframe: %s)",
window.location.toString(),
window.parent !== window
);
})(window);