blob: d6c5e3004b6deb6b0333bfca4f4aaa2fdd6bfe01 (
plain) (
blame)
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
window.browser = window.browser || window.chrome
import servicesHelper from "./services.js"
function getRandomInstance(instances) {
return instances[~~(instances.length * Math.random())]
}
function camelCase(str) {
return str.charAt(0).toUpperCase() + str.slice(1)
}
function protocolHost(url) {
if (url.username && url.password) return `${url.protocol}//${url.username}:${url.password}@${url.host}`
return `${url.protocol}//${url.host}`
}
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 servicesHelper.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(test) {
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 servicesHelper.switchInstance(url)
if (newUrl) {
if (!test) browser.tabs.update({ url: newUrl })
resolve(true)
} else resolve()
}
})
})
}
export default {
getRandomInstance,
protocolHost,
switchInstance,
copyRaw,
camelCase,
}
|