/
popup.js
45 lines (35 loc) · 910 Bytes
/
popup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import {
loadProxies,
reloadTab,
transformURL,
} from './shared.js'
if (typeof globalThis.browser === 'undefined') {
globalThis.browser = chrome
}
async function main() {
const list = document.getElementById('proxy-list')
const proxies = await loadProxies()
for (const proxy of proxies) {
const button = document.createElement('button')
button.type = 'button'
button.innerText = proxy.name
button.title = proxy.url
button.value = proxy.url
button.addEventListener('click', onClick)
list.appendChild(button)
}
}
async function onClick(evt) {
const tabs = await browser.tabs.query({ active: true, currentWindow: true })
if (tabs.length !== 1) {
console.error('failed to get current tab')
return
}
const tab = tabs[0]
const proxy = evt.target.value
const oldURL = tab.url
const newURL = transformURL(oldURL, proxy)
await reloadTab(tab, newURL)
window.close()
}
main()