Added context menu to toggle redirects for a certain tab

Closes https://github.com/libredirect/libredirect/issues/464
This commit is contained in:
Hygna 2022-10-08 11:37:45 +01:00
parent 6d9ddaae2f
commit be78f86a67
No known key found for this signature in database
3 changed files with 65 additions and 34 deletions

View File

@ -210,5 +210,9 @@
},
"lbryDesktop": {
"message": "LBRY Desktop"
},
"bypassTab": {
"message": "Toggle redirects in this tab",
"description": "Used in context menus when right clicking on a page/tab"
}
}

View File

@ -68,13 +68,14 @@ function redirect(url, type, initiator) {
if (!options[service].enabled) continue
if (config.services[service].embeddable && type != options[service].redirectType && options[service].redirectType != "both") continue
if (!config.services[service].embeddable && type != "main_frame") continue
let targets = new RegExp(config.services[service].targets.join("|"), "i")
// let targets = new RegExp(config.services[service].targets.join("|"), "i")
if (!regexArray(service, url, config)) continue
if (initiator) {
if (targets.test(initiator.host)) continue
if (all(service, null, options, config, redirects).includes(initiator.origin) && reverse(initiator) == url) return "BYPASSTAB"
}
// if (initiator) {
// console.log(initiator.host)
// if (targets.test(initiator.host)) continue
// //if (all(service, null, options, config, redirects).includes(initiator.origin) && reverse(initiator) == url) return "BYPASSTAB"
// }
if (Object.keys(config.services[service].frontends).length > 1) {
if (type == "sub_frame" && config.services[service].embeddable && !config.services[service].frontends[options[service].frontend].embeddable) frontend = options[service].embedFrontend
@ -473,26 +474,29 @@ function switchInstance(url) {
})
}
function reverse(url) {
function reverse(url, urlString) {
return new Promise(async resolve => {
await init()
let protocolHost = utils.protocolHost(url)
let protocolHost
if (!urlString) protocolHost = utils.protocolHost(url)
else protocolHost = url.match(/https?:\/{2}(?:[^\s\/]+\.)+[a-zA-Z0-9]+/)[0]
for (const service in config.services) {
if (!all(service, null, options, config, redirects).includes(protocolHost)) continue
switch (service) {
case "instagram":
if (url.pathname.startsWith("/p")) resolve(`https://instagram.com${url.pathname.replace("/p", "")}${url.search}`)
if (url.pathname.startsWith("/u")) resolve(`https://instagram.com${url.pathname.replace("/u", "")}${url.search}`)
resolve(config.services[service].url + url.pathname + url.search)
return
case "youtube":
case "imdb":
case "imgur":
case "tiktok":
case "twitter":
case "reddit":
resolve(config.services[service].url + url.pathname + url.search)
case "imdb":
case "reuters":
case "quora":
case "medium":
if (!urlString) resolve(config.services[service].url + url.pathname + url.search)
else resolve(url.replace(/https?:\/{2}(?:[^\s\/]+\.)+[a-zA-Z0-9]+/, config.services[service].url))
return
default:
resolve()

View File

@ -69,11 +69,11 @@ browser.webRequest.onBeforeRequest.addListener(
console.log(`Canceled ${url}`)
return { cancel: true }
}
if (newUrl === "BYPASSTAB") {
console.log(`Bypassed ${details.tabId} ${url}`)
if (!BYPASSTABs.includes(details.tabId)) BYPASSTABs.push(details.tabId)
return null
}
// if (newUrl === "BYPASSTAB") {
// console.log(`Bypassed ${details.tabId} ${url}`)
// if (!BYPASSTABs.includes(details.tabId)) BYPASSTABs.push(details.tabId)
// return null
// }
console.info("Redirecting", url.href, "=>", newUrl)
return { redirectUrl: newUrl }
}
@ -91,18 +91,6 @@ browser.tabs.onRemoved.addListener(tabId => {
}
})
/*
browser.webRequest.onHeadersReceived.addListener(
e => {
let response = youtubeHelper.removeXFrameOptions(e)
if (!response) response = twitterHelper.removeXFrameOptions(e)
return response
},
{ urls: ["<all_urls>"] },
["blocking", "responseHeaders"]
)
*/
async function redirectOfflineInstance(url, tabId) {
let newUrl = await servicesHelper.switchInstance(url, true)
@ -170,11 +158,46 @@ browser.contextMenus.create({
contexts: ["browser_action"],
})
browser.contextMenus.onClicked.addListener(info => {
if (info.menuItemId == "switchInstance") utils.switchInstance()
else if (info.menuItemId == "settings") browser.runtime.openOptionsPage()
else if (info.menuItemId == "copyRaw") utils.copyRaw()
else if (info.menuItemId == "unify") utils.unify()
browser.contextMenus.create({
id: "bypassTab",
title: browser.i18n.getMessage("bypassTab"),
contexts: ["page", "tab"],
})
browser.contextMenus.onClicked.addListener((info, tab) => {
return new Promise(async resolve => {
switch (info.menuItemId) {
case "switchInstance":
utils.switchInstance()
resolve()
return
case "settings":
browser.runtime.openOptionsPage()
resolve()
return
case "copyRaw":
utils.copyRaw()
resolve()
return
case "unify":
utils.unify()
resolve()
return
case "bypassTab":
if (!BYPASSTABs.includes(tab.id)) {
BYPASSTABs.push(tab.id)
let newUrl = await servicesHelper.reverse(tab.url, true)
if (newUrl) browser.tabs.update(tab.id, { url: newUrl })
resolve()
return
} else {
BYPASSTABs.splice(BYPASSTABs.indexOf(tab.id), 1)
browser.tabs.reload(tab.id)
resolve()
return
}
}
})
})
browser.runtime.onMessage.addListener((message, sender, sendResponse) => {