pzp-hub/lib/public/bootstrap.js

53 lines
1.4 KiB
JavaScript

let hasFocus = true
window.addEventListener('blur', () => {
hasFocus = false
})
window.addEventListener('focus', () => {
hasFocus = true
})
const host = window.location.hostname
const hostFormat = /[a-zA-Z]/.test(window.location.hostname) ? 'dns' : 'ip4';
const { tcpPort, shseCredentials } = window.hubInvite
const uri = `ppppp://invite/join/${hostFormat}/${host}/tcp/${tcpPort}/shse/${shseCredentials}`
const inviteLinkElem = document.getElementById('invite')
inviteLinkElem.href = uri
// Autoredirect to the PPPPP URI as soon as possible
setTimeout(() => {
window.location.replace(uri)
}, 100)
// Redirect to uri or show failure state
inviteLinkElem.onclick = function handleURI(ev) {
ev.preventDefault()
const uri = inviteLinkElem.href
inviteLinkElem.classList.remove('hidden')
setTimeout(function () {
if (hasFocus) {
inviteLinkElem.classList.add('hidden')
}
}, 5000)
window.location.replace(uri)
}
function copyText(text) {
const textArea = document.createElement("textarea")
textArea.value = text
textArea.style.position = "absolute"
textArea.style.left = "-999999px"
document.body.prepend(textArea)
textArea.select()
document.execCommand('copy')
textArea.remove()
}
const copyLinkElem = document.getElementById('copy')
copyLinkElem.onclick = function handleCopy(ev) {
copyText(uri);
copyLinkElem.innerHTML = copyLinkElem.innerHTML += '✓'
}