Link menu: Redirect, Reverse, Copy Reverse. Icon menu: Settings, SwitchInstance, Copy Reverse, Redirect, Reverse redirect. https://github.com/libredirect/libredirect/issues/607

This commit is contained in:
ManeraKai 2023-02-08 11:28:57 +03:00
parent a9f192ba71
commit d0961f5d1f
4 changed files with 118 additions and 94 deletions

View File

@ -94,7 +94,7 @@
"description": "Used in context menus when right clicking on a page/tab"
},
"redirectLink": {
"message": "Attempt to redirect this hyperlink",
"message": "Attempt to redirect",
"description": "Used in context menus when right clicking on a hyperlink"
},
"about": {

View File

@ -496,9 +496,11 @@ function computeService(url, returnFrontend) {
})
}
function _switchInstance(url) {
function switchInstance(url) {
return new Promise(async resolve => {
await init()
let options = await utils.getOptions()
let config = await utils.getConfig()
const protocolHost = utils.protocolHost(url)
for (const service in config.services) {
let frontend = options[service].frontend ?? Object.keys(config.services[service].frontends)[0]
@ -522,8 +524,9 @@ function _switchInstance(url) {
function reverse(url) {
return new Promise(async resolve => {
await init()
url = new URL(url)
let options = await utils.getOptions()
let config = await utils.getConfig()
let protocolHost = utils.protocolHost(url)
for (const service in config.services) {
let frontend = options[service].frontend ?? Object.keys(config.services[service].frontends)[0]
@ -741,55 +744,14 @@ function modifyContentSecurityPolicy(details) {
}
}
function copyRaw(test, copyRawElement) {
return new Promise(resolve => {
browser.tabs.query({ active: true, currentWindow: true }, async tabs => {
let currTab = tabs[0]
if (currTab) {
let url
try {
url = new URL(currTab.url)
} catch {
resolve()
return
}
const newUrl = await reverse(url)
if (newUrl) {
resolve(newUrl)
if (test) return
navigator.clipboard.writeText(newUrl)
if (copyRawElement) {
const textElement = copyRawElement.getElementsByTagName("h4")[0]
const oldHtml = textElement.innerHTML
textElement.innerHTML = browser.i18n.getMessage("copied")
setTimeout(() => (textElement.innerHTML = oldHtml), 1000)
}
}
}
resolve()
})
})
}
function switchInstance() {
return new Promise(resolve => {
browser.tabs.query({ active: true, currentWindow: true }, async tabs => {
let currTab = tabs[0]
if (currTab) {
let url
try {
url = new URL(currTab.url)
} catch {
resolve()
return
}
const newUrl = await _switchInstance(url)
resolve(newUrl)
}
})
})
async function copyRaw(url, test) {
const newUrl = await reverse(url)
if (newUrl) {
if (!test) {
navigator.clipboard.writeText(newUrl)
}
return newUrl
}
}
export default {

View File

@ -78,11 +78,16 @@ browser.tabs.onRemoved.addListener(tabId => {
})
browser.commands.onCommand.addListener(async command => {
if (command == "switchInstance") {
const newUrl = await servicesHelper.switchInstance()
if (newUrl) browser.tabs.update({ url: newUrl })
}
else if (command == "copyRaw") servicesHelper.copyRaw()
browser.tabs.query({ active: true, currentWindow: true }, async tabs => {
const url = new URL(tabs[0].url)
if (command == "switchInstance") {
const newUrl = await servicesHelper.switchInstance(url)
if (newUrl) browser.tabs.update({ url: newUrl })
}
else if (command == "copyRaw") {
servicesHelper.copyRaw(url)
}
})
})
browser.contextMenus.create({
@ -98,50 +103,101 @@ browser.contextMenus.create({
})
browser.contextMenus.create({
id: "copyRaw",
title: browser.i18n.getMessage("copyRaw"),
id: "copyReverse",
title: 'Copy reverse',
contexts: ["browser_action"],
})
browser.contextMenus.create({
id: "redirectToOriginal",
title: browser.i18n.getMessage("redirectToOriginal"),
id: "redirectTab",
title: 'Redirect',
contexts: ["browser_action"],
})
browser.contextMenus.create({
id: "reverse",
title: 'Reverse redirect',
contexts: ["browser_action"],
})
browser.contextMenus.create({
id: "redirectLink",
title: browser.i18n.getMessage("redirectLink"),
title: 'Redirect',
contexts: ["link"],
})
browser.contextMenus.onClicked.addListener((info, tab) => {
return new Promise(async resolve => {
if (info.menuItemId == 'switchInstance') {
const newUrl = await servicesHelper.switchInstance()
if (newUrl) browser.tabs.update({ url: newUrl })
browser.contextMenus.create({
id: "reverseLink",
title: 'Reverse redirect',
contexts: ["link"],
})
browser.contextMenus.create({
id: "copyReverseLink",
title: 'Copy Reverse',
contexts: ["link"],
})
browser.contextMenus.onClicked.addListener(async (info, tab) => {
switch (info.menuItemId) {
case 'switchInstance': {
const url = new URL(info.pageUrl)
const newUrl = await servicesHelper.switchInstance(url)
if (newUrl) {
browser.tabs.update({ url: newUrl })
}
return
}
else if (info.menuItemId == 'settings') {
case 'settings': {
browser.runtime.openOptionsPage()
return
}
else if (info.menuItemId == 'copyRaw') {
servicesHelper.copyRaw()
case 'copyReverse': {
const url = new URL(info.pageUrl)
servicesHelper.copyRaw(url)
return
}
else if (info.menuItemId == 'redirectToOriginal') {
const newUrl = await servicesHelper.reverse(tab.url)
case 'reverse': {
const url = new URL(info.pageUrl)
const newUrl = await servicesHelper.reverse(url)
if (newUrl) {
tabIdRedirects[tab.id] = false
browser.tabs.update(tab.id, { url: newUrl })
browser.tabs.update({ url: newUrl })
}
return
}
else if (info.menuItemId == 'redirectLink') {
case 'redirectTab': {
const url = new URL(info.pageUrl)
const newUrl = servicesHelper.redirect(url, "main_frame", null, true)
if (newUrl) {
tabIdRedirects[tab.id] = true
browser.tabs.update({ url: newUrl })
}
return
}
case 'copyReverseLink': {
const url = new URL(info.linkUrl)
servicesHelper.copyRaw(url)
return
}
case 'redirectLink': {
const url = new URL(info.linkUrl)
const newUrl = servicesHelper.redirect(url, "main_frame", null, true)
if (newUrl) browser.tabs.create({ url: newUrl })
return
}
resolve()
return
})
case 'reverseLink': {
const url = new URL(info.linkUrl)
const newUrl = await servicesHelper.reverse(url)
if (newUrl) {
tabIdRedirects[tab.id] = false
browser.tabs.create({ url: newUrl })
}
return
}
}
})
browser.webRequest.onHeadersReceived.addListener(

View File

@ -4,23 +4,29 @@ window.browser = window.browser || window.chrome
import servicesHelper from "../../assets/javascripts/services.js"
import utils from "../../assets/javascripts/utils.js"
servicesHelper.switchInstance().then(r => {
if (!r)
document.getElementById("change_instance_div").style.display = "none"
else
document.getElementById("change_instance").addEventListener("click", async () => {
browser.tabs.update({ url: await servicesHelper.switchInstance() })
})
browser.tabs.query({ active: true, currentWindow: true }, async tabs => {
const url = new URL(tabs[0].url)
servicesHelper.switchInstance(url).then(r => {
if (!r) {
document.getElementById("change_instance_div").style.display = "none"
}
else {
document.getElementById("change_instance").addEventListener("click",
async () => browser.tabs.update({ url: await servicesHelper.switchInstance(url) })
)
}
})
servicesHelper.copyRaw(url, true).then(r => {
if (!r) {
document.getElementById("copy_raw_div").style.display = "none"
}
else {
const copy_raw = document.getElementById("copy_raw")
copy_raw.addEventListener("click", () => servicesHelper.copyRaw(url))
}
})
})
servicesHelper.copyRaw(true).then(r => {
if (!r)
document.getElementById("copy_raw_div").style.display = "none"
else {
const copy_raw = document.getElementById("copy_raw")
copy_raw.addEventListener("click", () => servicesHelper.copyRaw(false, copy_raw))
}
})
document.getElementById("more-options").addEventListener("click", () => browser.runtime.openOptionsPage())
const allSites = document.getElementsByClassName("all_sites")[0]