-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.ts
290 lines (265 loc) · 8.56 KB
/
util.ts
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
import { EventEmitter } from 'events';
import React from 'react';
export type Unpromisify<T> = T extends Promise<infer R> ? R : T;
export type Unarray<T> = T extends Array<infer R> ? R : T;
export type Tab = Unarray<Unpromisify<ReturnType<typeof browser.tabs.query>>>;
export type Request = {
cookieStoreId?: string;
documentUrl?: string; // RL of the document in which the resource will be loaded. For example, if the web page at "https://example.com" contains an image or an iframe, then the documentUrl for the image or iframe will be "https://example.com". For a top-level document, documentUrl is undefined.
frameId: number;
incognito?: boolean;
method: string;
originUrl: string;
parentFrameId: number;
proxyInfo?: {
host: string;
port: number;
type: string;
username: string;
proxyDNS: boolean;
failoverTimeout: number;
};
requestHeaders?: { name: string; value?: string; binaryValue?: number[] }[];
requestId: string;
tabId: number;
thirdParty?: boolean;
timeStamp: number;
type: string;
url: string; // the target of the request;
urlClassification?: { firstParty: string[]; thirdParty: string[] };
};
export function getshorthost(host: string) {
const parts = host
.replace(/^.*:\/\//, '')
.replace(/\/.*$/, '')
.split('.');
const second_last = parts.at(-2);
if (!second_last) {
throw new Error('url too short?');
}
let lookback = !['co', 'com'].includes(second_last) ? -2 : -3;
if (parts.at(-2) == 'doubleclick' || parts.at(-2) == 'google') {
lookback = -4; // to distinguish between google ads and stats
} else if (parts.at(-2) == 'google') {
lookback = -3; // to distinguish various google services
}
return parts.slice(lookback).join('.');
}
export function useEmitter(
e: EventEmitter
): [
Record<string, number | undefined>,
React.Dispatch<React.SetStateAction<Record<string, number | undefined>>>
] {
const [eventCounts, setEventCounts] = React.useState<Record<string, number | undefined>>({
'*': 0,
});
React.useEffect(() => {
const callback = (eventSubtype: string) => {
setEventCounts((eventCounts) => ({
...eventCounts,
...{ [eventSubtype]: (eventCounts[eventSubtype] || 0) + 1 },
...{ '*': (eventCounts['*'] === undefined ? 0 : eventCounts['*']) + 1 },
}));
};
e.on('change', callback);
return () => {
e.removeListener('change', callback);
};
}, []);
return [eventCounts, setEventCounts];
}
export function parseCookie(cookie: string): Record<string, string> {
return cookie
.split(';')
.map((l) => l.split('='))
.reduce(
(acc, [key, value]) => ({
...acc,
[key]: value,
}),
{}
);
}
export async function getTabByID(id: number) {
const tabs = await browser.tabs.query({ currentWindow: true });
return tabs.find((tab) => tab.id == id);
}
export function parseToObject(str: unknown): Record<string | symbol, unknown> {
let result: Record<string | symbol, unknown> = {};
let original_string: string;
if (typeof str === 'string') {
original_string = str;
result = JSON.parse(str);
} else if (typeof str == 'object') {
result = str as Record<string | symbol, unknown>;
original_string = (result[Symbol.for('originalString')] as string) || JSON.stringify(str);
} else {
return result;
}
result[Symbol.for('originalString')] = original_string;
return result;
}
export function isJSONObject(str: unknown): str is Record<string, unknown> | string | number {
try {
const firstChar = JSON.stringify(parseToObject(str))[0];
return ['{', '['].includes(firstChar);
} catch (e) {
return false;
}
}
export function isURL(str: unknown): str is string {
try {
return !!(typeof str === 'string' && new URL(str));
} catch (e) {
return false;
}
}
export function hyphenate(str: string): string {
return str.replace(/[_\[A-Z]/g, `${String.fromCharCode(173)}$&`);
}
export function unique<T>(array: T[]): Array<T> {
return Array.from(new Set<T>(array));
}
export function allSubhosts(host: string) {
const parts = host.split('.');
const result = [];
for (let i = 0; i < parts.length - 2; i++) {
result.push(parts.slice(i).join('.'));
}
return result;
}
export function reduceConcat<T>(a: T[], b: T[]): T[] {
return a.concat(b);
}
export function getDate() {
const d = new Date();
return `${d.getFullYear()}-${(d.getMonth() + 1).toString().padStart(2, '0')}-${d
.getDate()
.toString()
.padStart(2, '0')}`;
}
export function toBase64(file: File): Promise<string> {
return new Promise((resolve, reject) => {
const FR = new FileReader();
FR.addEventListener('load', (e) => {
const target = e.target;
if (!target) {
return reject('File missing?');
}
resolve(e.target.result as string);
});
FR.readAsDataURL(file);
});
}
export function makeThrottle(interval: number) {
let last_emit = 0;
function emit(callback: () => void) {
if (Date.now() - last_emit > interval) {
callback();
last_emit = Date.now();
return true;
} else {
return false;
}
}
return function (callback: () => void) {
if (!emit(callback)) {
setTimeout(() => emit(callback), interval);
}
};
}
export function isSameURL(url1: string, url2: string): boolean {
if (url1 === url2) {
return true;
}
url1 = url1.replace(/^https?:\/\//, '').replace(/\/$/, '');
url2 = url2.replace(/^https?:\/\//, '').replace(/\/$/, '');
return url1 === url2;
}
export function isBase64(s: string): boolean {
try {
atob(s);
return true;
} catch (e) {}
return false;
}
export function isBase64JSON(s: unknown): s is string {
return typeof s === 'string' && isBase64(s) && isJSONObject(atob(s));
}
export function flattenObject(
obj: unknown,
parser: (to_parse: { toString: () => string }) => string | Record<string, unknown> = (id) =>
id.toString(),
key = '',
ret = [] as [string, string][],
parsed = false
): [string, string][] {
const prefix = key === '' ? '' : `${key}.`;
if (Array.isArray(obj)) {
if (obj.length == 1) {
flattenObject(obj[0], parser, key, ret);
} else {
for (let i in obj) {
flattenObject(obj[i], parser, prefix + i, ret);
}
}
} else if (obj === null) {
ret.push([key, '']);
} else if (typeof obj === 'object') {
for (const [subkey, value] of Object.entries(obj)) {
flattenObject(value, parser, prefix + subkey, ret);
}
} else if (!parsed) {
try {
flattenObject(parser(obj as { toString: () => string }), parser, key, ret, true);
} catch (e) {
//emergency case, mostly for just type safety
ret.push([key, JSON.stringify(obj)]);
}
} else if (typeof obj === 'string') {
ret.push([key, obj]);
} else {
throw new Error('Something went wrong when parsing ' + obj);
}
return ret;
}
export function flattenObjectEntries(
entries: [string, unknown][],
parser: (to_parse: { toString: () => string }) => string | Record<string, unknown> = (id) =>
id.toString()
): [string, string][] {
return flattenObject(Object.fromEntries(entries), parser);
}
export function maskString(
str: string,
max_fraction_remaining: number,
max_chars_total: number
): string {
const amount_of_chars_to_cut =
str.length - Math.min(str.length * max_fraction_remaining, max_chars_total);
if (amount_of_chars_to_cut == 0) {
return str;
}
return (
str.slice(0, str.length / 2 - amount_of_chars_to_cut / 2) +
'(...)' +
str.slice(str.length / 2 + amount_of_chars_to_cut / 2)
);
}
export function safeDecodeURIComponent(s: string) {
try {
return decodeURIComponent(s);
} catch (e) {
return s;
}
}
export function normalizeForClassname(string: string) {
return string.replace(/[^a-z0-9]/gi, '-');
}
export function wordlist(words: string[]) {
return words.reduce(
(acc, word, i) => `${acc}${i > 0 ? (i < words.length - 1 ? ',' : ' i') : ''} ${word}`,
''
);
}