Skip to content

Commit

Permalink
Avoided Redundancy in ping test #827
Browse files Browse the repository at this point in the history
  • Loading branch information
ManeraKai committed Oct 21, 2023
1 parent 0710585 commit 8649bad
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 36 deletions.
69 changes: 36 additions & 33 deletions src/pages/background/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,38 +90,43 @@ browser.tabs.onRemoved.addListener(tabId => {
browser.commands.onCommand.addListener(async command => {
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)
}
else if (command == "redirect") {
browser.tabs.query({ active: true, currentWindow: true }, async tabs => {
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
})
switch (command) {
case "switchInstance":
const newUrl = await servicesHelper.switchInstance(url)
if (newUrl) browser.tabs.update({ url: newUrl })
break
case "copyRaw": {
servicesHelper.copyRaw(url)
break
}
case "redirect": {
browser.tabs.query({ active: true, currentWindow: true }, async tabs => {
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
})
}
}
}
})
}
else if (command == "reverse") {
browser.tabs.query({ active: true, currentWindow: true }, async tabs => {
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
})
})
break
}
case "reverse": {
browser.tabs.query({ active: true, currentWindow: true }, async tabs => {
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
})
}
}
}
})
})
break
}
}
})
})
Expand Down Expand Up @@ -201,7 +206,6 @@ browser.contextMenus.onClicked.addListener(async (info) => {

case 'copyReverseLink': {
const url = new URL(info.linkUrl)
console.log(url)
await servicesHelper.copyRaw(url)
return
}
Expand Down Expand Up @@ -250,7 +254,6 @@ browser.contextMenus.onClicked.addListener(async (info) => {
else browser.tabs.create({ url: newUrl })
}
})

return
}
case 'reverseBookmark':
Expand Down Expand Up @@ -302,4 +305,4 @@ browser.runtime.onMessage.addListener((request, sender, sendResponse) => {
}
})
}
});
})
9 changes: 6 additions & 3 deletions src/pages/options/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,18 +338,21 @@ async function ping(frontend) {
]

let pingCache = await utils.getPingCache()
let redundancyList = {}
for (const element of instanceElements) {
let span = element.getElementsByClassName('ping')[0]
if (!span) span = document.createElement('span')
span.classList = ['ping']
span.innerHTML = '<span style="color:lightblue">pinging...</span>'
element.appendChild(span)

const href = element.getElementsByTagName('a')[0].href
const time = await utils.ping(href)
const innerHTML = element.getElementsByTagName('a')[0].innerHTML
const time = redundancyList[innerHTML] ?? await utils.ping(href)
const { color, text } = processTime(time)
span.innerHTML = `<span style="color:${color};">${text}</span>`
pingCache[element.getElementsByTagName('a')[0].innerHTML] = time
pingCache[innerHTML] = time
redundancyList[innerHTML] = time

browser.storage.local.set({ pingCache })
}
}
Expand Down

0 comments on commit 8649bad

Please sign in to comment.