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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
|
window.browser = window.browser || window.chrome
import utils from "./utils.js"
let config, options, redirects, targets
function init() {
return new Promise(async resolve => {
browser.storage.local.get(["options", "redirects", "targets"], r => {
options = r.options
redirects = r.redirects
targets = r.targets
fetch("/config.json")
.then(response => response.text())
.then(configData => {
config = JSON.parse(configData)
resolve()
})
})
})
}
init()
browser.storage.onChanged.addListener(init)
function fetchFrontendInstanceList(service, frontend, redirects, options, config) {
let tmp = []
if (config.services[service].frontends[frontend].instanceList) {
for (const network in config.networks) {
tmp.push(...redirects[network], ...options[frontend][network].custom)
}
}
return tmp
}
function all(service, frontend, options, config, redirects) {
let instances = []
if (!frontend) {
for (const frontend in config.services[service].frontends) {
instances.push(...fetchFrontendInstanceList(service, frontend, redirects[frontend], options, config))
}
} else {
instances.push(...fetchFrontendInstanceList(service, frontend, redirects[frontend], options, config))
}
return instances
}
function regexArray(service, url, config, frontend) {
if (config.services[service].targets == "datajson") {
for (const instance of targets[service]) {
if (instance.startsWith(utils.protocolHost(url))) return true
}
} else {
let targetList = config.services[service].targets
if (frontend && config.services[service].frontends[frontend].excludeTargets)
for (const i in config.services[service].frontends[frontend].excludeTargets) {
targetList = targetList.splice(i, 1)
}
for (const targetString in targetList) {
const target = new RegExp(targetList[targetString])
if (target.test(url.href)) return true
}
}
return false
}
function redirect(url, type, initiator, forceRedirection) {
if (type != "main_frame" && type != "sub_frame" && type != "image") return
let randomInstance
let frontend
for (const service in config.services) {
if (!forceRedirection && !options[service].enabled) continue
if (config.services[service].embeddable && type != options[service].redirectType && options[service].redirectType != "both") continue
if (!config.services[service].embeddable && type != "main_frame") continue
frontend = options[service].frontend ?? Object.keys(config.services[service].frontends)[0]
if (!regexArray(service, url, config, frontend)) continue
if (initiator && all(service, null, options, config, redirects).includes(initiator.origin)) return "BYPASSTAB"
let instanceList = []
for (const network in options[frontend]) {
instanceList.push(...[...options[frontend][network].enabled, ...options[frontend][network].custom])
}
if (instanceList.length === 0) return
randomInstance = utils.getRandomInstance(instanceList)
break
}
if (!frontend || !randomInstance) return
// Here is a (temperory) space for defining constants required in 2 or more switch cases.
const mapCentreRegex = /@(-?\d[0-9.]*),(-?\d[0-9.]*),(\d{1,2})[.z]/
const dataLatLngRegex = /!3d(-?[0-9]{1,}.[0-9]{1,})!4d(-?[0-9]{1,}.[0-9]{1,})/
const placeRegex = /\/place\/(.*)\//
function convertMapCentre() {
let [lat, lon, zoom] = [null, null, null]
if (url.pathname.match(mapCentreRegex)) {
// Set map centre if present
;[lat, lon, zoom] = url.pathname.match(mapCentreRegex)
} else if (url.searchParams.has("center")) {
;[lat, lon] = url.searchParams.get("center").split(",")
zoom = url.searchParams.get("zoom") ?? "17"
}
return [zoom, lon, lat]
}
console.log(frontend)
switch (frontend) {
// This is where all instance-specific code must be ran to convert the service url to one that can be understood by the frontend.
case "beatbump":
return `${randomInstance}${url.pathname}${url.search}`
.replace("/watch?v=", "/listen?id=")
.replace("/channel/", "/artist/")
.replace("/playlist?list=", "/playlist/VL")
.replace(/\/search\?q=.*/, searchQuery => searchQuery.replace("?q=", "/") + "?filter=all")
case "hyperpipe":
return `${randomInstance}${url.pathname}${url.search}`.replace(/\/search\?q=.*/, searchQuery => searchQuery.replace("?q=", "/"))
case "bibliogram":
const reservedPaths = ["u", "p", "privacy"]
if (url.pathname === "/" || reservedPaths.includes(url.pathname.split("/")[1])) return `${randomInstance}${url.pathname}${url.search}`
if (url.pathname.startsWith("/reel")) return `${randomInstance}${url.pathname}`
if (url.pathname.startsWith("/tv")) return `${randomInstance}/p${url.pathname.replace(/\/tv/i, "")}${url.search}`
else return `${randomInstance}/u${url.pathname}${url.search}` // Likely a user profile, redirect to '/u/...'
case "lbryDesktop":
return url.href.replace(/^https?:\/{2}odysee\.com\//, "lbry://").replace(/:(?=[a-zA-Z0-9])/g, "#")
case "searx":
case "searxng":
return `${randomInstance}/${url.search}`
case "whoogle":
return `${randomInstance}/search${url.search}`
case "librex":
return `${randomInstance}/search.php${url.search}`
case "send":
return randomInstance
case "nitter":
let search = new URLSearchParams(url.search)
search.delete("ref_src")
search.delete("ref_url")
search = search.toString()
if (search !== "") search = `?${search}`
if (url.host.split(".")[0] === "pbs" || url.host.split(".")[0] === "video") {
try {
const [, id, format, extra] = search.match(/(.*)\?format=(.*)&(.*)/)
const query = encodeURIComponent(`${id}.${format}?${extra}`)
return `${randomInstance}/pic${url.pathname}${query}`
} catch {
return `${randomInstance}/pic${url.pathname}${search}`
}
}
if (url.pathname.split("/").includes("tweets")) return `${randomInstance}${url.pathname.replace("/tweets", "")}${search}`
if (url.host == "t.co") return `${randomInstance}/t.co${url.pathname}`
return `${randomInstance}${url.pathname}${search}#m`
case "yattee":
return url.href.replace(/^https?:\/{2}/, "yattee://")
case "freetube":
return `freetube://https://youtu.be${url.pathname}${url.search}`.replace(/watch\?v=/, "")
case "simplyTranslate":
return `${randomInstance}/${url.search}`
case "libreTranslate":
return `${randomInstance}/${url.search}`
.replace(/(?<=\/?)sl/, "source")
.replace(/(?<=&)tl/, "target")
.replace(/(?<=&)text/, "q")
case "osm": {
if (initiator && initiator.host === "earth.google.com") return
const travelModes = {
driving: "fossgis_osrm_car",
walking: "fossgis_osrm_foot",
bicycling: "fossgis_osrm_bike",
transit: "fossgis_osrm_car", // not implemented on OSM, default to car.
}
function addressToLatLng(address) {
const xmlhttp = new XMLHttpRequest()
xmlhttp.timeout = 5000
http.ontimeout = () => {
return
}
http.onerror = () => {
return
}
xmlhttp.send()
http.onreadystatechange = () => {
if (xmlhttp.status === 200) {
const json = JSON.parse(xmlhttp.responseText)[0]
if (json) {
console.log("json", json)
return [`${json.lat},${json.lon}`, `${json.boundingbox[2]},${json.boundingbox[1]},${json.boundingbox[3]},${json.boundingbox[0]}`]
}
}
console.info("Error: Status is " + xmlhttp.status)
}
xmlhttp.open("GET", `https://nominatim.openstreetmap.org/search/${address}?format=json&limit=1`, false)
}
let mapCentre = "#"
let prefs = {}
const mapCentreData = convertMapCentre()
if (mapCentreData[0] && mapCentreData[1] && mapCentreData[2]) mapCentre = `#map=${mapCentreData[0]}/${mapCentreData[1]}/${mapCentreData[2]}`
if (url.searchParams.get("layer")) prefs.layers = osmLayers[url.searchParams.get("layer")]
if (url.pathname.includes("/embed")) {
// Handle Google Maps Embed API
// https://www.google.com/maps/embed/v1/place?key=AIzaSyD4iE2xVSpkLLOXoyqT-RuPwURN3ddScAI&q=Eiffel+Tower,Paris+France
//console.log("embed life")
let query = ""
if (url.searchParams.has("q")) query = url.searchParams.get("q")
else if (url.searchParams.has("query")) query = url.searchParams.has("query")
else if (url.searchParams.has("pb"))
try {
query = url.searchParams.get("pb").split(/!2s(.*?)!/)[1]
} catch (error) {
console.error(error)
} // Unable to find map marker in URL.
let [coords, boundingbox] = addressToLatLng(query)
prefs.bbox = boundingbox
prefs.marker = coords
prefs.layer = "mapnik"
let prefsEncoded = new URLSearchParams(prefs).toString()
return `${randomInstance}/export/embed.html?${prefsEncoded}`
} else if (url.pathname.includes("/dir")) {
// Handle Google Maps Directions
// https://www.google.com/maps/dir/?api=1&origin=Space+Needle+Seattle+WA&destination=Pike+Place+Market+Seattle+WA&travelmode=bicycling
let travMod = url.searchParams.get("travelmode")
if (url.searchParams.has("travelmode")) prefs.engine = travelModes[travMod]
let orgVal = url.searchParams.get("origin")
let destVal = url.searchParams.get("destination")
let org = addressToLatLng(orgVal)
let dest = addressToLatLng(destVal)
prefs.route = `${org};${dest}`
let prefsEncoded = new URLSearchParams(prefs).toString()
return `${randomInstance}/directions?${prefsEncoded}${mapCentre}`
} else if (url.pathname.includes("data=") && url.pathname.match(dataLatLngRegex)) {
// Get marker from data attribute
// https://www.google.com/maps/place/41%C2%B001'58.2%22N+40%C2%B029'18.2%22E/@41.032833,40.4862063,17z/data=!3m1!4b1!4m6!3m5!1s0x0:0xf64286eaf72fc49d!7e2!8m2!3d41.0328329!4d40.4883948
//console.log("data life")
let [, mlat, mlon] = url.pathname.match(dataLatLngRegex)
return `${randomInstance}/search?query=${mlat}%2C${mlon}`
} else if (url.searchParams.has("ll")) {
// Get marker from ll param
// https://maps.google.com/?ll=38.882147,-76.99017
//console.log("ll life")
const [mlat, mlon] = url.searchParams.get("ll").split(",")
return `${randomInstance}/search?query=${mlat}%2C${mlon}`
} else if (url.searchParams.has("viewpoint")) {
// Get marker from viewpoint param.
// https://www.google.com/maps/@?api=1&map_action=pano&viewpoint=48.857832,2.295226&heading=-45&pitch=38&fov=80
//console.log("viewpoint life")
const [mlat, mlon] = url.searchParams.get("viewpoint").split(",")
return `${randomInstance}/search?query=${mlat}%2C${mlon}`
} else {
// Use query as search if present.
//console.log("normal life")
let query
if (url.searchParams.has("q")) query = url.searchParams.get("q")
else if (url.searchParams.has("query")) query = url.searchParams.get("query")
else if (url.pathname.match(placeRegex)) query = url.pathname.match(placeRegex)[1]
let prefsEncoded = new URLSearchParams(prefs).toString()
if (query) return `${randomInstance}/search?query="${query}${mapCentre}&${prefsEncoded}`
}
let prefsEncoded = new URLSearchParams(prefs).toString()
console.log("mapCentre", mapCentre)
console.log("prefs", prefs)
console.log("prefsEncoded", prefsEncoded)
return `${randomInstance}/${mapCentre}&${prefsEncoded}`
}
case "facil": {
if (initiator && initiator.host === "earth.google.com") return
const travelModes = {
driving: "car",
walking: "pedestrian",
bicycling: "bicycle",
transit: "car", // not implemented on Facil, default to car.
}
const mapCentreData = convertMapCentre()
let mapCentre = "#"
if (mapCentreData[0] && mapCentreData[1] && mapCentreData[2]) mapCentre = `#${mapCentreData[0]}/${mapCentreData[1]}/${mapCentreData[2]}`
if (url.pathname.includes("/embed")) {
// Handle Google Maps Embed API
// https://www.google.com/maps/embed/v1/place?key=AIzaSyD4iE2xVSpkLLOXoyqT-RuPwURN3ddScAI&q=Eiffel+Tower,Paris+France
//console.log("embed life")
let query = ""
if (url.searchParams.has("q")) query = url.searchParams.get("q")
else if (url.searchParams.has("query")) query = url.searchParams.has("query")
else if (url.searchParams.has("pb"))
try {
query = url.searchParams.get("pb").split(/!2s(.*?)!/)[1]
} catch (error) {
console.error(error)
} // Unable to find map marker in URL.
return `${randomInstance}/#q=${query}`
} else if (url.pathname.includes("/dir")) {
// Handle Google Maps Directions
// https://www.google.com/maps/dir/?api=1&origin=Space+Needle+Seattle+WA&destination=Pike+Place+Market+Seattle+WA&travelmode=bicycling
let travMod = url.searchParams.get("travelmode")
let orgVal = url.searchParams.get("origin")
let destVal = url.searchParams.get("destination")
return `${randomInstance}/#q=${orgVal}%20to%20${destVal}%20by%20${travelModes[travMod]}`
} else if (url.pathname.includes("data=") && url.pathname.match(dataLatLngRegex)) {
// Get marker from data attribute
// https://www.google.com/maps/place/41%C2%B001'58.2%22N+40%C2%B029'18.2%22E/@41.032833,40.4862063,17z/data=!3m1!4b1!4m6!3m5!1s0x0:0xf64286eaf72fc49d!7e2!8m2!3d41.0328329!4d40.4883948
//console.log("data life")
let [, mlat, mlon] = url.pathname.match(dataLatLngRegex)
return `${randomInstance}/#q=${mlat}%2C${mlon}`
} else if (url.searchParams.has("ll")) {
// Get marker from ll param
// https://maps.google.com/?ll=38.882147,-76.99017
//console.log("ll life")
const [mlat, mlon] = url.searchParams.get("ll").split(",")
return `${randomInstance}/#q=${mlat}%2C${mlon}`
} else if (url.searchParams.has("viewpoint")) {
// Get marker from viewpoint param.
// https://www.google.com/maps/@?api=1&map_action=pano&viewpoint=48.857832,2.295226&heading=-45&pitch=38&fov=80
//console.log("viewpoint life")
const [mlat, mlon] = url.searchParams.get("viewpoint").split(",")
return `${randomInstance}/#q=${mlat}%2C${mlon}`
} else {
// Use query as search if present.
//console.log("normal life")
let query
if (url.searchParams.has("q")) query = url.searchParams.get("q")
else if (url.searchParams.has("query")) query = url.searchParams.get("query")
else if (url.pathname.match(placeRegex)) query = url.pathname.match(placeRegex)[1]
if (query) return `${randomInstance}/${mapCentre}/Mpnk/${query}`
}
}
case "lingva":
let params_arr = url.search.split("&")
params_arr[0] = params_arr[0].substring(1)
let params = {}
for (let i = 0; i < params_arr.length; i++) {
let pair = params_arr[i].split("=")
params[pair[0]] = pair[1]
}
if (params.sl && params.tl && params.text) {
return `${randomInstance}/${params.sl}/${params.tl}/${params.text}`
}
return randomInstance
case "breezeWiki":
let wiki,
urlpath = ""
if (url.hostname.match(/^[a-zA-Z0-9-]+\.fandom\.com/)) {
wiki = url.hostname.match(/^[a-zA-Z0-9-]+(?=\.fandom\.com)/)
if (wiki == "www" || !wiki) wiki = ""
else wiki = `/${wiki}`;
urlpath = url.pathname
} else {
wiki = url.pathname.match(/(?<=wiki\/w:c:)[a-zA-Z0-9-]+(?=:)/)
if (!wiki) wiki = ""
else {
wiki = "/" + wiki + "/wiki/"
urlpath = url.pathname.match(/(?<=wiki\/w:c:[a-zA-Z0-9-]+:).+/)
}
}
if (url.href.search(/Special:Search\?query/) > -1) return `${randomInstance}${wiki}${urlpath}${url.search}`.replace(/Special:Search\?query/, "search?q").replace(/\/wiki/, "")
else return `${randomInstance}${wiki}${urlpath}${url.search}`
case "rimgo":
if (url.href.search(/^https?:\/{2}(?:[im]\.)?stack\./) > -1) return `${randomInstance}/stack${url.pathname}${url.search}`
else return `${randomInstance}${url.pathname}${url.search}`
case "libreddit":
const subdomain = url.hostname.match(/^(?:(?:external-)?preview|i)(?=\.redd\.it)/)
if (!subdomain) return `${randomInstance}${url.pathname}${url.search}`
switch (subdomain[0]) {
case "preview":
return `${randomInstance}/preview/pre${url.pathname}${url.search}`
case "external-preview":
return `${randomInstance}/preview/external-pre${url.pathname}${url.search}`
case "i":
return `${randomInstance}/img${url.pathname}`
}
case "teddit":
if (/^(?:(?:external-)?preview|i)\.redd\.it/.test(url.hostname)) {
if (url.search == "") return `${randomInstance}${url.pathname}?teddit_proxy=${url.hostname}`
else return `${randomInstance}${url.pathname}${url.search}&teddit_proxy=${url.hostname}`
}
return `${randomInstance}${url.pathname}${url.search}`
case "simpleertube":
return `${randomInstance}/${url.hostname}${url.pathname}${url.search}`
default:
return `${randomInstance}${url.pathname}${url.search} `
}
}
function computeService(url, returnFrontend) {
return new Promise(resolve => {
fetch("/config.json")
.then(response => response.text())
.then(configData => {
const config = JSON.parse(configData)
browser.storage.local.get(["redirects", "options"], r => {
const redirects = r.redirects
const options = r.options
for (const service in config.services) {
if (regexArray(service, url, config)) {
resolve(service)
return
} else {
for (const frontend in config.services[service].frontends) {
if (all(service, frontend, options, config, redirects).includes(utils.protocolHost(url))) {
if (returnFrontend) resolve([service, frontend, utils.protocolHost(url)])
else resolve(service)
return
}
}
}
}
resolve()
})
})
})
}
function switchInstance(url) {
return new Promise(async resolve => {
await init()
const protocolHost = utils.protocolHost(url)
for (const service in config.services) {
if (!all(service, null, options, config, redirects).includes(protocolHost)) continue
let instancesList = []
if (Object.keys(config.services[service].frontends).length == 1) {
const frontend = Object.keys(config.services[service].frontends)[0]
for (const network in options[frontend]) {
instancesList.push(...[...options[frontend][network].enabled, ...options[frontend][network].custom])
}
} else {
const frontend = options[service].frontend
for (const network in options[frontend]) {
instancesList.push(...[...options[frontend][network].enabled, ...options[frontend][network].custom])
}
}
let oldInstance
const i = instancesList.indexOf(protocolHost)
if (i > -1) {
oldInstance = instancesList[i]
instancesList.splice(i, 1)
}
if (instancesList.length === 0) {
resolve()
return
}
const randomInstance = utils.getRandomInstance(instancesList)
const oldUrl = `${oldInstance}${url.pathname}${url.search} `
// This is to make instance switching work when the instance depends on the pathname, eg https://darmarit.org/searx
// Doesn't work because of .includes array method, not a top priotiry atm
resolve(oldUrl.replace(oldInstance, randomInstance))
return
}
resolve()
})
}
function reverse(url, urlString) {
return new Promise(async resolve => {
await init()
let protocolHost
if (!urlString) protocolHost = utils.protocolHost(url)
else protocolHost = url.match(/https?:\/{2}(?:[^\s\/]+\.)+[a-zA-Z0-9]+/)[0]
for (const service in config.services) {
if (!all(service, null, options, config, redirects).includes(protocolHost)) continue
switch (service) {
case "instagram":
case "youtube":
case "imdb":
case "imgur":
case "tiktok":
case "twitter":
case "reddit":
case "imdb":
case "quora":
case "medium":
case "fandom":
let regex = url.pathname.match(/^\/([a-zA-Z0-9-]+)\/wiki\/([a-zA-Z0-9-]+)/)
if (regex) {
resolve(`https://${regex[1]}.fandom.com/wiki/${regex[2]}`)
return
}
resolve()
return
default:
resolve()
return
}
}
resolve()
})
}
function setRedirects(passedRedirects) {
return new Promise(resolve => {
fetch("/config.json")
.then(response => response.text())
.then(configData => {
browser.storage.local.get(/* [ */ "options" /* , "blacklists"] */, async r => {
let redirects = passedRedirects
let options = r.options
const config = JSON.parse(configData)
let targets = {}
for (const service in config.services) {
if (config.services[service].targets == "datajson") {
targets[service] = redirects[service]
delete redirects[service]
}
for (const frontend in config.services[service].frontends) {
if (config.services[service].frontends[frontend].instanceList) {
for (const network in config.networks) {
for (const instance of options[frontend][network].enabled) {
let i = redirects[frontend][network].indexOf(instance)
if (i < 0) options[frontend][network].enabled.splice(i, 1)
}
}
}
}
/*
for (const frontend in config.services[service].frontends) {
if (config.services[service].frontends[frontend].instanceList) {
for (const network in config.networks) {
options[frontend][network].enabled = redirects[frontend][network]
}
for (const blacklist in r.blacklists) {
for (const instance of blacklist) {
let i = options[frontend].clearnet.enabled.indexOf(instance)
if (i > -1) options[frontend].clearnet.enabled.splice(i, 1)
}
}
}
}
*/
// The above will be implemented with https://github.com/libredirect/libredirect/issues/334
}
for (const frontend in redirects) {
let exists = false
for (const service in config.services) if (config.services[service].frontends[frontend]) exists = true
if (!exists) delete redirects[frontend]
else for (const network in redirects[frontend]) if (!config.networks[network]) delete redirects[frontend][network]
}
browser.storage.local.set({ redirects, targets, options }, () => resolve())
})
})
})
}
function initDefaults() {
return new Promise(resolve => {
fetch("/instances/data.json")
.then(response => response.text())
.then(data => {
fetch("/config.json")
.then(response => response.text())
.then(configData => {
browser.storage.local.get(["options", "blacklists"], r => {
let redirects = JSON.parse(data)
let options = r.options
let targets = {}
let config = JSON.parse(configData)
const localstorage = {}
for (const service in config.services) {
options[service] = {}
if (config.services[service].targets == "datajson") {
targets[service] = redirects[service]
delete redirects[service]
}
for (const defaultOption in config.services[service].options) options[service][defaultOption] = config.services[service].options[defaultOption]
for (const frontend in config.services[service].frontends) {
if (config.services[service].frontends[frontend].instanceList) {
options[frontend] = {}
for (const network in config.networks) {
options[frontend][network] = {}
options[frontend][network].enabled = JSON.parse(data)[frontend][network]
options[frontend][network].custom = []
}
for (const blacklist in r.blacklists) {
for (const instance of r.blacklists[blacklist]) {
let i = options[frontend].clearnet.enabled.indexOf(instance)
if (i > -1) options[frontend].clearnet.enabled.splice(i, 1)
}
}
}
}
}
browser.storage.local.set({ redirects, options, targets, localstorage })
resolve()
})
})
})
})
}
function upgradeOptions() {
return new Promise(resolve => {
fetch("/config.json")
.then(response => response.text())
.then(configData => {
browser.storage.local.get(null, r => {
let options = r.options
const config = JSON.parse(configData)
options.exceptions = r.exceptions
if (r.theme != "DEFAULT") options.theme = r.theme
options.popupServices = r.popupFrontends
let tmp = options.popupServices.indexOf("tikTok")
if (tmp > -1) {
options.popupServices.splice(tmp, 1)
options.popupServices.push("tiktok")
}
tmp = options.popupServices.indexOf("sendTarget")
if (tmp > -1) {
options.popupServices.splice(tmp, 1)
options.popupServices.push("sendFiles")
}
switch (r.onlyEmbeddedVideo) {
case "onlyNotEmbedded":
options.youtube.redirectType = "main_frame"
case "onlyEmbedded":
options.youtube.redirectType = "sub_frame"
case "both":
options.youtube.redirectType = "both"
}
for (const service in config.services) {
let oldService
switch (service) {
case "tiktok":
oldService = "tikTok"
break
case "sendFiles":
oldService = "sendTarget"
break
default:
oldService = service
}
options[service].enabled = !r["disable" + utils.camelCase(oldService)]
if (r[oldService + "Frontend"]) {
if (r[oldService + "Frontend"] == "yatte") options[service].frontend = "yattee"
else options[service].frontend = r[oldService + "Frontend"]
}
if (r[oldService + "RedirectType"]) options[service].redirectType = r[oldService + "RedirectType"]
for (const frontend in config.services[service].frontends) {
for (const network in config.networks) {
let protocol
if (network == "clearnet") protocol = "normal"
else protocol = network
if (r[frontend + utils.camelCase(protocol) + "RedirectsChecks"]) {
options[frontend][network].enabled = r[frontend + utils.camelCase(protocol) + "RedirectsChecks"]
options[frontend][network].custom = r[frontend + utils.camelCase(protocol) + "CustomRedirects"]
for (const instance of options[frontend][network].enabled) {
let i = r.redirects[frontend][network].indexOf(instance)
if (i < 0) options[frontend][network].enabled.splice(i, 1)
}
}
}
}
}
browser.storage.local.set({ options }, () => resolve())
})
})
})
}
function processUpdate() {
return new Promise(resolve => {
fetch("/instances/data.json")
.then(response => response.text())
.then(data => {
fetch("/config.json")
.then(response => response.text())
.then(configData => {
browser.storage.local.get(["options", "blacklists", "targets"], r => {
let redirects = JSON.parse(data)
let options = r.options
let targets = r.targets
let config = JSON.parse(configData)
for (const service in config.services) {
if (!options[service]) options[service] = {}
if (config.services[service].targets == "datajson") {
targets[service] = redirects[service]
delete redirects[service]
}
for (const defaultOption in config.services[service].options) {
if (options[service][defaultOption] === undefined) {
options[service][defaultOption] = config.services[service].options[defaultOption]
}
}
for (const frontend in config.services[service].frontends) {
if (config.services[service].frontends[frontend].instanceList) {
if (!options[frontend]) options[frontend] = {}
for (const network in config.networks) {
if (!options[frontend][network]) {
options[frontend][network] = {}
options[frontend][network].enabled = JSON.parse(data)[frontend][network]
options[frontend][network].custom = []
if (network == "clearnet") {
for (const blacklist in r.blacklists) {
for (const instance of r.blacklists[blacklist]) {
let i = options[frontend].clearnet.enabled.indexOf(instance)
if (i > -1) options[frontend].clearnet.enabled.splice(i, 1)
}
}
}
} else {
for (const instance of options[frontend][network].enabled) {
let i = redirects[frontend][network].indexOf(instance)
if (i < 0) options[frontend][network].enabled.splice(i, 1)
}
}
}
}
}
}
browser.storage.local.set({ redirects, options, targets })
resolve()
})
})
})
})
}
// For websites that have a strict policy that would not normally allow these frontends to be embedded within the website.
function modifyContentSecurityPolicy(details) {
let isChanged = false
if (details.type == "main_frame") {
for (const header in details.responseHeaders) {
if (details.responseHeaders[header].name == "content-security-policy") {
let instancesList = []
for (const service in config.services) {
if (config.services[service].embeddable) {
for (const frontend in config.services[service].frontends) {
if (config.services[service].frontends[frontend].embeddable) {
for (const network in config.networks) {
instancesList.push(...options[frontend][network].enabled, ...options[frontend][network].custom)
}
}
}
}
}
let securityPolicyList = details.responseHeaders[header].value.split(";")
for (const i in securityPolicyList) securityPolicyList[i] = securityPolicyList[i].trim()
let newSecurity = ""
for (const item of securityPolicyList) {
if (item.trim() == "") continue
let regex = item.match(/([a-z-]{0,}) (.*)/)
if (regex == null) continue
let [, key, vals] = regex
if (key == "frame-src") vals = vals + " " + instancesList.join(" ")
newSecurity += key + " " + vals + "; "
}
details.responseHeaders[header].value = newSecurity
isChanged = true
}
}
if (isChanged) return { responseHeaders: details.responseHeaders }
}
}
export default {
redirect,
computeService,
switchInstance,
reverse,
setRedirects,
initDefaults,
upgradeOptions,
processUpdate,
modifyContentSecurityPolicy,
}
|