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"
let config, options, redirects, targets
let config, options, targets
function init() {
return new Promise(async resolve => {
browser.storage.local.get(["options", "redirects", "targets"], r => {
browser.storage.local.get(["options", "targets", "embedTabs"], r => {
options = r.options
targets = r.targets
embedTabs = r.embedTabs
fetch("/config.json")
.then(response => response.text())
.then(configData => {
@ -55,7 +56,8 @@ function regexArray(service, url, config, frontend) {
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
let randomInstance
let frontend
@ -76,7 +78,19 @@ function redirect(url, type, initiator, forceRedirection) {
instanceList.push(...options[frontend])
}
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
}
if (!frontend || !randomInstance) return
@ -533,7 +547,7 @@ function initDefaults() {
}
}
browser.storage.local.set(
{ options, targets, localstorage },
{ options, targets, localstorage, embedTabs: {} },
() => resolve()
)
})

View File

@ -479,13 +479,5 @@
"embeddable": false,
"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 = {}
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
browser.webRequest.onBeforeRequest.addListener(
details => {
@ -51,7 +59,7 @@ browser.webRequest.onBeforeRequest.addListener(
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
@ -81,6 +89,11 @@ browser.tabs.onRemoved.addListener(tabId => {
delete tabIdRedirects[tabId]
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 => {
@ -185,4 +198,4 @@ browser.webRequest.onHeadersReceived.addListener(
},
{ urls: ["<all_urls>"] },
["blocking", "responseHeaders"]
)
)

View File

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