about summary refs log tree commit diff stats
path: root/src/assets/javascripts/utils.js
blob: f360a15b6dfb950bd903ee03cafb1ef4ad400a49 (plain) (blame)
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
window.browser = window.browser || window.chrome

/**
 * @param {Array.<T>} instances
 * @returns {T}
 */
function getRandomInstance(instances) {
  return instances[~~(instances.length * Math.random())]
}

/**
 * @param {string} currentInstanceUrl
 * @param {Array.<T>} instances
 * @returns {T}
 */
function getNextInstance(currentInstanceUrl, instances) {
  const currentInstanceIndex = instances.indexOf(currentInstanceUrl)
  if (currentInstanceIndex === -1) return getRandomInstance(instances)
  const nextInstanceIndex = (currentInstanceIndex + 1) % instances.length
  return instances[nextInstanceIndex]
}

/**
 * @param {URL} url
 */
function protocolHost(url) {
  url.pathname = url.pathname.replace(/\/$/, "")
  if (url.username && url.password) return `${url.protocol}//${url.username}:${url.password}@${url.host}${url.pathname}`

  // workaround
  if (url.pathname == "/TekstoLibre/" && url.host.endsWith("github.io"))
    return `${url.protocol}//${url.host}${url.pathname.slice(0, -1)}`

  const pathname = url.pathname != "/" ? url.pathname : ""
  return `${url.protocol}//${url.host}${pathname}`
}

/**
 * @typedef FrontendInfo
 * @prop {boolean} instanceList
 * @prop {string} name
 * @prop {string} url
 */

/**
 * @typedef {Object} Service
 * @prop {Object.<string, FrontendInfo>} frontends
 * @prop {Object} options
 */

/**
 * @typedef {Object} Config
 * @prop {Object.<string, Service>} services
 */

/**
 * @returns {Promise<Config>}
 */
function getConfig() {
  return new Promise(resolve => {
    fetch("/config.json")
      .then(response => response.text())
      .then(json => {
        resolve(JSON.parse(json))
        return
      })
  })
}

/**
 * @typedef {Object} Option
 * @prop {string} frontend
 */

/**
 * @returns {Promise<Object.<string, Option | string[]>>}
 */
function getOptions() {
  return new Promise(resolve => browser.storage.local.get("options", r => resolve(r.options)))
}

function getPingCache() {
  return new Promise(resolve => browser.storage.local.get("pingCache", r => resolve(r.pingCache ?? {})))
}

function getBlacklist(options) {
  return new Promise(resolve => {
    let url
    if (options.fetchInstances == "github")
      url = "https://raw.githubusercontent.com/libredirect/instances/main/blacklist.json"
    else if (options.fetchInstances == "codeberg")
      url = "https://codeberg.org/LibRedirect/instances/raw/branch/main/blacklist.json"
    else return resolve("disabled")
    const http = new XMLHttpRequest()
    http.open("GET", url, true)
    http.onreadystatechange = () => {
      if (http.status === 200 && http.readyState == XMLHttpRequest.DONE) resolve(JSON.parse(http.responseText))
    }
    http.onerror = () => resolve()
    http.ontimeout = () => resolve()
    http.send(null)
  })
}

function getList(options) {
  return new Promise(resolve => {
    let url
    if (options.fetchInstances == "github")
      url = "https://raw.githubusercontent.com/libredirect/instances/main/data.json"
    else if (options.fetchInstances == "codeberg")
      url = "https://codeberg.org/LibRedirect/instances/raw/branch/main/data.json"
    else return resolve("disabled")
    const http = new XMLHttpRequest()
    http.open("GET", url, true)
    http.onreadystatechange = () => {
      if (http.status === 200 && http.readyState == XMLHttpRequest.DONE) return resolve(JSON.parse(http.responseText))
    }
    http.onerror = () => resolve()
    http.ontimeout = () => resolve()
    http.send(null)
  })
}

/**
 * @param {string} href
 */
function pingOnce(href) {
  return new Promise(async resolve => {
    let started
    let http = new XMLHttpRequest()
    http.timeout = 5000
    http.ontimeout = () => resolve(5000)
    http.onerror = () => resolve()
    http.onreadystatechange = () => {
      if (http.readyState == 2) {
        if (http.status == 200) {
          let ended = new Date().getTime()
          http.abort()
          resolve(ended - started)
        } else {
          resolve(5000 + http.status)
        }
      }
    }
    http.open("GET", `${href}?_=${new Date().getTime()}`, true)
    started = new Date().getTime()
    http.send(null)
  })
}

/**
 * @param {string} href
 */
function ping(href) {
  return new Promise(async resolve => {
    let average = 0
    let time
    for (let i = 0; i < 3; i++) {
      time = await pingOnce(href)
      if (i == 0) continue
      if (time >= 5000) {
        resolve(time)
        return
      }
      average += time
    }
    average = parseInt(average / 3)
    resolve(average)
  })
}

function addressToLatLng(address) {
  const http = new XMLHttpRequest()
  http.open(
    "GET",
    `https://nominatim.openstreetmap.org/search?q=${encodeURIComponent(address)}&format=json&limit=1`,
    false
  )
  http.send()
  if (http.status == 200) {
    const json = JSON.parse(http.responseText)[0]
    if (json) {
      return {
        coordinate: `${json.lat},${json.lon}`,
        boundingbox: `${json.boundingbox[2]},${json.boundingbox[1]},${json.boundingbox[3]},${json.boundingbox[0]}`,
      }
    }
    return {}
  }
}

function getQuery(url) {
  let query = ""
  if (url.searchParams.has("q")) query = url.searchParams.get("q")
  else if (url.searchParams.has("query")) query = url.searchParams.has("query")
  return query
}
function prefsEncoded(prefs) {
  return new URLSearchParams(prefs).toString()
}

function convertMapCentre(url) {
  let [lat, lon, zoom] = [null, null, null]
  const reg = url.pathname.match(/@(-?\d[0-9.]*),(-?\d[0-9.]*),(\d{1,2})[.z]/)
  if (reg) {
    ;[, lon, lat, zoom] = reg
  } else if (url.searchParams.has("center")) {
    // Set map centre if present
    ;[lat, lon] = url.searchParams.get("center").split(",")
    zoom = url.searchParams.get("zoom") ?? "17"
  }
  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
}

async function autoPickInstance(clearnet, url) {
  if (url) {
    const i = clearnet.findIndex(instance => url.href.startsWith(instance))
    if (i >= 0) clearnet.splice(i, 1)
  }
  const random = randomInstances(clearnet, 5)
  const pings = await Promise.all([
    ...random.map(async instance => {
      return [instance, await ping(instance)]
    }),
  ])
  pings.sort((a, b) => a[1] - b[1])
  return pings[0][0]
}

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,
  protocolHost,
  getList,
  getBlacklist,
  getConfig,
  getOptions,
  getPingCache,
  ping,
  addressToLatLng,
  getQuery,
  prefsEncoded,
  convertMapCentre,
  randomInstances,
  style,
  autoPickInstance,
}