-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchrome-inspector.js
326 lines (280 loc) · 12.2 KB
/
chrome-inspector.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
/**************************************
* Fast's JS Function Call Inspector: *
* Chrome Console Edition *
**************************************/
var inspector = (() => {
const oldSetTimeout = window.setTimeout; // save the original setTimeout so antispam calling it doesn't show up in logs
return {
detours: new WeakMap(),
detourcount: 0,
spamcounter: 0,
isCursedKey: (key) => key.match(/[^(a-z|$|_|A-Z)]/),
isBanned: (key) => key == "window.inspector" || key=="window.performance" || inspector.BANNED_PATHS.includes(key) || inspector.includesPartial(inspector.BANNED_PATHS,key), // pls don't remove hardcoded values from here
isNative: (func) => (func+"").endsWith("{ [native code] }"),
antispam: () => { inspector.spamcounter = 0; oldSetTimeout(inspector.antispam,inspector.ANTISPAM_INTERVAL); },
ttime: 0
}
})();
/* HOW TO USE:
Paste in chrome console and hit enter. Click on debug log events to expand them.
Call inspector.unhook() on a detoured function to unhook it from the detour system.
Call inspector.getOld() on a detoured function to get the original function.
Add any problematic functions or objects to BANNED_PATHS to ban traversing them.
*/
inspector.MAX_SEARCH_DEPTH = 15; // Max recursive depth to search.
inspector.MAX_SEARCH_WIDTH = 100; // Any object besides window with more objects stored under it than this will be ignored.
inspector.ANTISPAM_INTERVAL = 1000; // in ms
inspector.ANTISPAM_QUOTA = 64; // max logs per interval
inspector.LIST_GROUPING_MODE = console.group; // or console.groupCollapsed
inspector.LIST_DETOURED = true; // list attempted detours
inspector.LIST_SUCCESS_ONLY = true; // only list successful detours
inspector.MIN_CPU_TO_LOG = 5; // don't log any calls that take less than this
inspector.DETOUR_NATIVE = true; // detours native functions like setTimeout
inspector.DETOUR_PROTOTYPES = true; // if true, recursively explore prototypes and detour everything applicable that's found!
inspector.DETOUR_ARGS = false; // function called with detourable arguments? detour those too!
if( inspector.DETOUR_ARGS ){ console.warn("Function argument detouring enabled. This will incur a severe performance hit."); }
// Something specific spamming console? Completely breaking the website? Add it here. Supports both full paths and substrings of paths.
// Feel free to make a PR if you find something nasty I haven't added yet.
inspector.BANNED_PATHS = [
"frameElement","webpackJsonp","cssRules",".apply","onTick","shouldRunAtMaxSpeed","React","timedOut"
];
inspector.COLORS = { // inspired by Wiremod's Expression 2 Language in Garry's Mod
string: "color: #999999;",
number: "color: #ff6666;",
bigint: "color: #a45b5b;",
boolean: "color: #668cff;",
symbol: "color: #fbfb51;",
object: "color: #80ff80;",
undefined: "color: #ffb56b;",
function: "color: #fc83fc;",
null: "color: #4d804d;",
default: "color: #dddddd;",
function_ignored: "color: #864086;",
object_ignored: "color: #407040;",
};
inspector.antispam(); // kick off antispam
inspector.getPath = function(path,key){
if( inspector.isCursedKey(key) ){
if( parseInt(key) != NaN ){ // int
return `${path}[${key}]`;
}
else{ // something nasty
return `${path}[\`${key}\`]`;
}
}
else{
return `${path}.${key}`;
}
};
if( inspector.LIST_DETOURED ){
inspector.logDetoured = function(path,success,obj){
if( success === true ){
console.log(`success: %c${path}`,inspector.COLORS["function"]);
}
else if( !inspector.LIST_SUCCESS_ONLY ){
console.log(`%cignored: %c${path} %c(${success})`,"color: #666666;",inspector.COLORS[typeof(obj)+"_ignored"],"color: #666666;");
}
};
}
else{
inspector.logDetoured = () => {};
}
inspector.getPrettyCall = function(data,args){
let tags = [`%c ${(data.ctime).toFixed(0)}us %c| `,inspector.COLORS.string,inspector.COLORS.default];
if(args.length==0){
tags[0] += `${data.location}();`
return tags;
}
const colors = inspector.COLORS; // tiny perf save
for (let i = 0; i < args.length; i++) {
const type = args[i]===null ? 'null' : typeof(args[i]);
tags[tags.length] = colors[type]
tags[tags.length] = colors.default; // argument separators
}
tags[0] += `${data.location}(%c${inspector.join(args)}%c);`;
return tags;
}
inspector.join = function(array){
var output = [];
for (let index = 0; index < array.length; index++){
if( array[index] === null ){
output[index] = "null";
}
else{
let type = typeof(array[index])
switch( type ){
case 'string': output[index] = `\`${array[index]}\``; break;
case 'object': output[index] = `[Object ${(inspector.getOld(array[index].constructor).name)}]`; break;
case 'undefined': output[index] = 'undefined'; break;
default: output[index] = array[index]; break;
}
}
}
return output.join("%c, %c");
}; // display null properly
inspector.trace = function(){ let trace = (new Error().stack).replaceAll(" ","").split("at ").splice(3); var output = []; for (let index = 0; index < trace.length; index++) { output[index] = " ".repeat(index+1) + trace[index]; } return output.join(""); }; // get fancy stack trace
inspector.argobj = function Arguments( args ) { for (let index = 0; index < args.length; index++) { this[index] = args[index]; } }; // names the object "Returned" in console
inspector.returnobj = function Returned( obj ) { this.result = obj; }; // names the object "Arguments" in console
inspector.handleLogging = function(data, args, result){
if(args[0] == inspector.antispam){ return; } // ignore antispam setTimeout calls
if(data.ctime < inspector.MIN_CPU_TO_LOG){ return; } // boooring
if(inspector.spamcounter == inspector.ANTISPAM_QUOTA){ console.warn(`ANTISPAM: Suppressing further logs for the next ${inspector.ANTISPAM_INTERVAL} ms`); }
if(inspector.spamcounter > inspector.ANTISPAM_QUOTA){ return; }
inspector.spamcounter++;
try{
console.groupCollapsed.apply(this,inspector.getPrettyCall(data,args));
if(args.length > 0){ console.log(new inspector.argobj(args)); }
if( typeof result !== 'undefined' ){ console.log(new inspector.returnobj(result)); }
console.log(data.old);
console.log("Stack Trace:\n" + inspector.trace());
}
finally{
console.groupEnd();
}
};
inspector.includesPartial = function(strings,string){
for (const key in strings) {
if(string.includes(strings[key])){ return true; }
}
return false;
};
inspector.unhook = function(detoured_function){
let old = inspector.detours.get(detoured_function);
if( old ){
old.revert();
console.log("Function unhooked.");
inspector.detours.delete(detoured_function);
return true;
}
return false;
};
inspector.getOld = function(detoured_function){
let old = inspector.detours.get(detoured_function);
if( old ){
return old.get();
}
return detoured_function;
};
inspector.cloneProperties = function(parent,child){
for(var key in parent) { // clone properties from old to new
if(parent.hasOwnProperty(key)) {
child[key] = parent[key];
}
}
};
inspector.detour = function(obj,key,path="unknown") {
try{
let func = obj[key];
// I had a 1-on-1 talk with $.fn.init and found out the value of 'this' was different between the original func and the detoured one
// hopefully I can find a way to get and use the correct 'this' in the detoured calls
if(func.toString().includes("this.")){
return "uses 'this'";
}
if( !inspector.DETOUR_NATIVE && inspector.isNative(func) ){ return "native" }
let func_name = path + " [detoured]";
let metadata = {
location : path,
old : func,
ctime : 0,
}
if( inspector.DETOUR_ARGS ){
metadata.detoured = {[func_name]: (function() { /* DETOUR */
try{ inspector.recurse(arguments, `${path}(...)`) }
catch(e){}
let finish, start = performance.now()
result = func.apply(this,arguments);
finish = performance.now();
metadata.ctime = (finish-start)*1000;
inspector.handleLogging(metadata,arguments,result);
if( result != undefined ){
return result;
}
})}[func_name];
}
else { // avoid the overhead of detouring args if we don't need them
metadata.detoured = {[func_name]: (function() { /* DETOUR */
let finish, start = performance.now()
result = func.apply(this,arguments);
finish = performance.now();
metadata.ctime = (finish-start)*1000;
inspector.handleLogging(metadata,arguments,result);
if( result != undefined ){
return result;
}
})}[func_name];
}
metadata.detoured.toString = function(){ // really epic toString override
return func.toString();
}
inspector.detours.set(metadata.detoured,{ // make it easy to revert without adding any fields to the new function
revert: (function(){obj[key] = func;}),
get: (function(){return func;})
});
inspector.cloneProperties(func,metadata.detoured);
obj[key] = metadata.detoured;
}
catch(e){
console.log(e);
return "script error";
}
return true;
};
inspector.recurse = function(obj, path, depth=0, refs=new WeakSet()) {
if( obj == null || obj == undefined ){ return; }
if( depth > inspector.MAX_SEARCH_DEPTH ){ return; }
// Avoid infinite recursion
if( refs.has(obj) ) return;
else refs.add(obj);
let group = (depth > 0) && inspector.LIST_DETOURED;
let parent_type = typeof(obj);
if( inspector.DETOUR_PROTOTYPES ){
try {
inspector.recurse(Object.getPrototypeOf(obj),`Object.getPrototypeOf(${path})`,depth+1, refs);
}
catch(e){}
}
for (const key in obj) {
try{
let value = obj[key]; // this may throw illegalinvocation
if( value == window ){ continue; }
// dodge javascript frameworks and libraries
let count = 0;
if( depth > 0 ){
for (const _ in obj) {
count++;
}
if( count > inspector.MAX_SEARCH_WIDTH ){
inspector.logDetoured(path,`${count} children`,obj);
return;
}
}
let type = typeof(value);
let newpath = inspector.getPath(path,key);
let doDetour = false;
switch(type){
case 'function':
doDetour = true;
if( inspector.getOld(value)!=value ){ continue; } // don't detour stuff multiple times!!
if( group ){ group = false; inspector.LIST_GROUPING_MODE(`%c${path} (${parent_type})`,inspector.COLORS[parent_type]); }
case 'object':
if( inspector.isBanned(newpath) ){ continue; }
inspector.recurse(value, newpath, depth+1, refs);
break;
}
if(doDetour){
let success = inspector.detour(obj,key,newpath); // this has to be here due to recursion order
inspector.logDetoured(newpath,success,obj);
if( success === true ){ inspector.detourcount++; }
}
}
catch(e){}
}
if( !group ){
console.groupEnd();
}
};
console.groupCollapsed("Detours...");
inspector.recurse(window, "window");
inspector.LIST_DETOURED = false;
console.groupEnd();
console.log(`${inspector.detourcount} total functions detoured.`);