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": { "lbryDesktop": {
"message": "LBRY Desktop" "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 (!options[service].enabled) continue
if (config.services[service].embeddable && type != options[service].redirectType && options[service].redirectType != "both") continue if (config.services[service].embeddable && type != options[service].redirectType && options[service].redirectType != "both") continue
if (!config.services[service].embeddable && type != "main_frame") 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 (!regexArray(service, url, config)) continue
if (initiator) { // if (initiator) {
if (targets.test(initiator.host)) continue // console.log(initiator.host)
if (all(service, null, options, config, redirects).includes(initiator.origin) && reverse(initiator) == url) return "BYPASSTAB" // 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 (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 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 => { return new Promise(async resolve => {
await init() 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) { for (const service in config.services) {
if (!all(service, null, options, config, redirects).includes(protocolHost)) continue if (!all(service, null, options, config, redirects).includes(protocolHost)) continue
switch (service) { switch (service) {
case "instagram": 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 "youtube":
case "imdb": case "imdb":
case "imgur": case "imgur":
case "tiktok": case "tiktok":
case "twitter": case "twitter":
case "reddit": 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 return
default: default:
resolve() resolve()

View File

@ -69,11 +69,11 @@ browser.webRequest.onBeforeRequest.addListener(
console.log(`Canceled ${url}`) console.log(`Canceled ${url}`)
return { cancel: true } return { cancel: true }
} }
if (newUrl === "BYPASSTAB") { // if (newUrl === "BYPASSTAB") {
console.log(`Bypassed ${details.tabId} ${url}`) // console.log(`Bypassed ${details.tabId} ${url}`)
if (!BYPASSTABs.includes(details.tabId)) BYPASSTABs.push(details.tabId) // if (!BYPASSTABs.includes(details.tabId)) BYPASSTABs.push(details.tabId)
return null // return null
} // }
console.info("Redirecting", url.href, "=>", newUrl) console.info("Redirecting", url.href, "=>", newUrl)
return { redirectUrl: 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) { async function redirectOfflineInstance(url, tabId) {
let newUrl = await servicesHelper.switchInstance(url, true) let newUrl = await servicesHelper.switchInstance(url, true)
@ -170,11 +158,46 @@ browser.contextMenus.create({
contexts: ["browser_action"], contexts: ["browser_action"],
}) })
browser.contextMenus.onClicked.addListener(info => { browser.contextMenus.create({
if (info.menuItemId == "switchInstance") utils.switchInstance() id: "bypassTab",
else if (info.menuItemId == "settings") browser.runtime.openOptionsPage() title: browser.i18n.getMessage("bypassTab"),
else if (info.menuItemId == "copyRaw") utils.copyRaw() contexts: ["page", "tab"],
else if (info.menuItemId == "unify") utils.unify() })
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) => { browser.runtime.onMessage.addListener((message, sender, sendResponse) => {