Added context menu to redirect

Closes https://github.com/libredirect/libredirect/issues/414
This commit is contained in:
Hygna 2022-10-08 17:48:24 +01:00
parent 9560cfc3e7
commit 6fdf656630
No known key found for this signature in database
4 changed files with 39 additions and 30 deletions

View File

@ -214,5 +214,9 @@
"toggleTab": {
"message": "Toggle redirects in this tab",
"description": "Used in context menus when right clicking on a page/tab"
},
"redirectLink": {
"message": "Attempt to redirect this hyperlink",
"description": "Used in context menus when right clicking on a hyperlink"
}
}

View File

@ -407,7 +407,6 @@ function redirect(url, type, initiator, forceRedirection) {
}
function computeService(url, returnFrontend) {
console.log(url)
return new Promise(resolve => {
fetch("/config/config.json")
.then(response => response.text())

View File

@ -414,7 +414,7 @@
},
"imageType": "svg",
"embeddable": false,
"url": "https://search.joinpeertube.org"
"url": "https://joinpeertube.org"
},
"lbry": {
"frontends": {

View File

@ -21,28 +21,28 @@ function initDefaults() {
browser.runtime.onInstalled.addListener(details => {
if (details.previousVersion != browser.runtime.getManifest().version) {
switch (details.reason) {
case "install":
initDefaults()
break
case "update":
fetch("/instances/blacklist.json")
.then(response => response.text())
.then(async data => {
browser.storage.local.set({ blacklists: JSON.parse(data) }, async () => {
switch (details.previousVersion) {
case "2.2.0":
case "2.2.1":
await generalHelper.initDefaults()
await servicesHelper.initDefaults()
await servicesHelper.upgradeOptions()
break
default:
await servicesHelper.processUpdate()
}
switch (details.reason) {
case "install":
initDefaults()
break
case "update":
fetch("/instances/blacklist.json")
.then(response => response.text())
.then(async data => {
browser.storage.local.set({ blacklists: JSON.parse(data) }, async () => {
switch (details.previousVersion) {
case "2.2.0":
case "2.2.1":
await generalHelper.initDefaults()
await servicesHelper.initDefaults()
await servicesHelper.upgradeOptions()
break
default:
await servicesHelper.processUpdate()
}
})
})
})
}
}
}
})
@ -166,18 +166,21 @@ browser.contextMenus.create({
contexts: ["page", "tab"],
})
browser.contextMenus.create({
id: "redirectLink",
title: browser.i18n.getMessage("redirectLink"),
contexts: ["link"],
})
function handleToggleTab(tab) {
return new Promise(async resolve => {
console.log(tabIdRedirects[tab.id])
switch (tabIdRedirects[tab.id]) {
case false:
console.log("kj")
const newUrl = await servicesHelper.reverse(tab.url, true)
if (newUrl) browser.tabs.update(tab.id, { url: newUrl })
resolve()
return
case true:
console.log("lsad")
browser.tabs.reload(tab.id)
resolve()
return
@ -211,14 +214,10 @@ browser.contextMenus.onClicked.addListener((info, tab) => {
resolve()
return
} else {
console.log("n")
const url = new URL(tab.url)
const service = await servicesHelper.computeService(url)
console.log(service)
if (service) {
console.log("h")
browser.storage.local.get("options", async r => {
console.log(r.options[service])
if (r.options[service].enabled) tabIdRedirects[tab.id] = false
else tabIdRedirects[tab.id] = true
await handleToggleTab(tab)
@ -232,6 +231,13 @@ browser.contextMenus.onClicked.addListener((info, tab) => {
return
}
}
case "redirectLink":
console.log(info.linkUrl)
const tmpUrl = new URL(info.linkUrl)
const newUrl = servicesHelper.redirect(tmpUrl, "main_frame", null, true)
if (newUrl) browser.tabs.create({ url: newUrl })
resolve()
return
}
})
})