|
1 | | -const localExportDisplayElement = document.getElementById('local-storage-export'); |
2 | | -const localCopyButton = document.getElementById('copy-local'); |
3 | | -const localDownloadButton = document.getElementById('download-local'); |
| 1 | +const exportForm = document.getElementById('export'); |
| 2 | +const exportValueTextarea = document.getElementById('export-value'); |
| 3 | +const exportCopyButton = document.getElementById('export-copy'); |
| 4 | +const exportDownloadButton = document.getElementById('export-download'); |
4 | 5 |
|
5 | | -const localImportTextarea = document.getElementById('local-storage-import'); |
6 | | -const localOverwriteWarning = document.getElementById('overwrite-warning'); |
7 | | -const localRestoreButton = document.getElementById('restore-local'); |
| 6 | +const importForm = document.getElementById('import'); |
| 7 | +const importValueTextarea = document.getElementById('import-value'); |
| 8 | +const importSubmitButton = document.getElementById('import-submit'); |
| 9 | +const importWarningElement = document.getElementById('import-warning'); |
8 | 10 |
|
9 | 11 | const sleep = ms => new Promise(resolve => setTimeout(() => resolve(), ms)); |
10 | 12 |
|
11 | 13 | const onStorageChanged = async function () { |
12 | 14 | const storageLocal = await browser.storage.local.get(); |
13 | 15 | const stringifiedStorage = JSON.stringify(storageLocal, null, 2); |
14 | 16 |
|
15 | | - localExportDisplayElement.textContent = stringifiedStorage; |
16 | | - localOverwriteWarning.dataset.hidden = Object.keys(storageLocal).length === 0; |
| 17 | + exportValueTextarea.value = stringifiedStorage; |
| 18 | + importWarningElement.dataset.hidden = Object.keys(storageLocal).length === 0; |
17 | 19 | }; |
18 | 20 |
|
19 | | -const localCopy = async function () { |
20 | | - if (localCopyButton.classList.contains('copied')) { return; } |
| 21 | +const localCopy = () => { |
| 22 | + if (exportCopyButton.classList.contains('copied')) { return; } |
21 | 23 |
|
22 | | - navigator.clipboard.writeText(localExportDisplayElement.textContent).then(async () => { |
23 | | - localCopyButton.classList.add('copied'); |
| 24 | + navigator.clipboard.writeText(exportValueTextarea.value).then(async () => { |
| 25 | + exportCopyButton.classList.add('copied'); |
24 | 26 | await sleep(2000); |
25 | | - localCopyButton.classList.add('fading'); |
| 27 | + exportCopyButton.classList.add('fading'); |
26 | 28 | await sleep(1000); |
27 | | - localCopyButton.classList.remove('copied', 'fading'); |
| 29 | + exportCopyButton.classList.remove('copied', 'fading'); |
28 | 30 | }); |
29 | 31 | }; |
30 | 32 |
|
@@ -53,53 +55,54 @@ const localExport = async function () { |
53 | 55 | URL.revokeObjectURL(blobUrl); |
54 | 56 | }; |
55 | 57 |
|
56 | | -const localRestore = async function () { |
57 | | - const importText = localImportTextarea.value; |
| 58 | +/** @type {(event: SubmitEvent) => void} */ |
| 59 | +function onExportSubmit (event) { |
| 60 | + event.preventDefault(); |
| 61 | + if (event.submitter === exportCopyButton) localCopy(); |
| 62 | + if (event.submitter === exportDownloadButton) localExport(); |
| 63 | +} |
| 64 | + |
| 65 | +/** @type {(event: SubmitEvent) => Promise<void>} */ |
| 66 | +async function onImportSubmit (event) { |
| 67 | + event.preventDefault(); |
| 68 | + |
| 69 | + const importText = importValueTextarea.value; |
58 | 70 |
|
59 | 71 | try { |
60 | | - localRestoreButton.disabled = true; |
| 72 | + importSubmitButton.disabled = true; |
61 | 73 |
|
62 | 74 | const parsedStorage = JSON.parse(importText); |
63 | 75 |
|
64 | | - localOverwriteWarning.dataset.forceHidden = localOverwriteWarning.dataset.hidden; |
| 76 | + importWarningElement.dataset.forceHidden = importWarningElement.dataset.hidden; |
65 | 77 |
|
66 | 78 | await browser.storage.local.clear(); |
67 | 79 | await browser.storage.local.set(parsedStorage); |
68 | 80 |
|
69 | | - localRestoreButton.classList.add('success'); |
70 | | - localRestoreButton.textContent = 'Successfully restored!'; |
71 | | - localImportTextarea.value = ''; |
| 81 | + importSubmitButton.classList.add('success'); |
| 82 | + importSubmitButton.textContent = 'Successfully restored!'; |
| 83 | + importValueTextarea.value = ''; |
72 | 84 | document.getElementById('configuration-tab').classList.add('outdated'); |
73 | 85 | } catch (exception) { |
74 | | - localRestoreButton.classList.add('failure'); |
75 | | - localRestoreButton.textContent = |
76 | | - exception instanceof SyntaxError ? 'Failed to parse backup contents!' : 'Failed to restore!'; |
| 86 | + importSubmitButton.classList.add('failure'); |
| 87 | + importSubmitButton.textContent = exception instanceof SyntaxError |
| 88 | + ? 'Failed to parse backup contents!' |
| 89 | + : 'Failed to restore!'; |
77 | 90 | console.error(exception); |
78 | 91 | } finally { |
79 | 92 | await sleep(3000); |
80 | | - localRestoreButton.disabled = false; |
81 | | - localRestoreButton.classList.remove('success', 'failure'); |
82 | | - localRestoreButton.textContent = ''; |
83 | | - delete localOverwriteWarning.dataset.forceHidden; |
| 93 | + importSubmitButton.disabled = false; |
| 94 | + importSubmitButton.classList.remove('success', 'failure'); |
| 95 | + importSubmitButton.textContent = 'Restore'; |
| 96 | + delete importWarningElement.dataset.forceHidden; |
84 | 97 | } |
85 | | -}; |
| 98 | +} |
86 | 99 |
|
87 | 100 | const renderLocalBackup = async function () { |
88 | 101 | onStorageChanged(); |
89 | 102 | browser.storage.local.onChanged.addListener(onStorageChanged); |
90 | 103 |
|
91 | | - localCopyButton.addEventListener('click', localCopy); |
92 | | - localDownloadButton.addEventListener('click', localExport); |
93 | | - |
94 | | - localRestoreButton.addEventListener('click', localRestore); |
| 104 | + exportForm.addEventListener('submit', onExportSubmit); |
| 105 | + importForm.addEventListener('submit', onImportSubmit); |
95 | 106 | }; |
96 | 107 |
|
97 | 108 | renderLocalBackup(); |
98 | | - |
99 | | -document.querySelectorAll('#backup-panel details').forEach(details => details.addEventListener('toggle', ({ currentTarget }) => { |
100 | | - if (currentTarget.open) { |
101 | | - [...currentTarget.parentNode.children] |
102 | | - .filter(element => element !== currentTarget) |
103 | | - .forEach(sibling => { sibling.open = false; }); |
104 | | - } |
105 | | -})); |
|
0 commit comments