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
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
|
import utils from "./utils.js"
const isChrome = browser.runtime.getBrowserInfo === undefined
window.browser = window.browser || window.chrome
let config, options
async function init() {
options = await utils.getOptions()
config = await utils.getConfig()
}
init()
browser.storage.onChanged.addListener(init)
function all(service, frontend, options, config) {
let instances = []
if (!frontend) {
for (const frontend in config.services[service].frontends) {
if (options[frontend]) {
instances.push(...options[frontend])
}
}
} else if (options[frontend]) {
instances = options[frontend]
}
return instances
}
/**
* @param {string} service
* @param {URL} url
* @param {{}} config
* @param {{}} options
* @param {string} frontend
*/
function regexArray(service, url, config, options, frontend) {
let targetList = config.services[service].targets
if (frontend && "excludeTargets" in config.services[service].frontends[frontend]) {
if (service !== "search" || !options["search"].redirectGoogle) {
targetList = targetList.filter(
val => !config.services[service].frontends[frontend].excludeTargets.includes(targetList.indexOf(val))
)
}
}
for (const targetString in targetList) {
const target = new RegExp(targetList[targetString])
if (target.test(url.href)) return true
}
return false
}
/**
* @param {URL} url
* @param {string} frontend
* @param {string} randomInstance
* @returns {undefined|string}
*/
function rewrite(url, originUrl, frontend, randomInstance) {
switch (frontend) {
case "hyperpipe":
for (const key of [...url.searchParams.keys()]) if (key !== "q") url.searchParams.delete(key)
return `${randomInstance}${url.pathname}${url.search}`.replace(/\/search\?q=.*/, searchQuery =>
searchQuery.replace("?q=", "/")
)
case "searx":
case "searxng":
for (const key of [...url.searchParams.keys()]) if (key !== "q") url.searchParams.delete(key)
return `${randomInstance}/${url.search}`
case "whoogle":
for (const key of [...url.searchParams.keys()]) if (key !== "q") url.searchParams.delete(key)
return `${randomInstance}/search${url.search}`
case "4get": {
const s = url.searchParams.get("q")
if (s !== null) return `${randomInstance}/web?s=${encodeURIComponent(s)}`
return randomInstance
}
case "librey":
for (const key in url.searchParams.keys()) if (key != "q") url.searchParams.delete(key)
return `${randomInstance}/search.php${url.search}`
case "yattee":
url.searchParams.delete("si")
return url.href.replace(/^https?:\/{2}/, "yattee://")
case "freetube":
url.searchParams.delete("si")
return "freetube://" + url.href
case "freetubePwa":
url.searchParams.delete("si")
return "freetube://" + url.href
case "poketube": {
url.searchParams.delete("si")
if (url.pathname.startsWith("/channel")) {
const reg = /\/channel\/(.*)\/?$/.exec(url.pathname)
if (reg) {
const id = reg[1]
return `${randomInstance}/channel?id=${id}${url.search}`
}
}
if (/\/@[a-z]+\//.exec(url.pathname)) return randomInstance
return `${randomInstance}${url.pathname}${url.search}`
}
case "libMedium":
case "scribe": {
const regex = url.hostname.match(/^(link|cdn-images-\d+|.*)\.medium\.com/)
if (regex && regex.length > 1) {
const subdomain = regex[1]
if (subdomain != "link" || !subdomain.startsWith("cdn-images")) {
return `${randomInstance}/@${subdomain}${url.pathname}${url.search}`
}
}
return `${randomInstance}${url.pathname}${url.search}`
}
case "translite":
case "simplyTranslate":
return `${randomInstance}/${url.search}`
case "send":
case "mozhi":
return randomInstance
case "libreTranslate":
return `${randomInstance}/${url.search.replace("sl", "source").replace("tl", "target").replace("text", "q")}`
case "osm": {
if (originUrl && originUrl.host === "earth.google.com") return randomInstance
let prefs = { layers: "mapnik" }
let mapCentre = "#"
const mapCentreData = utils.convertMapCentre(url)
if (mapCentreData.zoom && mapCentreData.lon && mapCentreData.lat) {
mapCentre = `#map=${mapCentreData.zoom}/${mapCentreData.lon}/${mapCentreData.lat}`
}
if (url.pathname.includes("/embed")) {
// https://www.google.com/maps/embed/v1/place?key=AIzaSyD4iE2xVSpkLLOXoyqT-RuPwURN3ddScAI&q=Eiffel+Tower,Paris+France
const query = utils.getQuery(url)
let { coordinate, boundingbox } = utils.addressToLatLng(query)
prefs.bbox = boundingbox
prefs.marker = coordinate
return `${randomInstance}/export/embed.html?${utils.prefsEncoded(prefs)}`
}
if (url.pathname.includes("/dir")) {
if (url.searchParams.has("travelmode")) {
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.
}
prefs.engine = travelModes[url.searchParams.get("travelmode")]
}
const regex1 = /\/dir\/([^@/]+)\/([^@/]+)\/@-?\d[0-9.]*,-?\d[0-9.]*,\d{1,2}[.z]/.exec(url.pathname)
const regex2 = /\/dir\/([^@/]+)\//.exec(url.pathname)
if (regex1) {
// https://www.google.com/maps/dir/92+Rue+Moncey,+69003+Lyon,+France/M%C3%A9dip%C3%B4le+Lyon-Villeurbanne/@45.760254,4.8486298,13z?travelmode=bicycling
const origin = utils.addressToLatLng(decodeURIComponent(regex1[1])).coordinate ?? ""
const destination = utils.addressToLatLng(decodeURIComponent(regex1[2])).coordinate ?? ""
prefs.route = `${origin};${destination}`
} else if (regex2) {
// https://www.google.com/maps/dir/92+Rue+Moncey,+69003+Lyon,+France/@45.760254,4.8486298,13z?travelmode=bicycling
const origin = utils.addressToLatLng(decodeURIComponent(regex2[1])).coordinate ?? ""
prefs.route = `${origin};`
} else {
// https://www.google.com/maps/dir/?api=1&origin=Space+Needle+Seattle+WA&destination=Pike+Place+Market+Seattle+WA&travelmode=bicycling
const origin = utils.addressToLatLng(url.searchParams.get("origin")).coordinate ?? ""
const destination = utils.addressToLatLng(url.searchParams.get("destination")).coordinate ?? ""
prefs.route = `${origin};${destination}`
}
return `${randomInstance}/directions?${utils.prefsEncoded(prefs)}${mapCentre}`
}
const placeRegex = /\/place\/(.*?)\//
if (url.pathname.match(placeRegex)) {
// https://www.google.com/maps/place/H%C3%B4tel+de+Londres+Eiffel/@40.9845265,28.7081268,14z
const query = url.pathname.match(placeRegex)[1]
return `${randomInstance}/search?query=${query}${mapCentre}`
}
if (url.searchParams.has("ll")) {
// https://maps.google.com/?ll=38.882147,-76.99017
const [mlat, mlon] = url.searchParams.get("ll").split(",")
return `${randomInstance}/search?query=${mlat}%2C${mlon}`
}
if (url.searchParams.has("viewpoint")) {
// https://www.google.com/maps/@?api=1&map_action=pano&viewpoint=48.857832,2.295226&heading=-45&pitch=38&fov=80
const [mlat, mlon] = url.searchParams.get("viewpoint").split(",")
return `${randomInstance}/search?query=${mlat}%2C${mlon}`
}
const query = utils.getQuery(url)
if (query) return `${randomInstance}/search?query="${query}${mapCentre}&${utils.prefsEncoded(prefs)}`
return `${randomInstance}/${mapCentre}&${utils.prefsEncoded(prefs)}`
}
case "breezeWiki": {
let wiki,
urlpath = ""
if (url.hostname.match(/^[a-zA-Z0-9-]+\.(?:fandom|wikia)\.com/)) {
wiki = url.hostname.match(/^[a-zA-Z0-9-]+(?=\.(?:fandom|wikia)\.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/, "")
}
return `${randomInstance}${wiki}${urlpath}${url.search}`
}
case "rimgo":
if (url.href.search(/^https?:\/{2}(?:[im]\.)?stack\./) > -1)
return `${randomInstance}/stack${url.pathname}${url.search}`
return `${randomInstance}${url.pathname}${url.search}`
case "redlib":
case "libreddit": {
const subdomain = url.hostname.match(/^(?:((?:external-)?preview|i)\.)?redd\.it/)
if (!subdomain) return `${randomInstance}${url.pathname}${url.search}`
switch (subdomain[1]) {
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}`
default:
return `${randomInstance}/comments${url.pathname}`
}
return randomInstance
}
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 "eddrit":
if (/^(?:(?:external-)?preview|i)\.redd\.it/.test(url.hostname)) return randomInstance
return `${randomInstance}${url.pathname}${url.search}`
case "neuters": {
const p = url.pathname
if (p.startsWith("/article/") || p.startsWith("/pf/") || p.startsWith("/arc/") || p.startsWith("/resizer/")) {
return randomInstance
}
return `${randomInstance}${p}`
}
case "dumb":
if (url.pathname.endsWith("-lyrics")) return `${randomInstance}${url.pathname}`
return `${randomInstance}${url.pathname}${url.search}`
case "intellectual":
return `${randomInstance}${url.pathname}${url.search}`
case "ruralDictionary":
if (!url.pathname.includes("/define.php") && !url.pathname.includes("/random.php") && url.pathname != "/")
return randomInstance
return `${randomInstance}${url.pathname}${url.search}`
case "anonymousOverflow": {
if (url.hostname == "stackoverflow.com") {
const threadID = /^\/a\/(\d+)\/?/.exec(url.pathname)
if (threadID) return `${randomInstance}/questions/${threadID[1]}${url.search}`
return `${randomInstance}${url.pathname}${url.search}`
}
if (url.pathname == "/" || url.pathname == "") {
// https://stackexchange.com or https://superuser.com
return `${randomInstance}${url.pathname}${url.search}`
}
const regex = url.href.match(/https?:\/{2}(?:([a-zA-Z0-9-]+)\.)?stackexchange\.com\//)
if (regex && regex.length > 1) {
const subdomain = regex[1]
return `${randomInstance}/exchange/${subdomain}${url.pathname}${url.search}`
}
const notExchangeRegex = url.hostname.match(
/(?:[a-zA-Z]+\.)?(?:askubuntu\.com|mathoverflow\.net|serverfault\.com|stackapps\.com|superuser\.com|stackoverflow\.com)/
)
if (notExchangeRegex) {
return `${randomInstance}/exchange/${notExchangeRegex[0]}${url.pathname}${url.search}`
}
return `${randomInstance}${url.pathname}${url.search}`
}
case "biblioReads":
return `${randomInstance}${url.pathname}${url.search}`
case "wikiless": {
let hostSplit = url.host.split(".")
// wikiless doesn't have mobile view support yet
if (hostSplit[0] != "wikipedia" && hostSplit[0] != "www") {
if (hostSplit[0] == "m") url.searchParams.append("mobileaction", "toggle_view_mobile")
else url.searchParams.append("lang", hostSplit[0])
if (hostSplit[1] == "m") url.searchParams.append("mobileaction", "toggle_view_mobile")
}
return `${randomInstance}${url.pathname}${url.search}${url.hash}`
}
case "proxiTok":
if (url.pathname.startsWith("/email")) return randomInstance
return `${randomInstance}${url.pathname}${url.search}`
case "waybackClassic": {
const regex = /^\/\web\/(?:[0-9]+)?\*\/(.*)/.exec(url.pathname)
if (regex) {
const link = regex[1]
return `${randomInstance}/cgi-bin/history.cgi?utf8=✓&q=${encodeURIComponent(link)}`
}
const regex2 = /(^\/\web\/([0-9]+)\/.*)/.exec(url.pathname)
if (regex2) {
let link = regex2[1]
link = link.replace(regex2[2], regex2[2] + "if_")
return `https://web.archive.org${link}`
}
return
}
case "gothub":
if (url.hostname == "gist.github.com") return `${randomInstance}/gist${url.pathname}${url.search}`
if (url.hostname == "raw.githubusercontent.com") return `${randomInstance}/raw${url.pathname}${url.search}`
return `${randomInstance}${url.pathname}${url.search}`
case "mikuInvidious":
if (url.hostname == "bilibili.com" || url.hostname == "www.bilibili.com" || url.hostname == "b23.tv")
return `${randomInstance}${url.pathname}${url.search}`
if (url.hostname == "space.bilibili.com") return `${randomInstance}/space${url.pathname}${url.search}`
case "tent": {
if (url.hostname == "bandcamp.com" && url.pathname == "/search") {
const query = url.searchParams.get("q")
return `${randomInstance}/search.php?query=${encodeURIComponent(query)}`
}
if (url.hostname.endsWith("bandcamp.com")) {
const regex = /^(.*)\.bandcamp\.com/.exec(url.hostname)
const artist = regex[1]
if (url.pathname == "/" || url.pathname == "/music") {
return `${randomInstance}/artist.php?name=${artist}`
} else {
const regex = /^\/(.*)\/(.*)/.exec(url.pathname)
if (regex) {
const type = regex[1]
const name = regex[2]
return `${randomInstance}/release.php?artist=${artist}&type=${type}&name=${name}`
}
}
}
if (url.hostname == "f4.bcbits.com") {
const regex = /\/img\/(.*)/.exec(url.pathname)
const image = regex[1]
return `${randomInstance}/image.php?file=${image}`
}
if (url.hostname == "t4.bcbits.com") {
const regex = /\/stream\/(.*)\/(.*)\/(.*)/.exec(url.pathname)
if (regex) {
const directory = regex[1]
const format = regex[2]
const file = regex[3]
const token = url.searchParams.get("token")
return `${randomInstance}/audio.php/?directory=${directory}&format=${format}&file=${file}&token=${encodeURIComponent(token)}`
}
}
}
case "binternet":
if (url.hostname == "i.pinimg.com") return `${randomInstance}/image_proxy.php?url=${url.href}`
return `${randomInstance}${url.pathname}${url.search}`
case "laboratory": {
let path = url.pathname
if (path == "/") path = ""
return `${randomInstance}/${url.hostname}${path}${url.search}`
}
case "quetre": {
const regex = /([a-z]+)\.quora\.com/.exec(url.hostname)
if (regex) {
const lang = regex[1]
url.searchParams.append("lang", lang)
return `${randomInstance}${url.pathname}${url.search}`
}
return `${randomInstance}${url.pathname}${url.search}`
}
case "liteXiv":
case "pixivFe": {
const regex = /\/[a-z]{1,3}\/(.*)/.exec(url.pathname)
if (regex) {
const path = regex[1]
return `${randomInstance}/${path}${url.search}`
}
return `${randomInstance}${url.pathname}${url.search}`
}
case "invidious": {
url.searchParams.delete("si")
if (url.hostname == "youtu.be" || (url.hostname.endsWith("youtube.com") && url.pathname.startsWith("/live"))) {
const watch = url.pathname.substring(url.pathname.lastIndexOf("/") + 1)
return `${randomInstance}/watch?v=${watch}${url.search.replace("?", "&")}`
}
if (url.hostname.endsWith("youtube.com") && url.pathname.startsWith("/redirect?")) return url.href
return `${randomInstance}${url.pathname}${url.search}`
}
case "freetubeMusic": {
if (url.hostname == "youtu.be" || (url.hostname.endsWith("youtube.com") && url.pathname.startsWith("/live"))) {
const watch = url.pathname.substring(url.pathname.lastIndexOf("/") + 1)
return `freetube://youtube.com/watch?v=${watch}`
}
return "freetube://" + url.href
}
case "invidiousMusic": {
if (url.hostname == "youtu.be" || (url.hostname.endsWith("youtube.com") && url.pathname.startsWith("/live"))) {
const watch = url.pathname.substring(url.pathname.lastIndexOf("/") + 1)
return `${randomInstance}/watch?v=${watch}`
}
return `${randomInstance}${url.pathname}${url.search}`
}
case "materialious": {
url.searchParams.delete("si")
if (url.hostname == "youtu.be" || (url.hostname.endsWith("youtube.com") && url.pathname.startsWith("/live"))) {
const watch = url.pathname.substring(url.pathname.lastIndexOf("/") + 1)
return `${randomInstance}/watch/${watch}${url.search.replace("?", "&")}`
}
if (url.hostname.endsWith("youtube.com")) {
if (url.pathname.startsWith("/watch")) {
if (url.searchParams.has("v")) {
const watch = url.searchParams.get("v")
url.searchParams.delete("v")
return `${randomInstance}/watch/${watch}${url.search.replace("?", "&")}`
}
return `${randomInstance}/watch/${url.search.replace("?", "&")}`
}
if (url.pathname.startsWith("/results")) {
if (url.searchParams.has("search_query")) {
const search = url.searchParams.get("search_query")
url.searchParams.delete("search_query")
return `${randomInstance}/search/${search}${url.search.replace("?", "&")}`
}
return `${randomInstance}/search/${url.search.replace("?", "&")}`
}
if (url.pathname.startsWith("/redirect?")) {
return url.href
}
}
return `${randomInstance}${url.pathname}${url.search}`
}
case "libremdb": {
if (url.pathname.startsWith("/Name")) {
for (const [key, value] of url.searchParams.entries()) {
return `${randomInstance}/title/${encodeURIComponent(key)}`
}
}
return `${randomInstance}${url.pathname}${url.search}`
}
case "tuboYoutube":
url.searchParams.delete("si")
if (url.pathname.startsWith("/channel")) return `${randomInstance}/channel?url=${encodeURIComponent(url.href)}`
if (url.pathname.startsWith("/watch")) return `${randomInstance}/stream?url=${encodeURIComponent(url.href)}`
return randomInstance
case "tuboSoundcloud":
if (url.pathname == "/") return `${randomInstance}?kiosk?serviceId=1`
if (url.pathname.match(/^\/[^\/]+(\/$|$)/)) return `${randomInstance}/channel?url=${encodeURIComponent(url.href)}`
if (url.pathname.match(/^\/[^\/]+\/[^\/]+/)) return `${randomInstance}/stream?url=${encodeURIComponent(url.href)}`
return randomInstance
case "twineo":
case "safetwitch":
if (url.hostname.startsWith("clips.")) return `${randomInstance}/clip${url.pathname}${url.search}`
return `${randomInstance}${url.pathname}${url.search}`
case "tekstoLibre":
return `${randomInstance}/?${url.pathname.slice(1)}`
case "skyview":
if (url.pathname == "/") return randomInstance
return `${randomInstance}?url=${encodeURIComponent(url.href)}`
case "nitter": {
let search = new URLSearchParams(url.search)
search.delete("ref_src")
search.delete("ref_url")
search.delete("s") // type of device that shared the link
search.delete("t") // some sort of tracking ID
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 "priviblur": {
if (url.hostname == "www.tumblr.com") return `${randomInstance}${url.pathname}${url.search}`
if (url.hostname.startsWith("assets")) return `${randomInstance}/tblr/assets${url.pathname}${url.search}`
if (url.hostname.startsWith("static")) return `${randomInstance}/tblr/static${url.pathname}${url.search}`
const reg = /^([0-9]+)\.media\.tumblr\.com/.exec(url.hostname) // *.media.tumblr.com
if (reg) return `${randomInstance}/tblr/media/${reg[1]}${url.pathname}${url.search}`
const blogregex = /^(?:www\.)?([a-z\d-]+)\.tumblr\.com/.exec(url.hostname) // <blog>.tumblr.com
if (blogregex) {
const blog_name = blogregex[1]
// Under the <blog>.tumblr.com domain posts are under a /post path
if (url.pathname.startsWith("/post"))
return `${randomInstance}/${blog_name}${url.pathname.slice(5)}${url.search}`
else return `${randomInstance}/${blog_name}${url.pathname}${url.search}`
}
return `${randomInstance}${url.pathname}${url.search}`
}
case "freetar":
if (url.pathname.startsWith("/search.php")) {
url.searchParams.set("search_term", url.searchParams.get("value"))
url.searchParams.delete("value")
url.searchParams.delete("search_type")
return `${randomInstance}/search${url.search}`
}
if (url.pathname.startsWith("/artist")) return
return `${randomInstance}${url.pathname}${url.search}`
case "ratAintTieba":
url.searchParams.delete("ie")
return `${randomInstance}${url.pathname}${url.search}`
case "shoelace": {
const reg = /^\/(?:(?:(?:[^\/])?\/post)|t)\/([^\/])/.exec(url.pathname)
if (reg) return `${randomInstance}/t/${reg[1]}${url.search}`
return `${randomInstance}${url.pathname}${url.search}`
}
case "skunkyArt": {
if (url.pathname.startsWith("/search")) return `${randomInstance}${url.pathname}${url.search}&type=all`
const artReg = /^\/(.*?)\/art\/(.*)\/?/.exec(url.pathname)
if (artReg) return `${randomInstance}/post/${artReg[1]}/${artReg[2]}${url.search}`
const userReg = /^\/([^\/]+)$/.exec(url.pathname)
if (userReg) return `${randomInstance}/group_user?q=${userReg[1]}&type=about`
const galleryReg = /^\/(.*?)\/gallery(\/$|$)$/.exec(url.pathname)
if (galleryReg) return `${randomInstance}/group_user?q=${galleryReg[1]}&type=gallery`
return `${randomInstance}${url.pathname}${url.search}`
}
case "ytify": {
if (url.pathname.startsWith("/watch"))
return `${randomInstance}/?s=${encodeURIComponent(url.searchParams.get("v"))}`
const channelReg = /\/channel\/([^\/]+)/.exec(url.pathname)
if (channelReg) return `${randomInstance}/list?channel=${channelReg[1]}`
if (url.pathname.startsWith("/playlist"))
return `${randomInstance}/list?playlists=${encodeURIComponent(url.searchParams.get("list"))}`
return `${randomInstance}${url.pathname}${url.search}`
}
case "koub":
if (url.pathname.startsWith("/view/") || url.pathname.startsWith("/stories/")) {
return `${randomInstance}${url.pathname}${url.search}`
}
const accountReg = /^\/([^\/]+)\/?$/.exec(url.pathname)
if (accountReg) return `${randomInstance}/account${url.pathname}${url.search}`
case "piped":
case "pipedMaterial":
case "cloudtube":
case "lightTube":
case "viewtube":
url.searchParams.delete("si")
default:
return `${randomInstance}${url.pathname}${url.search}`
}
}
/**
* @param {URL} url
* @param {string} type
* @param {URL} originUrl
* @param {URL} documentUrl
* @param {boolean} incognito
* @param {boolean} forceRedirection
* @returns {string | undefined}
*/
function redirect(url, type, originUrl, documentUrl, incognito, forceRedirection) {
if (type != "main_frame" && type != "sub_frame" && type != "image") return
let randomInstance
let frontend
if (!forceRedirection && options.redirectOnlyInIncognito == true && !incognito) return
for (const service in config.services) {
if (!forceRedirection && !options[service].enabled) continue
if (!forceRedirection && options[service].redirectOnlyInIncognito == true && !incognito) continue
frontend = options[service].frontend
if (
config.services[service].frontends[frontend].desktopApp &&
type != "main_frame" &&
options[service].redirectType != "main_frame"
)
frontend = options[service].embedFrontend
if (!regexArray(service, url, config, options, frontend)) {
frontend = null
continue
}
if (type != "main_frame" && documentUrl && options[service].redirectType == "sub_frame") {
if (regexArray(service, documentUrl, config, options, frontend)) {
return
}
}
if (
config.services[service].embeddable &&
type != options[service].redirectType &&
options[service].redirectType != "both"
) {
if (options[service].unsupportedUrls == "block") return "CANCEL"
return
}
let instanceList = options[frontend]
if (instanceList === undefined) break // should not happen if settings are correct
if (config.services[service].frontends[frontend].localhost && options[service].instance == "localhost") {
randomInstance = `http://${frontend}.localhost:8080`
} else if (instanceList.length === 0) {
return `https://no-instance.libredirect.invalid?frontend=${encodeURIComponent(frontend)}&url=${encodeURIComponent(url.href)}`
} else {
randomInstance = utils.getRandomInstance(instanceList)
}
if (originUrl && instanceList.includes(originUrl.origin)) {
if (type == "main_frame") return "BYPASSTAB"
else return null
}
break
}
if (!frontend) return
return rewrite(url, originUrl, frontend, randomInstance)
}
/**
* @param {URL} url
* @param {string} type
* @param {URL} originUrl
* @param {URL} documentUrl
* @param {boolean} incognito
* @param {boolean} forceRedirection
* @returns {Promise<string | undefined>}
*/
async function redirectAsync(url, type, originUrl, documentUrl, incognito, forceRedirection) {
await init()
return redirect(url, type, originUrl, documentUrl, incognito, forceRedirection)
}
/**
* @param {URL} url
*/
async function computeServiceFrontend(url) {
const config = await utils.getConfig()
const options = await utils.getOptions()
for (const service in config.services) {
if (regexArray(service, url, config, options)) {
return { service, frontend: null }
} else {
for (const frontend in config.services[service].frontends) {
const instances = all(service, frontend, options, config)
const i = instances.findIndex(instance => url.href.startsWith(instance))
if (i >= 0) {
return { service, frontend }
}
}
}
}
}
/**
* @param {URL} url
* @param {string} customService
*/
function switchInstance(url, customService) {
return new Promise(async resolve => {
let options = await utils.getOptions()
let config = await utils.getConfig()
const protocolHost = utils.protocolHost(url)
if (customService) {
const instancesList = options[options[customService].frontend]
if (instancesList !== undefined) {
const newInstance = utils.getNextInstance(url.origin, instancesList)
if (newInstance) {
return resolve(`${newInstance}${url.pathname}${url.search}`)
}
}
} else {
for (const service in config.services) {
let instancesList = options[options[service].frontend]
if (instancesList === undefined) continue
const index = instancesList.findIndex(instance => url.href.startsWith(instance))
if (index < 0) continue
instancesList.splice(index, 1)
if (instancesList.length === 0) return resolve()
const newInstance = utils.getNextInstance(url.origin, instancesList)
if (newInstance) {
return resolve(`${newInstance}${url.pathname}${url.search}`)
}
}
}
resolve()
})
}
/**
* @param {URL} url
*/
async function reverse(url) {
let options = await utils.getOptions()
let config = await utils.getConfig()
for (const service in config.services) {
let frontend = options[service].frontend
if (options[frontend] == undefined) continue
if (
options[frontend].findIndex(instance => url.href.startsWith(instance)) < 0 &&
!url.href.startsWith(`http://${frontend}.localhost:8080`)
)
continue
switch (service) {
case "youtube":
case "imdb":
case "imgur":
case "tiktok":
case "reddit":
case "imdb":
case "snopes":
case "urbanDictionary":
case "quora":
case "twitter":
case "medium":
return `${config.services[service].url}${url.pathname}${url.search}`
case "fandom": {
let regex = url.pathname.match(/^\/([a-zA-Z0-9-]+)\/wiki\/(.*)/)
if (regex) return `https://${regex[1]}.fandom.com/wiki/${regex[2]}`
return
}
case "wikipedia": {
const lang = url.searchParams.get("lang")
if (lang != null) {
return `https://${lang}.wikipedia.org${url.pathname}${url.search}${url.hash}`
}
return `https://wikipedia.org${url.pathname}${url.search}${url.hash}`
}
case "stackOverflow": {
if (url.pathname.startsWith("/questions/")) {
return `https://stackoverflow.com${url.pathname}${url.search}`
}
if (url.pathname.startsWith("/exchange/")) {
const regex = /\/exchange\/(.*?)(\/.*)/.exec(url.pathname)
if (regex) return `https://${regex[1]}.stackexchange.com${regex[2]}`
}
return
}
case "tekstowo":
return `${config.services[service].url}/${url.search.slice(1)}`
case "goodreads":
return `https://goodreads.com${url.pathname}${url.search}`
default:
return
}
}
return
}
const defaultInstances = {
materialious: ["https://app.materialio.us"],
viewtube: ["https://viewtube.io"],
piped: ["https://pipedapi-libre.kavin.rocks"],
pipedMaterial: ["https://piped-material.xn--17b.net"],
cloudtube: ["https://tube.cadence.moe"],
lightTube: ["https://tube.kuylar.dev"],
poketube: ["https://poketube.fun"],
proxiTok: ["https://proxitok.pabloferreiro.es"],
redlib: ["https://safereddit.com"],
eddrit: ["https://eddrit.com"],
scribe: ["https://scribe.rip"],
libMedium: ["https://md.vern.cc"],
quetre: ["https://quetre.iket.me"],
libremdb: ["https://libremdb.iket.me"],
simplyTranslate: ["https://simplytranslate.org"],
translite: ["https://tl.bloat.cat"],
mozhi: ["https://mozhi.aryak.me"],
searxng: ["https://search.bus-hit.me"],
"4get": ["https://4get.ca"],
rimgo: ["https://rimgo.vern.cc"],
hyperpipe: ["https://hyperpipe.surge.sh"],
osm: ["https://www.openstreetmap.org"],
breezeWiki: ["https://breezewiki.com"],
neuters: ["https://neuters.de"],
dumb: ["https://dm.vern.cc"],
intellectual: ["https://intellectual.insprill.net"],
ruralDictionary: ["https://rd.vern.cc"],
anonymousOverflow: ["https://code.whatever.social"],
suds: ["https://sd.vern.cc"],
unfunny: ["https://uf.vern.cc"],
soprano: ["https://sp.vern.cc"],
meme: ["https://mm.vern.cc"],
waybackClassic: ["https://wayback-classic.net"],
tent: ["https://tent.sny.sh"],
wolfreeAlpha: ["https://gqq.gitlab.io", "https://uqq.gitlab.io"],
laboratory: ["https://lab.vern.cc"],
binternet: ["https://bn.bloat.cat"],
pixivFe: ["https://pixivfe.exozy.me"],
liteXiv: ["https://litexiv.exozy.me"],
indestructables: ["https://indestructables.private.coffee"],
destructables: ["https://ds.vern.cc"],
safetwitch: ["https://safetwitch.drgns.space"],
twineo: ["https://twineo.exozy.me"],
proxigram: ["https://ig.opnxng.com"],
tuboYoutube: ["https://tubo.migalmoreno.com"],
tuboSoundcloud: ["https://tubo.migalmoreno.com"],
tekstoLibre: ["https://davilarek.github.io/TekstoLibre"],
skyview: ["https://skyview.social"],
priviblur: ["https://pb.bloat.cat"],
nitter: ["https://nitter.privacydev.net"],
pasted: ["https://pasted.drakeerv.com"],
freetar: ["https://freetar.de"],
ratAintTieba: ["https://rat.fis.land"],
shoelace: ["https://shoelace.mint.lgbt"],
skunkyArt: ["https://skunky.bloat.cat"],
ytify: ["https://ytify.netlify.app"],
nerdsForNerds: ["https://nn.vern.cc"],
koub: ["https://koub.clovius.club"],
}
async function getDefaults() {
let config = await utils.getConfig()
let options = {}
for (const service in config.services) {
options[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] = []
}
}
}
options.exceptions = {
url: [],
regex: [],
}
options.theme = "detect"
options.popupServices = ["youtube", "tiktok", "imgur", "reddit", "quora", "translate", "maps"]
options.fetchInstances = "github"
options.redirectOnlyInIncognito = false
options = { ...options, ...defaultInstances }
return options
}
function initDefaults() {
return new Promise(resolve => {
browser.storage.local.clear(async () => {
options = await getDefaults()
browser.storage.local.set({ options }, () => resolve())
})
})
}
function processUpdate(_options) {
return new Promise(async resolve => {
const config = await utils.getConfig()
let options = _options ?? (await utils.getOptions())
const defaults = await getDefaults()
// Remove any unknown option or subOption
for (const optionName in options) {
if (!(optionName in defaults)) delete options[optionName]
else if (typeof optionName === "object" && optionName !== null) {
for (const subOptionName in options[optionName]) {
if (!(subOptionName in defaults[optionName])) delete options[optionName][subOptionName]
}
}
}
// Remove any unknwon popupService
options.popupServices = options.popupServices.filter(service => service in config.services)
// Add missing options
for (const [defaultName, defaultValue] of Object.entries(defaults)) {
if (!(defaultName in options)) {
options[defaultName] = defaultValue
}
}
for (const [serviceName, serviceValue] of Object.entries(config.services)) {
// Reset service options if selected frontend is deprecated
if (!(options[serviceName].frontend in serviceValue.frontends)) {
options[serviceName] = serviceValue.options
}
// Add a default service option if it's not present
for (const optionName in serviceValue.options) {
if (!(optionName in options[serviceName])) {
options[serviceName][optionName] = serviceValue.options[optionName]
}
}
}
browser.storage.local.clear(() => {
browser.storage.local.set({ options }, () => {
resolve(options)
})
})
})
}
/**
* @param {URL} url
*/
async function copyRaw(url) {
const newUrl = await reverse(url)
if (newUrl) {
if (!isChrome) {
navigator.clipboard.writeText(newUrl)
} else {
var copyFrom = document.createElement("textarea")
copyFrom.textContent = newUrl
document.body.appendChild(copyFrom)
copyFrom.select()
document.execCommand("copy")
copyFrom.blur()
document.body.removeChild(copyFrom)
}
}
}
/**
* @param {URL} url
*/
function isException(url) {
if (!options) return false
if (!options.exceptions) return false
let exceptions = options.exceptions
if (exceptions && url) {
if (exceptions.url) {
for (let item of exceptions.url) {
item = new URL(item)
item = item.href.replace(/^http:\/\//, "https://")
if (item == url.href) {
return true
}
}
}
if (exceptions.regex) {
for (const item of exceptions.regex) {
if (new RegExp(item).test(url.href)) {
return true
}
}
}
}
return false
}
export default {
redirect,
redirectAsync,
computeServiceFrontend,
reverse,
initDefaults,
processUpdate,
copyRaw,
switchInstance,
isException,
}
|