aboutsummaryrefslogtreecommitdiffstats
path: root/src/assets
diff options
context:
space:
mode:
Diffstat (limited to 'src/assets')
-rw-r--r--src/assets/javascripts/services.js27
-rw-r--r--src/assets/javascripts/utils.js53
2 files changed, 65 insertions, 15 deletions
diff --git a/src/assets/javascripts/services.js b/src/assets/javascripts/services.js
index ee48a1b9..d1c12375 100644
--- a/src/assets/javascripts/services.js
+++ b/src/assets/javascripts/services.js
@@ -610,18 +610,20 @@ function redirect(url, type, originUrl, documentUrl, incognito, forceRedirection
}
let instanceList = options[frontend]
- if (instanceList === undefined) break
- if (instanceList.length === 0) return "https://libredirect.invalid"
+ 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
}
-
- randomInstance = utils.getRandomInstance(instanceList)
- if (config.services[service].frontends[frontend].localhost && options[service].instance == "localhost") {
- randomInstance = `http://${frontend}.localhost:8080`
- }
break
}
if (!frontend) return
@@ -636,7 +638,7 @@ function redirect(url, type, originUrl, documentUrl, incognito, forceRedirection
* @param {URL} documentUrl
* @param {boolean} incognito
* @param {boolean} forceRedirection
- * @returns {string | undefined}
+ * @returns {Promise<string | undefined>}
*/
async function redirectAsync(url, type, originUrl, documentUrl, incognito, forceRedirection) {
await init()
@@ -645,9 +647,8 @@ async function redirectAsync(url, type, originUrl, documentUrl, incognito, force
/**
* @param {URL} url
- * @param {*} returnFrontend
*/
-function computeService(url, returnFrontend) {
+function computeService(url) {
return new Promise(async resolve => {
const config = await utils.getConfig()
const options = await utils.getOptions()
@@ -658,9 +659,7 @@ function computeService(url, returnFrontend) {
} else {
for (const frontend in config.services[service].frontends) {
if (all(service, frontend, options, config).includes(utils.protocolHost(url))) {
- if (returnFrontend) resolve([service, frontend, utils.protocolHost(url)])
- else resolve(service)
- return
+ return resolve(service)
}
}
}
@@ -768,7 +767,7 @@ async function reverse(url) {
}
const defaultInstances = {
- invidious: ["https://inv.vern.cc"],
+ // invidious: ["https://inv.vern.cc"],
materialious: ["https://app.materialio.us"],
viewtube: ["https://viewtube.io"],
piped: ["https://pipedapi-libre.kavin.rocks"],
diff --git a/src/assets/javascripts/utils.js b/src/assets/javascripts/utils.js
index 36271a8a..e5b8ba46 100644
--- a/src/assets/javascripts/utils.js
+++ b/src/assets/javascripts/utils.js
@@ -24,7 +24,7 @@ function getNextInstance(currentInstanceUrl, instances) {
* @param {URL} url
*/
function protocolHost(url) {
- url.pathname = url.pathname.replace(/\/$/, '');
+ url.pathname = url.pathname.replace(/\/$/, "")
if (url.username && url.password) return `${url.protocol}//${url.username}:${url.password}@${url.host}${url.pathname}`
// workaround
@@ -211,6 +211,55 @@ function convertMapCentre(url) {
return { zoom, lon, lat }
}
+export function randomInstances(clearnet, n) {
+ let instances = []
+ if (n > clearnet.length) n = clearnet.length
+ for (let i = 0; i < n; i++) {
+ const randomNumber = Math.floor(Math.random() * clearnet.length)
+ const randomInstance = clearnet[randomNumber]
+ instances.push(randomInstance)
+ }
+ return instances
+}
+export function style(options, window) {
+ const vars = cssVariables(options, window)
+ return `--text: ${vars.text};
+ --bg-main: ${vars.bgMain};
+ --bg-secondary: ${vars.bgSecondary};
+ --active: ${vars.active};
+ --danger: ${vars.danger};
+ --light-grey: ${vars.lightGrey};`
+}
+
+function cssVariables(options, window) {
+ const dark = {
+ text: "#fff",
+ bgMain: "#121212",
+ bgSecondary: "#202020",
+ active: "#fbc117",
+ danger: "#f04141",
+ lightGrey: "#c3c3c3",
+ }
+
+ const light = {
+ text: "black",
+ bgMain: "white",
+ bgSecondary: "#e4e4e4",
+ active: "#fb9817",
+ danger: "#f04141",
+ lightGrey: "#c3c3c3",
+ }
+ if (options.theme == "dark") {
+ return dark
+ } else if (options.theme == "light") {
+ return light
+ } else if (window.matchMedia("(prefers-color-scheme: dark)").matches) {
+ return dark
+ } else {
+ return light
+ }
+}
+
export default {
getRandomInstance,
getNextInstance,
@@ -225,4 +274,6 @@ export default {
getQuery,
prefsEncoded,
convertMapCentre,
+ randomInstances,
+ style,
}