-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Disable export buttons on form submit #11541
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mekarpeles
merged 2 commits into
internetarchive:master
from
jimchamp:disable-export-buttons-on-form-submit
Dec 9, 2025
+25
−142
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,144 +1,26 @@ | ||
| /** | ||
| * Enhances export buttons on /account/import so that when an export starts: | ||
| * - The clicked button shows "Downloading..." | ||
| * - The button is disabled and styled as unavailable (grey) | ||
| * - Subsequent clicks are prevented while the download is in progress | ||
| * Switches the given button to a disabled state. | ||
| * | ||
| * Implementation detail: We use fetch() to retrieve the CSV as a Blob so we | ||
| * can reliably detect completion across browsers (some do not fire iframe | ||
| * load events when Content-Disposition: attachment is used). Once the blob is | ||
| * received, we trigger a programmatic download and then restore button state. | ||
| * @param {HTMLElement} buttonElement | ||
| */ | ||
|
|
||
| function getSubmitButton(formElement) { | ||
| // Each export form has a single submit input | ||
| return formElement.querySelector('input[type="submit"], button[type="submit"]'); | ||
| } | ||
|
|
||
| function disableButton(buttonElement) { | ||
| if (!buttonElement) return; | ||
| buttonElement.dataset.originalValue = buttonElement.value || buttonElement.textContent || ''; | ||
| if (buttonElement.tagName === 'INPUT') { | ||
| buttonElement.value = 'Downloading...'; | ||
| } else { | ||
| buttonElement.textContent = 'Downloading...'; | ||
| } | ||
| buttonElement.setAttribute('disabled', 'true'); | ||
| buttonElement.setAttribute('aria-disabled', 'true'); | ||
| buttonElement.classList.remove('cta-btn--available'); | ||
| buttonElement.classList.add('cta-btn--unavailable'); | ||
| // Show loading affordance consistent with other unavailable actions | ||
| buttonElement.classList.add('cta-btn--unavailable--load'); | ||
| } | ||
|
|
||
| function enableButton(buttonElement) { | ||
| if (!buttonElement) return; | ||
| const original = buttonElement.dataset.originalValue || ''; | ||
| if (buttonElement.tagName === 'INPUT') { | ||
| buttonElement.value = original; | ||
| } else { | ||
| buttonElement.textContent = original; | ||
| } | ||
| buttonElement.removeAttribute('disabled'); | ||
| buttonElement.setAttribute('aria-disabled', 'false'); | ||
| buttonElement.classList.remove('cta-btn--unavailable'); | ||
| buttonElement.classList.remove('cta-btn--unavailable--load'); | ||
| buttonElement.classList.add('cta-btn--available'); | ||
| delete buttonElement.dataset.originalValue; | ||
| } | ||
|
|
||
| function buildGetUrlFromForm(formElement) { | ||
| const action = formElement.getAttribute('action') || ''; | ||
| const params = new URLSearchParams(new FormData(formElement)); | ||
| const query = params.toString(); | ||
| return query ? `${action}?${query}` : action; | ||
| } | ||
|
|
||
| function parseFilenameFromContentDisposition(headerValue) { | ||
| if (!headerValue) return null; | ||
| // RFC 5987 filename*= | ||
| const starMatch = /filename\*=UTF-8''([^;]+)/i.exec(headerValue); | ||
| if (starMatch && starMatch[1]) { | ||
| try { | ||
| return decodeURIComponent(starMatch[1]); | ||
| } catch (_) { | ||
| return starMatch[1]; | ||
| } | ||
| } | ||
| // Basic filename="..." | ||
| const plainMatch = /filename="?([^";]+)"?/i.exec(headerValue); | ||
| return plainMatch && plainMatch[1] ? plainMatch[1] : null; | ||
| } | ||
|
|
||
| async function fetchAndDownload(url) { | ||
| const response = await fetch(url, { credentials: 'same-origin' }); | ||
| if (!response.ok) { | ||
| const error = new Error(`Export request failed with status ${response.status}`); | ||
| error.status = response.status; | ||
| throw error; | ||
| } | ||
| const blob = await response.blob(); | ||
| const contentDisposition = response.headers.get('Content-Disposition') || response.headers.get('content-disposition'); | ||
| const fallbackName = 'OpenLibrary_Export.csv'; | ||
| const filename = parseFilenameFromContentDisposition(contentDisposition) || fallbackName; | ||
| const objectUrl = URL.createObjectURL(blob); | ||
| const a = document.createElement('a'); | ||
| a.href = objectUrl; | ||
| a.download = filename; | ||
| document.body.appendChild(a); | ||
| a.click(); | ||
| setTimeout(() => { | ||
| URL.revokeObjectURL(objectUrl); | ||
| if (a.parentNode) a.parentNode.removeChild(a); | ||
| }, 0); | ||
| } | ||
|
|
||
| export function initPatronExportButtons() { | ||
| // Guard: only on the import page | ||
| if (location.pathname !== '/account/import') return; | ||
|
|
||
| const exportForms = document.querySelectorAll('form[action="/account/export"][method="GET" i]'); | ||
| if (!exportForms.length) return; | ||
|
|
||
| exportForms.forEach((form) => { | ||
| const submitButton = getSubmitButton(form); | ||
| if (!submitButton) return; | ||
|
|
||
| // Prevent double-binding | ||
| if (form.dataset.patronExportBound === 'true') return; | ||
| form.dataset.patronExportBound = 'true'; | ||
|
|
||
| form.addEventListener('submit', (event) => { | ||
| // If already disabled, block to prevent duplicates | ||
| if (submitButton.hasAttribute('disabled')) { | ||
| event.preventDefault(); | ||
| return; | ||
| } | ||
|
|
||
| event.preventDefault(); | ||
| /** | ||
| * Adds `submit` listeners for the given form elements. | ||
| * | ||
| * When any of the given forms are submitted, the form's | ||
| * submit button is disabled. | ||
| * | ||
| * @param {NodeList<HTMLFormElement>} elems | ||
| */ | ||
| export function initPatronExportForms(elems) { | ||
| elems.forEach((form) => { | ||
| const submitButton = form.querySelector('input[type=submit]') | ||
mekarpeles marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| form.addEventListener('submit', () => { | ||
mekarpeles marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| disableButton(submitButton); | ||
| const startMs = performance.now(); | ||
| const url = buildGetUrlFromForm(form); | ||
| fetchAndDownload(url) | ||
| .catch(() => { | ||
| // Swallow errors but restore button; | ||
| }) | ||
| .finally(() => { | ||
| // Ensure state stays visible for at least a short time | ||
| const minDurationMs = 500; | ||
| const elapsed = performance.now() - startMs; | ||
| const remaining = Math.max(0, minDurationMs - elapsed); | ||
| setTimeout(() => enableButton(submitButton), remaining); | ||
| }); | ||
| }); | ||
|
|
||
| // Also handle direct button clicks that might bypass submit event ordering | ||
| submitButton.addEventListener('click', (event) => { | ||
| if (submitButton.hasAttribute('disabled')) { | ||
| event.preventDefault(); | ||
| } | ||
| }); | ||
| }); | ||
| }) | ||
| }) | ||
mekarpeles marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.