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
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 => {

View File

@ -103,6 +103,7 @@ async function processDefaultCustomInstances(frontend, networks, document) {
})
)
!async function () {
const blacklist = utils.getBlacklist()
const redirects = utils.getList()
@ -136,6 +137,7 @@ async function processDefaultCustomInstances(frontend, networks, document) {
].join("\n<hr>\n")
}
}
}()
localise.localisePage()