-
Notifications
You must be signed in to change notification settings - Fork 1
/
dot_finicky.js
483 lines (442 loc) · 14.7 KB
/
dot_finicky.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/** @typedef {"main"|"work"|"home"} Context */
/** @typedef {Record<Context,string>} Contexts */
/** @typedef {Record<string,Contexts>} Browsers */
/** @typedef {Record<string,string>} Applications */
if (typeof URLSearchParams === "undefined" || URLSearchParams === null) {
URLSearchParams = class URLSearchParamsPolyfill {
/**
* @readonly
* @type {number}
*/
size;
/**
* @private
* @type {Record<string, string[]>}
*/
data;
/**
* @param {string[][] | Record<string, string> | string | URLSearchParams} [init]
*/
constructor(init) {
switch (typeof init) {
case "string":
Object.defineProperty(this, "data", {
configurable: false,
enumerable: true,
value: init.split("&").reduce(
/**
* @param {Record<string,string[]>} obj
*/
(obj, entry) => {
const pair = entry.split("=", 2);
let value = obj[pair[0]] || new Array();
value.push(pair[1]);
obj[pair[0]] = value;
return obj;
},
{},
),
writable: false,
});
break;
default:
throw new Error("URLSearchParams polyfill supports string init only");
}
Object.defineProperty(this, "size", {
configurable: false,
enumerable: false,
value: Object.keys(this.data).length,
writable: false,
});
}
/**
* Appends a specified key/value pair as a new search parameter.
*
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/URLSearchParams/append)
* @param {string} name
* @param {string} value
*/
append(name, value) {
let currentValue = this.data[name] || new Array();
currentValue.push(value);
this.data[name] = currentValue;
}
/**
* Deletes the given search parameter, and its associated value, from the list of all search parameters.
*
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/URLSearchParams/delete)
* @param {string} name
* @param {string} [value]
*/
delete(name, value) {
const currentValues = this.data[name];
if (typeof currentValues === "undefined" || currentValues === null) {
return;
}
if (typeof value !== "undefined" && value !== null) {
this.data[name] = currentValues.filter(currentValue => currentValue != value);
return;
}
this.data[name] = null;
}
/**
* Returns the first value associated to the given search parameter.
*
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/URLSearchParams/get)
* @param {string} name
* @returns {string | null}
*/
get(name) {
return (this.data[name] || [])[0] || null;
};
/**
* Returns all the values association with a given search parameter.
*
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/URLSearchParams/getAll)
* @param {string} name
* @returns {string[]}
*/
getAll(name) {
return this.data[name] || [];
}
/**
* Returns a Boolean indicating if such a search parameter exists.
*
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/URLSearchParams/has)
* @param {string} name
* @param {string} [value]
* @returns {boolean}
*/
has(name, value) {
}
/**
* Sets the value associated to a given search parameter to the given value. If there were several values, delete the others.
*
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/URLSearchParams/set)
* @param {string} name
* @param {string} value
*/
set(name, value) {
this.data[name] = [value];
}
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/URLSearchParams/sort) */
sort() {
}
/** Returns a string containing a query string suitable for use in a URL. Does not include the question mark.
* @returns {string}
*/
toString() {
return Object.entries(this.data).map(([key, values]) =>
values.map(value => key + "=" + value).join("&")).join("&");
}
/**
* @callback forEachCallback
* @param {string} value
* @param {string} key
* @param {URLSearchParams} parent
*/
/**
* @param {forEachCallback} callbackfn
* @param {any} [thisArg]
*/
forEach(callbackfn, thisArg) {
Object.entries(this.data).forEach(([key, values]) =>
values.forEach(value => callbackfn(value, key, thisArg || this)));
}
/** Returns an array of key, value pairs for every entry in the search params.
* @returns {IterableIterator<[string, string]>}
*/
[Symbol.iterator]() {
return Object.entries(this.data).map(([key, values]) =>
values.map(value => [key, value])).flat(1);
}
/** Returns an array of key, value pairs for every entry in the search params.
* @returns {IterableIterator<[string, string]>}
*/
entries() {
return Object.entries(this.data).map(([key, values]) =>
values.map(value => [key, value])).flat(1);
}
/** Returns a list of keys in the search params. */
/**
* @returns {IterableIterator<string>}
*/
keys() {
return Object.keys(this.data);
}
/** Returns a list of values in the search params. */
/**
* @returns {IterableIterator<string>}
*/
values() {
return Object.values(this.data);
}
};
}
if (typeof atob === "undefined" || atob === null) {
const b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
// Regular expression to check formal correctness of base64 encoded strings
b64re = /^(?:[A-Za-z\d+\/]{4})*?(?:[A-Za-z\d+\/]{2}(?:==)?|[A-Za-z\d+\/]{3}=?)?$/;
const atob = function(string) {
// atob can work with strings with whitespaces, even inside the encoded part,
// but only \t, \n, \f, \r and ' ', which can be stripped.
string = String(string).replace(/[\t\n\f\r ]+/g, "");
if (!b64re.test(string))
throw new TypeError("Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded.");
// Adding the padding if missing, for semplicity
string += "==".slice(2 - (string.length & 3));
var bitmap, result = "", r1, r2, i = 0;
for (; i < string.length;) {
bitmap = b64.indexOf(string.charAt(i++)) << 18 | b64.indexOf(string.charAt(i++)) << 12
| (r1 = b64.indexOf(string.charAt(i++))) << 6 | (r2 = b64.indexOf(string.charAt(i++)));
result += r1 === 64 ? String.fromCharCode(bitmap >> 16 & 255)
: r2 === 64 ? String.fromCharCode(bitmap >> 16 & 255, bitmap >> 8 & 255)
: String.fromCharCode(bitmap >> 16 & 255, bitmap >> 8 & 255, bitmap & 255);
}
return result;
};
globalThis.atob = atob;
}
/** @type {Applications} */
const apps = {
Chrome: "com.google.Chrome",
Edge: "com.microsoft.edgemac",
Firefox: "org.mozilla.firefox",
Safari: "com.apple.Safari",
};
/** @type {Browsers} */
const browsers = {
"nllm4000559023": {
main: apps.Edge,
work: apps.Edge,
home: apps.Safari,
},
};
/** @type {Contexts} */
const defaultBrowsers = {
main: apps.Safari,
work: apps.Safari,
home: apps.Safari,
};
/**
* @param {Context} contextName
* @returns {import("./.finicky.d").Finicky.BrowserFn}
* */
const getBrowser = (contextName) => (params) => {
// finicky.log(finicky.getSystemInfo().name.split(".")[0]);
const context = browsers[finicky.getSystemInfo().name.split(".")[0].toLowerCase()] || defaultBrowsers;
return context[contextName];
};
/**
* @param {string} prefix
* @returns {import("./.finicky.d").Finicky.Rewrite}
*/
const prefixBGone = (prefix) => ({
match: ({ urlString }) => urlString.startsWith(prefix),
url: ({ urlString }) => decodeURIComponent(urlString.substring(prefix.length).replace(/%25/g, "%")).replaceAll(" ", "%20"),
});
/**
* @param {string} host
* @param {string} queryParam
* @returns {import("./.finicky.d").Finicky.Rewrite}
*/
const redirectBGone = (host, queryParam) => ({
match: ({ url }) => url.host.endsWith(host),
url: ({ url }) => decodeURIComponent((new URLSearchParams(url.search)).get(queryParam).replace(/%25/g, "%")),
});
const suffixBGone = (prefix, suffix) => ({
match: ({ urlString }) => urlString.endsWith(suffix),
url: ({ urlString }) => decodeURIComponent(urlString.substring(0, urlString.length - suffix.length).replace(/%25/g, "%")),
});
const matchBGone = (re) => ({
match: ({ urlString }) => urlString.match(re),
url: ({ urlString }) => decodeURIComponent(urlString.replace(re, "").replace(/%25/g, "%")).replaceAll(" ", "%20"),
});
const defaultPrefix = (prefix) => ({
match: ({ urlString }) => !urlString.match(/^[a-z]+:\/\//),
url: ({ urlString }) => "https://" + urlString,
});
/**
* @type {import("./.finicky.d").Finicky.URLFn}
*/
const fuckOffMandrill = ({ url }) =>
JSON.parse(JSON.parse(atob((new URLSearchParams(url.search)).get("p"))).p).url;
/// <reference path="./.finicky.d.ts" />
/** @type {import("./.finicky.d").Finicky.Config} */
module.exports = {
defaultBrowser: getBrowser("main"),
rewrite: [
// cleanup TLDR newsletter links
prefixBGone("https://tracking.tldrnewsletter.com/CL0/"),
// cleanup Outlook redirects
redirectBGone("safelinks.protection.outlook.com", "url"),
// gotta love those email trackers
prefixBGone("https://click.pstmrk.it/3s/"),
matchBGone(/\/nqxP\/[^/]{6}\/AQ\/.*/),
// new gimmick: urldefense.com wraps the URL with some hash at the end
prefixBGone("https://urldefense.com/v3/__"),
matchBGone(/__;!!.*?\$$/),
// new gimmick: awstrack wraps the URL with some hash at the end
matchBGone(/^https:\/\/.+\.awstrack\.me\/L0\//),
matchBGone(/\/[0-9]\/\w{16}-\w{8}-\w{4}-\w{4}-\w{4}-\w{12}-\w{6}\/\S+=[0-9]+$/),
// some trackers don't add the protocol to the target URL, so we add https
defaultPrefix("https://"),
// remove tracking query parameters
{
match: () => true,
url: ({ url }) => {
const removeKeysStartingWith = [
"__hs", // HubSpot
"_bta_", // Bronto
"_hs", // HubSpot
"gdf", // GoDataFeed
"hsa_", // HubSpot
"matomo_", // Matomo
"mc_", // MailChimp
"mkt_", // Adobe Marketo
"ml_", // MailerLite
"mtm_", // Matomo
"oly_", // Omeda
"piwik_", // Piwik
"pk_", // Piwik
"trk_", // Listrak
"uta_",
"utm_", // Google Analytics
"vero_", // Vero
];
const removeKeys = [
"__s", // Drip.com
"_ga", // Google Analytics
"_ke", // Klaviyo
"_openstat", // Yandex
"auto_subscribed",
"dclid", // Google
"dm_i", // dotdigital
"ef_id", // Adobe Advertising Cloud
"email_source",
"epik", // Pinterest
"fbclid", // Facebook
"fblid",
"gclid", // Google AdWords/Analytics
"gclsrc", // Google DoubleClick
"hsCtaTracking", // HubSpot
"igshid", // Instagram
"mkwid", // Marin
"msclkid", // Microsoft Advertising
"pcrid", // Marin
"rb_clickid", // Unknown high-entropy
"redirect_log_mongo_id", // Springbot
"redirect_mongo_id", // Springbot
"s_cid", // Adobe Site Catalyst
"s_kwcid", // Adobe Analytics
"sb_referer_host", // Springbot
"wickedid", // Wicked Reports
"yclid", // Yandex click ID
];
const search = url.search
.split("&")
.map((parameter) => parameter.split("="))
.filter(([key]) =>
!removeKeysStartingWith.some(
(startingWith) => key.startsWith(startingWith)
)
)
.filter(([key]) => !removeKeys.some((removeKey) => key === removeKey));
return {
...url,
search: search.map((parameter) => parameter.join("=")).join("&"),
};
},
},
// Youtu.be => Yattee
{
// https://youtu.be/T_O-NeTvUzs?feature=shared
match: ({ urlString }) => urlString.startsWith("https://youtu.be"),
// https://r.yattee.stream/watch?feature=shared&v=T_O-NeTvUzs
url: ({ urlString }) => "https://r.yattee.stream/watch?v=" + urlString.replace(/https:\/\/youtu\.be\/([^\?]+)(\?.*)?/, "$1"),
},
// Youtube => Yattee
{
match: ({ url }) => url.host == "youtube.com",
url: ({ url }) => "https://r.yattee.stream/watch?v=" + url.search.replace(/.*(v=[^&]+).*/, "$1"),
},
// Mandrill => wrapped URL
{
match: ({ urlString }) => urlString.startsWith("https://mandrillapp.com/track/click/"),
url: fuckOffMandrill,
},
{
// https://realm-group-holdings-limited.app.loxo.co/agencies/11114/email_tracking/click?id=197135213&url=https%3A%2F%2Fdocs.google.com%2Fdocument%2Fd%2F1JLYlq2f4pwksRTO61r4-Dlnu9LIjn3ToV66pS2qvabA%2Fedit
match: ({ url }) => url.host == "realm-group-holdings-limited.app.loxo.co"
&& url.pathname.includes("/email_tracking/"),
url: ({ url }) => decodeURIComponent((new URLSearchParams(url.search)).get("url")),
}
],
handlers: [
// Work: Azure DevOps
{
match: "*dev.azure.com*",
browser: getBrowser("work")
},
// Work: Microsoft Teams handler
{
match: finicky.matchHostnames("teams.microsoft.com"),
browser: "com.microsoft.teams2",
url({ url }) {
return {
...url,
protocol: "msteams",
};
},
},
// Work: source apps
{
match: ({ opener }) =>
[
"com.tinyspeck.slackmacgap",
"com.microsoft.teams2",
"com.microsoft.Outlook",
].includes(opener.bundleId),
browser: getBrowser("work")
},
// Work: specific domains
{
match: [
"*.abnamro.com*",
"*.abnamro.org*",
"https://portal.azure.com*",
],
browser: getBrowser("work")
},
// Personal: social and IM apps
{
match: ({ opener }) =>
[
"com.facebook.archon",
"ru.keepcoder.Telegram",
"com.hnc.Discord",
"WhatsApp"
].includes(opener.bundleId),
browser: getBrowser("home")
},
// Personal: local and private domains
{
match: [
"github.com/wwmoraes*",
"*.local*",
"*.com.br*",
"*.thuisbezorgd.nl*",
"*.krisp.ai*"
],
browser: getBrowser("home")
},
// General: apps that should open on the main browser directly
{
match: ({ opener }) =>
[
"com.1password.1password"
].includes(opener.bundleId),
browser: getBrowser("main")
}
]
};