This commit is contained in:
ManeraKai 2023-01-07 12:33:38 +03:00
parent 6d648dde61
commit 91528e2192
No known key found for this signature in database
GPG Key ID: 5ABC31FFD562E337
4 changed files with 66 additions and 45 deletions

View File

@ -2,13 +2,14 @@ window.browser = window.browser || window.chrome
import utils from "./utils.js" import utils from "./utils.js"
let config, options, redirects, targets let config, options, targets
function init() { function init() {
return new Promise(async resolve => { return new Promise(async resolve => {
browser.storage.local.get(["options", "redirects", "targets"], r => { browser.storage.local.get(["options", "targets", "embedTabs"], r => {
options = r.options options = r.options
targets = r.targets targets = r.targets
embedTabs = r.embedTabs
fetch("/config.json") fetch("/config.json")
.then(response => response.text()) .then(response => response.text())
.then(configData => { .then(configData => {
@ -55,7 +56,8 @@ function regexArray(service, url, config, frontend) {
return false return false
} }
function redirect(url, type, initiator, forceRedirection) { let embedTabs = {}
function redirect(url, type, initiator, forceRedirection, tabId) {
if (type != "main_frame" && type != "sub_frame" && type != "image") return if (type != "main_frame" && type != "sub_frame" && type != "image") return
let randomInstance let randomInstance
let frontend let frontend
@ -76,7 +78,19 @@ function redirect(url, type, initiator, forceRedirection) {
instanceList.push(...options[frontend]) instanceList.push(...options[frontend])
} }
if (instanceList.length === 0) return if (instanceList.length === 0) return
randomInstance = utils.getRandomInstance(instanceList)
if ((type == "sub_frame" || type == "image") && embedTabs[tabId] && embedTabs[tabId][frontend] !== undefined) {
randomInstance = embedTabs[tabId][frontend]
} else {
randomInstance = utils.getRandomInstance(instanceList)
}
if ((type == "sub_frame" || type == "image") && embedTabs[tabId] === undefined) {
embedTabs[tabId] = {}
embedTabs[tabId][frontend] = randomInstance
browser.storage.local.set(embedTabs)
}
break break
} }
if (!frontend || !randomInstance) return if (!frontend || !randomInstance) return
@ -533,7 +547,7 @@ function initDefaults() {
} }
} }
browser.storage.local.set( browser.storage.local.set(
{ options, targets, localstorage }, { options, targets, localstorage, embedTabs: {} },
() => resolve() () => resolve()
) )
}) })

View File

@ -479,13 +479,5 @@
"embeddable": false, "embeddable": false,
"url": "https://paste.libredirect.invalid" "url": "https://paste.libredirect.invalid"
} }
},
"blacklist": {
"cloudflare": {
"color": "red"
},
"authenticate": {
"color": "orange"
}
} }
} }

View File

@ -38,6 +38,14 @@ browser.runtime.onInstalled.addListener(async details => {
}) })
let tabIdRedirects = {} let tabIdRedirects = {}
browser.storage.onChanged.addListener(() => {
browser.storage.local.get(["embedTabs"], r => {
embedTabs = r.embedTabs
})
})
let embedTabs = {}
// true == Always redirect, false == Never redirect, null/undefined == follow options for services // true == Always redirect, false == Never redirect, null/undefined == follow options for services
browser.webRequest.onBeforeRequest.addListener( browser.webRequest.onBeforeRequest.addListener(
details => { details => {
@ -51,7 +59,7 @@ browser.webRequest.onBeforeRequest.addListener(
return null return null
} }
if (tabIdRedirects[details.tabId] == false) return null if (tabIdRedirects[details.tabId] == false) return null
let newUrl = servicesHelper.redirect(url, details.type, initiator, tabIdRedirects[details.tabId]) let newUrl = servicesHelper.redirect(url, details.type, initiator, tabIdRedirects[details.tabId], details.tabId)
if (details.frameAncestors && details.frameAncestors.length > 0 && generalHelper.isException(new URL(details.frameAncestors[0].url))) newUrl = null if (details.frameAncestors && details.frameAncestors.length > 0 && generalHelper.isException(new URL(details.frameAncestors[0].url))) newUrl = null
@ -81,6 +89,11 @@ browser.tabs.onRemoved.addListener(tabId => {
delete tabIdRedirects[tabId] delete tabIdRedirects[tabId]
console.log("Removed tab " + tabId + " from tabIdRedirects") console.log("Removed tab " + tabId + " from tabIdRedirects")
} }
if (embedTabs[tab] != undefined) {
delete embedTabs[tabId]
browser.storage.local.set(embedTabs)
console.log("Removed tab " + tabId + " from embedTabs")
}
}) })
browser.commands.onCommand.addListener(command => { browser.commands.onCommand.addListener(command => {
@ -185,4 +198,4 @@ browser.webRequest.onHeadersReceived.addListener(
}, },
{ urls: ["<all_urls>"] }, { urls: ["<all_urls>"] },
["blocking", "responseHeaders"] ["blocking", "responseHeaders"]
) )

View File

@ -103,39 +103,41 @@ async function processDefaultCustomInstances(frontend, networks, document) {
}) })
) )
const blacklist = utils.getBlacklist() !async function () {
const redirects = utils.getList() const blacklist = utils.getBlacklist()
const redirects = utils.getList()
for (const network in networks) { for (const network in networks) {
if (redirects[frontend][network].length > 0) { if (redirects[frontend][network].length > 0) {
document.getElementById(frontend).getElementsByClassName(network)[0].getElementsByClassName("checklist")[0].innerHTML = [ document.getElementById(frontend).getElementsByClassName(network)[0].getElementsByClassName("checklist")[0].innerHTML = [
` `
<div class="some-block option-block"> <div class="some-block option-block">
<h4>${utils.camelCase(network)}</h4> <h4>${utils.camelCase(network)}</h4>
</div> </div>
`, `,
...redirects[frontend][network] ...redirects[frontend][network]
.sort((a, b) => .sort((a, b) =>
(blacklist.cloudflare.includes(a) && !blacklist.cloudflare.includes(b)) (blacklist.cloudflare.includes(a) && !blacklist.cloudflare.includes(b))
|| ||
(blacklist.authenticate.includes(a) && !blacklist.authenticate.includes(b)) (blacklist.authenticate.includes(a) && !blacklist.authenticate.includes(b))
) )
.map(x => { .map(x => {
const cloudflare = blacklist.cloudflare.includes(x) ? ' <span style="color:red;">cloudflare</span>' : "" const cloudflare = blacklist.cloudflare.includes(x) ? ' <span style="color:red;">cloudflare</span>' : ""
const authenticate = blacklist.authenticate.includes(x) ? ' <span style="color:orange;">authenticate</span>' : "" const authenticate = blacklist.authenticate.includes(x) ? ' <span style="color:orange;">authenticate</span>' : ""
let warnings = [cloudflare, authenticate].join(" ") let warnings = [cloudflare, authenticate].join(" ")
return ` return `
<div> <div>
<x> <x>
<a href="${x}" target="_blank">${x}</a>${warnings} <a href="${x}" target="_blank">${x}</a>${warnings}
</x> </x>
</div>` </div>`
}), }),
'<br>' '<br>'
].join("\n<hr>\n") ].join("\n<hr>\n")
}
} }
} }()
localise.localisePage() localise.localisePage()