Skip to content

Commit

Permalink
Cleaning code
Browse files Browse the repository at this point in the history
  • Loading branch information
ManeraKai committed May 20, 2024
1 parent 9ebfbd9 commit 462011d
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 210 deletions.
26 changes: 11 additions & 15 deletions src/assets/javascripts/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -792,25 +792,21 @@ function processUpdate() {

/**
* @param {URL} url
* @param {boolean} test
*/
async function copyRaw(url, test) {
async function copyRaw(url) {
const newUrl = await reverse(url)
if (newUrl) {
if (!test) {
if (!isChrome) {
navigator.clipboard.writeText(newUrl)
} else {
var copyFrom = document.createElement("textarea");
copyFrom.textContent = newUrl;
document.body.appendChild(copyFrom);
copyFrom.select()
document.execCommand('copy')
copyFrom.blur();
document.body.removeChild(copyFrom);
}
if (!isChrome) {
navigator.clipboard.writeText(newUrl)
} else {
var copyFrom = document.createElement("textarea");
copyFrom.textContent = newUrl;
document.body.appendChild(copyFrom);
copyFrom.select()
document.execCommand('copy')
copyFrom.blur();
document.body.removeChild(copyFrom);
}
return newUrl
}
}

Expand Down
52 changes: 11 additions & 41 deletions src/assets/javascripts/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,46 +78,27 @@ function getConfig() {
* @returns {Promise<Object.<string, Option | string[]>>}
*/
function getOptions() {
return new Promise(resolve =>
browser.storage.local.get("options", r => {
resolve(r.options)
})
)
return new Promise(resolve => browser.storage.local.get("options", r => resolve(r.options)))
}

function getPingCache() {
return new Promise(resolve =>
browser.storage.local.get("pingCache", r => {
resolve(r.pingCache ?? {})
})
)
return new Promise(resolve => browser.storage.local.get("pingCache", r => resolve(r.pingCache ?? {})))
}

function getBlacklist(options) {
return new Promise(resolve => {
let url
if (options.fetchInstances == 'github') url = 'https://raw.githubusercontent.com/libredirect/instances/main/blacklist.json'
else if (options.fetchInstances == 'codeberg') url = 'https://codeberg.org/LibRedirect/instances/raw/branch/main/blacklist.json'
else {
resolve('disabled')
return
}
else return resolve('disabled')
const http = new XMLHttpRequest()
http.open("GET", url, true)
http.onreadystatechange = () => {
if (http.status === 200 && http.readyState == XMLHttpRequest.DONE) {
if (http.status === 200 && http.readyState == XMLHttpRequest.DONE)
resolve(JSON.parse(http.responseText))
return
}
}
http.onerror = () => {
resolve()
return
}
http.ontimeout = () => {
resolve()
return
}
http.onerror = () => resolve()
http.ontimeout = () => resolve()
http.send(null)
})
}
Expand All @@ -127,26 +108,15 @@ function getList(options) {
let url
if (options.fetchInstances == 'github') url = 'https://raw.githubusercontent.com/libredirect/instances/main/data.json'
else if (options.fetchInstances == 'codeberg') url = 'https://codeberg.org/LibRedirect/instances/raw/branch/main/data.json'
else {
resolve('disabled')
return
}
else return resolve('disabled')
const http = new XMLHttpRequest()
http.open("GET", url, true)
http.onreadystatechange = () => {
if (http.status === 200 && http.readyState == XMLHttpRequest.DONE) {
resolve(JSON.parse(http.responseText))
return
}
}
http.onerror = () => {
resolve()
return
}
http.ontimeout = () => {
resolve()
return
if (http.status === 200 && http.readyState == XMLHttpRequest.DONE)
return resolve(JSON.parse(http.responseText))
}
http.onerror = () => resolve()
http.ontimeout = () => resolve()
http.send(null)
})
}
Expand Down
61 changes: 17 additions & 44 deletions src/pages/background/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,15 @@ browser.commands.onCommand.addListener(async command => {
browser.tabs.query({ active: true, currentWindow: true }, async tabs => {
const url = new URL(tabs[0].url)
switch (command) {
case "switchInstance":
case "switchInstance": {
const newUrl = await servicesHelper.switchInstance(url)
if (newUrl) browser.tabs.update({ url: newUrl })
break
case "copyRaw": {
}
case "copyRaw":
servicesHelper.copyRaw(url)
break
}
case "redirect": {
case "redirect":
browser.tabs.query({ active: true, currentWindow: true }, async tabs => {
if (tabs[0].url) {
const url = new URL(tabs[0].url)
Expand All @@ -113,8 +113,7 @@ browser.commands.onCommand.addListener(async command => {
}
})
break
}
case "reverse": {
case "reverse":
browser.tabs.query({ active: true, currentWindow: true }, async tabs => {
if (tabs[0].url) {
const url = new URL(tabs[0].url)
Expand All @@ -127,7 +126,6 @@ browser.commands.onCommand.addListener(async command => {
}
})
break
}
}
})
})
Expand Down Expand Up @@ -164,20 +162,18 @@ browser.contextMenus.onClicked.addListener(async (info) => {
if (newUrl) browser.tabs.update({ url: newUrl })
return
}
case 'settingsTab': {
case 'settingsTab':
browser.runtime.openOptionsPage()
return
}
case 'copyReverseTab': {
case 'copyReverseTab':
browser.tabs.query({ active: true, currentWindow: true }, async tabs => {
if (tabs[0].url) {
const url = new URL(tabs[0].url)
servicesHelper.copyRaw(url)
}
})
return
}
case 'reverseTab': {
case 'reverseTab':
browser.tabs.query({ active: true, currentWindow: true }, async tabs => {
if (tabs[0].url) {
const url = new URL(tabs[0].url)
Expand All @@ -190,8 +186,7 @@ browser.contextMenus.onClicked.addListener(async (info) => {
}
})
return
}
case 'redirectTab': {
case 'redirectTab':
browser.tabs.query({ active: true, currentWindow: true }, async tabs => {
if (tabs[0].url) {
const url = new URL(tabs[0].url)
Expand All @@ -204,8 +199,6 @@ browser.contextMenus.onClicked.addListener(async (info) => {
}
})
return
}

case 'copyReverseLink': {
const url = new URL(info.linkUrl)
await servicesHelper.copyRaw(url)
Expand Down Expand Up @@ -238,7 +231,6 @@ browser.contextMenus.onClicked.addListener(async (info) => {
}
return
}

case 'bypassLink':
case 'bypassLinkInNewTab': {
const url = new URL(info.linkUrl)
Expand All @@ -253,17 +245,14 @@ browser.contextMenus.onClicked.addListener(async (info) => {
}
return
}

case 'copyReverseBookmark': {
case 'copyReverseBookmark':
browser.bookmarks.get(info.bookmarkId, bookmarks => {
const url = new URL(bookmarks[0].url)
servicesHelper.copyRaw(url)
});
return
}

case 'redirectBookmark':
case 'redirectBookmarkInNewTab': {
case 'redirectBookmarkInNewTab':
browser.bookmarks.get(info.bookmarkId, bookmarks => {
const url = new URL(bookmarks[0].url)
const newUrl = servicesHelper.redirect(url, "main_frame", null, true)
Expand All @@ -273,9 +262,8 @@ browser.contextMenus.onClicked.addListener(async (info) => {
}
})
return
}
case 'reverseBookmark':
case 'reverseBookmarkInNewTab': {
case 'reverseBookmarkInNewTab':
browser.bookmarks.get(info.bookmarkId, async bookmarks => {
const url = new URL(bookmarks[0].url)
const newUrl = await servicesHelper.reverse(url)
Expand All @@ -292,24 +280,17 @@ browser.contextMenus.onClicked.addListener(async (info) => {
}
})
return
}

case 'bypassBookmark':
case 'bypassBookmarkInNewTab': {
case 'bypassBookmarkInNewTab':
browser.bookmarks.get(info.bookmarkId, async bookmarks => {
const url = new URL(bookmarks[0].url)
if (info.menuItemId == "bypassBookmark") {
browser.tabs.update({ url: url.href }, tab => {
tabIdRedirects[tab.id] = false
})
browser.tabs.update({ url: url.href }, tab => tabIdRedirects[tab.id] = false)
} else {
browser.tabs.create({ url: url.href }, tab => {
tabIdRedirects[tab.id] = false
})
browser.tabs.create({ url: url.href }, tab => tabIdRedirects[tab.id] = false)
}
return
})
}
}
})

Expand All @@ -319,11 +300,7 @@ browser.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (tabs[0].url) {
const url = new URL(tabs[0].url)
const newUrl = await servicesHelper.reverse(url)
if (newUrl) {
browser.tabs.update(tabs[0].id, { url: newUrl }, () => {
tabIdRedirects[tabs[0].id] = false
})
}
if (newUrl) browser.tabs.update(tabs[0].id, { url: newUrl }, () => tabIdRedirects[tabs[0].id] = false)
}
})
}
Expand All @@ -332,11 +309,7 @@ browser.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (tabs[0].url) {
const url = new URL(tabs[0].url)
const newUrl = servicesHelper.redirect(url, "main_frame", null, true)
if (newUrl) {
browser.tabs.update(tabs[0].id, { url: newUrl }, () => {
tabIdRedirects[tabs[0].id] = true
})
}
if (newUrl) browser.tabs.update(tabs[0].id, { url: newUrl }, () => tabIdRedirects[tabs[0].id] = true)
}
})
}
Expand Down
Loading

0 comments on commit 462011d

Please sign in to comment.