-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
281 lines (227 loc) · 6.91 KB
/
index.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
(() => {
let seenValues = null;
let initialWindowVariable = null;
function deepFindValueInArray(a, searchValue, currentObjectPath = '') {
if (!Array.isArray(a)) {
throw new Error(`deepFindValueInArray error: ${currentObjectPath} is not an array.`);
}
let results = [];
for (let i = 0; i < a.length; i++) {
if (a[i] === searchValue) {
results.push(`${currentObjectPath}[${i}]`);
} else if (Array.isArray(a[i]) && !seenValues.has(a[i])) {
seenValues.add(a[i]);
results = [
...results,
...deepFindValueInArray(a[i], searchValue, `${currentObjectPath}[${i}]`),
];
} else if (!!a[i] && typeof a[i] === 'object' && !seenValues.has(a[i])) {
seenValues.add(a[i]);
results = [
...results,
...deepFindValueInObject(a[i], searchValue, `${currentObjectPath}[${i}]`),
];
}
}
return results;
}
function deepFindValueInObject(o, searchValue, currentObjectPath = '') {
const keys = Object.keys(o);
let results = [];
for (let i = 0; i < keys.length; i++) {
const value = o[keys[i]];
if (value === searchValue) {
results.push(`${currentObjectPath}.${keys[i]}`);
} else if (Array.isArray(value) && !seenValues.has(value)) {
seenValues.add(value);
results = [
...results,
...deepFindValueInArray(value, searchValue, `${currentObjectPath}.${keys[i]}`),
];
} else if (!!value && typeof value === 'object' && !seenValues.has(value)) {
seenValues.add(value);
results = [
...results,
...deepFindValueInObject(value, searchValue, `${currentObjectPath}.${keys[i]}`),
];
}
}
return results;
}
function deepFindRegexValueInArray(a, regex, currentObjectPath = '') {
if (!Array.isArray(a)) {
throw new Error(
`deepFindRegexValueInArray error: ${currentObjectPath} is not an array.`,
);
}
let results = [];
for (let i = 0; i < a.length; i++) {
if (!!a[i] && a[i].toString && regex.test(a[i].toString())) {
results.push(`${currentObjectPath}[${i}] - ${a[i].toString()}`);
} else if (Array.isArray(a[i]) && !seenValues.has(a[i])) {
seenValues.add(a[i]);
results = [
...results,
...deepFindValueInArray(a[i], regex, `${currentObjectPath}[${i}]`),
];
} else if (!!a[i] && typeof a[i] === 'object' && !seenValues.has(a[i])) {
seenValues.add(a[i]);
results = [
...results,
...deepFindValueInObject(a[i], regex, `${currentObjectPath}[${i}]`),
];
}
}
return results;
}
function deepFindRegexValueInObject(o, regex, currentObjectPath = '') {
const keys = Object.keys(o);
let results = [];
for (let i = 0; i < keys.length; i++) {
const value = o[keys[i]];
if (!!value && value.toString && regex.test(value.toString())) {
results.push(`${currentObjectPath}.${keys[i]} - ${value.toString()}`);
} else if (Array.isArray(value) && !seenValues.has(value)) {
seenValues.add(value);
results = [
...results,
...deepFindRegexValueInArray(value, regex, `${currentObjectPath}.${keys[i]}`),
];
} else if (!!value && typeof value === 'object' && !seenValues.has(value)) {
seenValues.add(value);
results = [
...results,
...deepFindRegexValueInObject(value, regex, `${currentObjectPath}.${keys[i]}`),
];
}
}
return results;
}
function deepGetKeysInArray(a) {
if (!Array.isArray(a)) {
throw new Error(`deepGetKeysInArray error: ${a} is not an array.`);
}
const result = [];
for (let i = 0; i < a.length; i++) {
if (Array.isArray(a[i]) && !seenValues.has(a[i])) {
seenValues.add(a[i]);
result[i] = deepGetKeysInArray(a[i]);
} else if (!!a[i] && typeof a[i] === 'object' && !seenValues.has(a[i])) {
seenValues.add(a[i]);
result[i] = deepGetKeysInObject(a[i]);
} else {
result[i] = !!a[i];
}
}
return result;
}
function deepGetKeysInObject(o) {
const keys = Object.keys(o);
let result = {};
for (let i = 0; i < keys.length; i++) {
try {
const value = o[keys[i]];
if (Array.isArray(value) && !seenValues.has(value)) {
seenValues.add(value);
result[keys[i]] = deepGetKeysInArray(value);
} else if (!!value && typeof value === 'object' && !seenValues.has(value)) {
seenValues.add(value);
result[keys[i]] = deepGetKeysInObject(value);
} else {
result[keys[i]] = !!value;
}
} catch (e) {
if (e instanceof DOMException) {
continue;
}
throw e;
}
}
return result;
}
function deepSubtractOfArrays(a1, a2) {
if (!Array.isArray(a1) || !Array.isArray(a2)) {
throw new Error(`deepSubtractOfArrays error: a1:${a1} or a2:${a2} is not an array.`);
}
const result = [];
for (let i = 0; i < a1.length; i++) {
if (Array.isArray(a1[i]) && !seenValues.has(a1[i])) {
seenValues.add(a1[i]);
if (!Array.isArray(a2[i])) {
result[i] = a1[i];
} else {
result[i] = deepSubtractOfArrays(a1[i], a2[i]);
}
} else if (!!a1[i] && typeof a1[i] === 'object' && !seenValues.has(a1[i])) {
seenValues.add(a1[i]);
if (typeof a2[i] !== 'object') {
result[i] = a1[i];
} else {
result[i] = deepSubtractOfObjects(a1[i], a2[i]);
}
} else if (!!a1[i] !== !!a2[i]) {
result[i] = a1[i];
}
}
return result;
}
function deepSubtractOfObjects(o1, o2) {
const keys = Object.keys(o1);
let result = {};
for (let i = 0; i < keys.length; i++) {
const value = o1[keys[i]];
if (Array.isArray(value) && !seenValues.has(value)) {
seenValues.add(value);
if (!Array.isArray(o2[keys[i]])) {
result[keys[i]] = o1[keys[i]];
} else {
result[keys[i]] = deepSubtractOfArrays(value, o2[keys[i]]);
}
} else if (!!value && typeof value === 'object' && !seenValues.has(value)) {
seenValues.add(value);
if (typeof o2[keys[i]] !== 'object') {
result[keys[i]] = o1[keys[i]];
} else {
result[keys[i]] = deepSubtractOfObjects(value, o2[keys[i]]);
}
} else if (!!value !== !!o2[keys[i]]) {
result[keys[i]] = value;
}
}
return result;
}
function init() {
seenValues = new WeakSet();
}
window.Retrovisor = {
findValueInWindow: (str) => {
init();
return deepFindValueInObject(window, str);
},
findRegexValueInWindow: (regex) => {
if (typeof regex !== 'object' || !regex.test) {
throw new Error('findRegexValueInWindow need a regex');
}
init();
return deepFindRegexValueInObject(window, regex);
},
dumpCurrentWindowVariables: () => {
init();
return `Retrovisor.loadInitialWindowVariable(${JSON.stringify(
deepGetKeysInObject(window),
)})`;
},
loadInitialWindowVariable: (o) => {
initialWindowVariable = o;
},
extractNonNativeVariablesFromWindow: () => {
if (!initialWindowVariable) {
throw new Error(
`You first need to create a dump of window variable in your browser new tab with Retrovisor.dumpCurrentWindowVariables, and execute the returned text in this current tab.`,
);
}
init();
return deepSubtractOfObjects(window, initialWindowVariable.window);
},
};
})();