|
| 1 | +const checkmark = '<svg width="28" height="28" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="3" stroke="currentColor"><path vector-effect="non-scaling-stroke" stroke-linecap="round" stroke-linejoin="round" d="m4.5 12.75 6 6 9-13.5"/></svg>'; |
| 2 | + |
| 3 | +function getCookie(name: string) { |
| 4 | + const value = `; ${document.cookie}`; |
| 5 | + const parts = value.split(`; ${name}=`); |
| 6 | + if (parts.length === 2) { // @ts-ignore |
| 7 | + return parts.pop().split(';').shift(); |
| 8 | + } |
| 9 | +} |
| 10 | + |
| 11 | + |
| 12 | +function handleTransferOfSession() { |
| 13 | + const transferNowButton = document.querySelector('[data-l10n-id="index.transferSessionNow"]')! as HTMLButtonElement; |
| 14 | + |
| 15 | + transferNowButton.addEventListener('click', async () => { |
| 16 | + transferNowButton.style.display = 'inline-flex'; |
| 17 | + transferNowButton.style.alignItems = 'center'; |
| 18 | + transferNowButton.style.justifyContent = 'center'; |
| 19 | + transferNowButton.innerHTML = `${checkmark}`; |
| 20 | + transferNowButton.disabled = true; |
| 21 | + |
| 22 | + const responseWithId = await fetch("./tokenTransfer", { |
| 23 | + method: "POST", |
| 24 | + headers: { |
| 25 | + "Content-Type": "application/json" |
| 26 | + }, |
| 27 | + body: JSON.stringify({ |
| 28 | + prefsHttp: getCookie('prefsHttp'), |
| 29 | + token: getCookie('token'), |
| 30 | + }) |
| 31 | + }) |
| 32 | + |
| 33 | + const copyLinkSection = document.getElementById('copy-link-section') |
| 34 | + if (!copyLinkSection) return; |
| 35 | + copyLinkSection.style.display = 'block'; |
| 36 | + |
| 37 | + const copyButton = document.querySelector('#copy-link-section .btn-secondary') as HTMLButtonElement |
| 38 | + const responseData = await responseWithId.json(); |
| 39 | + copyButton.addEventListener('click', async ()=>{ |
| 40 | + await navigator.clipboard.writeText(responseData.id); |
| 41 | + copyButton.style.display = 'inline-flex'; |
| 42 | + copyButton.style.alignItems = 'center'; |
| 43 | + copyButton.style.justifyContent = 'center'; |
| 44 | + copyButton.innerHTML = `${checkmark}`; |
| 45 | + copyButton.disabled = true; |
| 46 | + }) |
| 47 | + }); |
| 48 | +} |
| 49 | + |
| 50 | + |
| 51 | +const handleSettingsButtonClick = () => { |
| 52 | + const settingsButton = document.querySelector('.settings-button')!; |
| 53 | + const settingsDialog = document.getElementById('settings-dialog') as HTMLDialogElement; |
| 54 | + let initialSettingsHtml: string; |
| 55 | + |
| 56 | + settingsDialog.addEventListener('click', (e) => { |
| 57 | + if (e.target === settingsDialog) { |
| 58 | + settingsDialog.close(); |
| 59 | + settingsDialog.innerHTML = initialSettingsHtml; |
| 60 | + handleMenuBarClicked(); |
| 61 | + handleTransferOfSession(); |
| 62 | + } |
| 63 | + }); |
| 64 | + |
| 65 | + settingsButton.addEventListener('click', () => { |
| 66 | + initialSettingsHtml = settingsDialog.innerHTML; |
| 67 | + settingsDialog.showModal(); |
| 68 | + }); |
| 69 | +}; |
| 70 | + |
| 71 | + |
| 72 | +const handleMenuBarClicked = () => { |
| 73 | + const menuBar = document.getElementById('button-bar')!; |
| 74 | + menuBar.querySelectorAll('button').forEach((button, index)=>{ |
| 75 | + button.addEventListener('click', ()=>{ |
| 76 | + menuBar.querySelectorAll('button').forEach((btn)=>btn.classList.remove('active-btn')); |
| 77 | + button.classList.add('active-btn'); |
| 78 | + |
| 79 | + const sections: NodeListOf<HTMLDivElement> = document.querySelectorAll('#settings-dialog > div'); |
| 80 | + sections.forEach((section, index)=>index >= 1 && (section.style.display = 'none')); |
| 81 | + (sections[index +1] as HTMLElement).style.display = 'block'; |
| 82 | + }); |
| 83 | + }) |
| 84 | + |
| 85 | + const transferSessionButton = document.getElementById('transferSessionButton') |
| 86 | + const codeInputField = document.getElementById('codeInput') as HTMLInputElement |
| 87 | + if (transferSessionButton) { |
| 88 | + transferSessionButton.addEventListener('click', ()=>{ |
| 89 | + const code = codeInputField.value |
| 90 | + fetch("./tokenTransfer/"+code, { |
| 91 | + method: 'GET' |
| 92 | + }) |
| 93 | + .then(res => res.json()) |
| 94 | + .then(()=>{ |
| 95 | + window.location.reload() |
| 96 | + }) |
| 97 | + }); |
| 98 | + } |
| 99 | + |
| 100 | + if (codeInputField) { |
| 101 | + codeInputField.addEventListener('input', (e)=>{ |
| 102 | + if ((e.target as HTMLInputElement).value?.length === 36) { |
| 103 | + transferSessionButton?.removeAttribute('disabled'); |
| 104 | + } else { |
| 105 | + transferSessionButton?.setAttribute('disabled', 'true'); |
| 106 | + } |
| 107 | + }) |
| 108 | + } |
| 109 | + |
| 110 | +} |
| 111 | + |
| 112 | +window.addEventListener('load', () => { |
| 113 | + handleSettingsButtonClick(); |
| 114 | + handleMenuBarClicked(); |
| 115 | + handleTransferOfSession(); |
| 116 | +}); |
0 commit comments