Skip to content

Commit 4988017

Browse files
committed
Merge branch '1fexd-refactor-redirect'
2 parents 7874877 + ab7a702 commit 4988017

File tree

1 file changed

+62
-56
lines changed

1 file changed

+62
-56
lines changed

src/assets/javascripts/services.js

Lines changed: 62 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -62,68 +62,21 @@ async function redirectAsync(url, type, initiator, forceRedirection) {
6262
}
6363

6464
/**
65-
* @param {URL} url
66-
* @param {string} type
67-
* @param {URL} initiator
68-
* @param {boolean} forceRedirection
69-
* @returns {string | undefined}
65+
* @param url
66+
* @param frontend
67+
* @param randomInstance
68+
* @returns {undefined|string}
7069
*/
71-
function redirect(url, type, initiator, forceRedirection, incognito) {
72-
if (type != "main_frame" && type != "sub_frame" && type != "image") return
73-
let randomInstance
74-
let frontend
75-
if (!forceRedirection && options.redirectOnlyInIncognito == true && !incognito) return
76-
for (const service in config.services) {
77-
if (!forceRedirection && !options[service].enabled) continue
78-
79-
frontend = options[service].frontend
80-
81-
82-
if (config.services[service].frontends[frontend].desktopApp && type != "main_frame" && options[service].redirectType != "main_frame")
83-
frontend = options[service].embedFrontend
84-
85-
86-
if (!regexArray(service, url, config, frontend)) {
87-
frontend = null
88-
continue
89-
}
90-
91-
if (
92-
config.services[service].embeddable &&
93-
type != options[service].redirectType && options[service].redirectType != "both"
94-
) {
95-
if (options[service].unsupportedUrls == 'block') return 'CANCEL'
96-
return
97-
}
98-
99-
let instanceList = options[frontend]
100-
if (instanceList === undefined) break
101-
if (instanceList.length === 0) return null
102-
103-
if (
104-
initiator
105-
&&
106-
instanceList.includes(initiator.origin)
107-
) {
108-
if (type != "main_frame") return null
109-
else return "BYPASSTAB"
110-
}
111-
112-
randomInstance = utils.getRandomInstance(instanceList)
113-
if (config.services[service].frontends[frontend].localhost && options[service].instance == "localhost") {
114-
randomInstance = `http://${frontend}.localhost:8080`
115-
}
116-
break
117-
}
118-
if (!frontend) return
119-
70+
function rewrite(url, frontend, randomInstance) {
71+
if (!frontend || !randomInstance) return
12072
switch (frontend) {
12173
case "hyperpipe": {
12274
return `${randomInstance}${url.pathname}${url.search}`.replace(/\/search\?q=.*/, searchQuery => searchQuery.replace("?q=", "/"))
12375
}
12476
case "searx":
125-
case "searxng":
77+
case "searxng": {
12678
return `${randomInstance}/${url.search}`
79+
}
12780
case "whoogle": {
12881
return `${randomInstance}/search${url.search}`
12982
}
@@ -149,7 +102,6 @@ function redirect(url, type, initiator, forceRedirection, incognito) {
149102
case "freetubePwa": {
150103
return 'freetube://' + url.href
151104
}
152-
153105
case "poketube": {
154106
if (url.pathname.startsWith('/channel')) {
155107
const reg = /\/channel\/(.*)\/?$/.exec(url.pathname)
@@ -557,6 +509,60 @@ function redirect(url, type, initiator, forceRedirection, incognito) {
557509
}
558510
}
559511

512+
/**
513+
* @param {URL} url
514+
* @param {string} type
515+
* @param {URL} initiator
516+
* @param {boolean} forceRedirection
517+
* @returns {string | undefined}
518+
*/
519+
function redirect(url, type, initiator, forceRedirection, incognito) {
520+
if (type != "main_frame" && type != "sub_frame" && type != "image") return
521+
let randomInstance
522+
let frontend
523+
if (!forceRedirection && options.redirectOnlyInIncognito == true && !incognito) return
524+
for (const service in config.services) {
525+
if (!forceRedirection && !options[service].enabled) continue
526+
527+
frontend = options[service].frontend
528+
529+
if (config.services[service].frontends[frontend].desktopApp && type != "main_frame" && options[service].redirectType != "main_frame")
530+
frontend = options[service].embedFrontend
531+
532+
if (!regexArray(service, url, config, frontend)) {
533+
frontend = null
534+
continue
535+
}
536+
537+
if (
538+
config.services[service].embeddable
539+
&&
540+
type != options[service].redirectType && options[service].redirectType != "both"
541+
) {
542+
if (options[service].unsupportedUrls == 'block') return 'CANCEL'
543+
return
544+
}
545+
546+
let instanceList = options[frontend]
547+
if (instanceList === undefined) break
548+
if (instanceList.length === 0) return null
549+
550+
if (initiator && instanceList.includes(initiator.origin)) {
551+
if (type != "main_frame") return null
552+
else return "BYPASSTAB"
553+
}
554+
555+
randomInstance = utils.getRandomInstance(instanceList)
556+
if (config.services[service].frontends[frontend].localhost && options[service].instance == "localhost") {
557+
randomInstance = `http://${frontend}.localhost:8080`
558+
}
559+
break
560+
}
561+
if (!frontend) return
562+
563+
return rewrite(url, frontend, randomInstance)
564+
}
565+
560566
/**
561567
* @param {URL} url
562568
* @param {*} returnFrontend

0 commit comments

Comments
 (0)