Skip to content

Commit

Permalink
Restricted maps regex
Browse files Browse the repository at this point in the history
  • Loading branch information
ManeraKai committed Sep 21, 2024
1 parent 67c564f commit d4151c8
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 129 deletions.
40 changes: 13 additions & 27 deletions src/assets/javascripts/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -647,32 +647,19 @@ async function redirectAsync(url, type, originUrl, documentUrl, incognito, force
/**
* @param {URL} url
*/
function computeService(url) {
return new Promise(async resolve => {
const config = await utils.getConfig()
const options = await utils.getOptions()
for (const service in config.services) {
if (regexArray(service, url, config, options)) {
resolve(service)
return
} else {
for (const frontend in config.services[service].frontends) {
if (all(service, frontend, options, config).findIndex(instance => url.href.startsWith(instance)) >= 0) {
return resolve(service)
}
}
}
}
resolve()
})
}
export function computeFrontend(url) {
async function computeServiceFrontend(url) {
const config = await utils.getConfig()
const options = await utils.getOptions()
for (const service in config.services) {
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 }
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 }
}
}
}
}
Expand Down Expand Up @@ -970,12 +957,11 @@ function isException(url) {
export default {
redirect,
redirectAsync,
computeService,
computeServiceFrontend,
reverse,
initDefaults,
processUpdate,
copyRaw,
switchInstance,
isException,
computeFrontend,
}
17 changes: 17 additions & 0 deletions src/assets/javascripts/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,22 @@ export function randomInstances(clearnet, n) {
}
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};
Expand Down Expand Up @@ -277,4 +293,5 @@ export default {
convertMapCentre,
randomInstances,
style,
autoPickInstance,
}
3 changes: 2 additions & 1 deletion src/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -1147,7 +1147,8 @@
},
"targets": [
"^https?:\\/{2}maps\\.libredirect\\.invalid",
"^https?:\\/{2}(((www|maps)\\.)?(google\\.).*(\\/maps)|maps\\.(google\\.).*)"
"^https?:\\/{2}(www\\.)?maps\\.google(\\.[a-z]{2,3}){1,2}\\/",
"^https?:\\/{2}(www\\.)?google(\\.[a-z]{2,3}){1,2}\\/maps\\/?"
],
"name": "Maps",
"options": {
Expand Down
43 changes: 19 additions & 24 deletions src/pages/messages_src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import { options, config, page } from "./stores"
import Button from "../components/Button.svelte"
import AutoPickIcon from "../icons/AutoPickIcon.svelte"
import SwitchInstanceIcon from "../icons/SwitchInstanceIcon.svelte"
let _options
const unsubscribeOptions = options.subscribe(val => {
Expand Down Expand Up @@ -49,33 +48,16 @@
const oldUrl = new URL(params.get("url"))
async function autoPick() {
const frontend = params.get("frontend")
autoPicking = true
const frontend = params.get("frontend")
const redirects = await utils.getList(_options)
const instances = utils.randomInstances(redirects[frontend]["clearnet"], 5)
const pings = await Promise.all([
...instances.map(async instance => {
return [instance, await utils.ping(instance)]
}),
])
pings.sort((a, b) => a[1] - b[1])
_options[frontend].push(pings[0][0])
const clearnet = redirects[frontend]["clearnet"]
const instance = await utils.autoPickInstance(clearnet)
_options[frontend].push(instance)
options.set(_options)
autoPicking = false
}
async function autoPickInstance() {
await autoPick()
await redirectUrl()
}
async function enableService() {
const service = await servicesHelper.computeService(oldUrl)
_options[service].enabled = true
options.set(_options)
await redirectUrl()
}
async function redirectUrl() {
const newUrl = await servicesHelper.redirectAsync(oldUrl, "main_frame", null, null, false, true)
browser.tabs.update({ url: newUrl })
Expand All @@ -87,14 +69,27 @@
{#if params.get("message") == "disabled"}
<div>
<h1>You disabled redirections for this service</h1>
<Button on:click={enableService}>
<Button
on:click={async () => {
const { service } = await servicesHelper.computeServiceFrontend(oldUrl)
_options[service].enabled = true
options.set(_options)
await redirectUrl()
}}
>
{browser.i18n.getMessage("enable") || "Enable"}
</Button>
</div>
{:else if params.get("message") == "no_instance"}
<div>
<h1>You have no instance selected for this frontend</h1>
<Button on:click={autoPickInstance} disabled={autoPicking}>
<Button
on:click={async () => {
await autoPick()
await redirectUrl()
}}
disabled={autoPicking}
>
<AutoPickIcon class="margin margin_{document.body.dir}" />
{browser.i18n.getMessage("autoPickInstance") || "Auto Pick Instance"}
</Button>
Expand Down
57 changes: 31 additions & 26 deletions src/pages/options_src/Services/Instances.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -56,30 +56,6 @@
return true
}
async function pingInstances() {
pingCache = {}
for (const instance of allInstances) {
pingCache[instance] = { color: "lightblue", value: "pinging..." }
const time = await utils.ping(instance)
pingCache[instance] = colorTime(time)
}
}
async function autoPickInstance() {
const instances = utils.randomInstances(redirects[selectedFrontend]["clearnet"], 5)
const myInstancesCache = []
for (const instance of instances) {
pingCache[instance] = { color: "lightblue", value: "pinging..." }
const time = await utils.ping(instance)
pingCache[instance] = colorTime(time)
myInstancesCache.push([instance, time])
}
myInstancesCache.sort((a, b) => a[1] - b[1])
_options[selectedFrontend].push(myInstancesCache[0][0])
options.set(_options)
}
function colorTime(time) {
let value
let color
Expand Down Expand Up @@ -113,17 +89,46 @@
options.set(_options)
}
}
let autoPicking = false
let pinging = false
</script>

{#if serviceConf.frontends[selectedFrontend].instanceList && redirects && blacklist}
<hr />

<div>
<Button on:click={pingInstances}>
<Button
on:click={async () => {
pinging = true
pingCache = {}
for (const instance of allInstances) {
pingCache[instance] = { color: "lightblue", value: "pinging..." }
const time = await utils.ping(instance)
pingCache[instance] = colorTime(time)
}
pinging = false
}}
disabled={pinging}
>
<PingIcon class="margin margin_{document.body.dir}" />
{browser.i18n.getMessage("pingInstances") || "Ping Instances"}
</Button>
<Button on:click={autoPickInstance}>
<Button
on:click={async () => {
autoPicking = true
const clearnet = redirects[selectedFrontend]["clearnet"]
for (const instance of _options[selectedFrontend]) {
const i = clearnet.indexOf(instance)
if (i >= 0) clearnet.splice(i, 1)
}
const instance = await utils.autoPickInstance(clearnet)
_options[selectedFrontend].push(instance)
options.set(_options)
autoPicking = false
}}
disabled={autoPicking}
>
<AutoPickIcon class="margin margin_{document.body.dir}" />
{browser.i18n.getMessage("autoPickInstance") || "Auto Pick Instance"}
</Button>
Expand Down
Loading

0 comments on commit d4151c8

Please sign in to comment.