From 7611f1f0c5f4a960cf59f30602df3df02588ae33 Mon Sep 17 00:00:00 2001 From: Eduardo Rosendo Date: Mon, 30 Sep 2024 18:24:05 -0400 Subject: [PATCH] feat(district): Adds logic to upload file from multi-doc page --- src/content_delegate.js | 88 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/src/content_delegate.js b/src/content_delegate.js index c36ee076..ae6dcf23 100644 --- a/src/content_delegate.js +++ b/src/content_delegate.js @@ -871,10 +871,98 @@ ContentDelegate.prototype.handleCombinedPDFView = async function () { return; } + // Extract the URL from the `onclick` attribute of one of the "Download + // Documents" buttons + const inputs = [...document.getElementsByTagName('input')]; + const targetInputs = inputs.filter((input) => input.type === 'button'); + const url = targetInputs[0] + .getAttribute('onclick') + .replace(/p.*\//, '') // remove parent.location='/cgi-bin/ + .replace(/\'(?=$)/, ''); // remove endquote + + // Manipulate the dom elements without injecting a script + if (PACER.hasFilingCookie(document.cookie)) { + const inputs = [ + ...document.querySelectorAll("form > input[type='button']"), + ]; + inputs.map((input) => { + let button = createRecapButtonForFilers('Download and RECAP Document'); + let spinner = createRecapSpinner(); + button.addEventListener('click', (event) => { + event.preventDefault(); + + let spinners = document.querySelectorAll('.recap-btn-spinner-hidden'); + for (const spinner of spinners) { + // Access and process each process result element here + spinner.classList.remove('recap-btn-spinner-hidden'); + } + + window.postMessage({ id: url }); + }); + // insert new button next to the "Download Documents" button + input.after(spinner); + input.after(document.createTextNode('\u00A0')); + input.after(button); + }); + } else { + const forms = [...document.querySelectorAll('form')]; + forms.map((form) => { + form.removeAttribute('action'); + const input = form.querySelector('input'); + input.removeAttribute('onclick'); + input.disabled = true; + form.hidden = true; + const div = document.createElement('div'); + const button = document.createElement('button'); + button.textContent = 'View Document'; + button.addEventListener('click', () => window.postMessage({ id: url })); + div.appendChild(button); + const parentNode = form.parentNode; + parentNode.insertBefore(div, form); + }); + } + // When we receive the message from the above submit method, submit the form + // via fetch so we can get the document before the browser does. + window.addEventListener('message', this.onDownloadSubmit.bind(this)); + await this.checkSingleDocInCombinedPDFPage(); }; +ContentDelegate.prototype.onDownloadSubmit = async function () { + // Make the Back button redisplay the previous page. + window.onpopstate = function (event) { + if (event.state.content) { + document.documentElement.innerHTML = event.state.content; + } + }; + history.replaceState({ content: document.documentElement.innerHTML }, ''); + // tell the user to wait + $('body').css('cursor', 'wait'); + + let previousPageHtml = copyPDFDocumentPage(); + let pdfData = PACER.parseDataFromReceipt(); + + const browserSpecificFetch = + navigator.userAgent.indexOf('Safari') + + navigator.userAgent.indexOf('Chrome') < + 0 + ? fetch + : window.fetch; + + const documentRequest = await browserSpecificFetch(event.data.id); + let documentBlob = await documentRequest.blob(); + + let requestHandler = handleDocFormResponse.bind(this); + requestHandler( + documentRequest.headers.get('Content-Type'), + documentBlob, + null, + previousPageHtml, + pdfData + ); +}; + // checks if a specific document within a combined PDF page is available in // the Recap archive. The document is identified by its PACER document ID. // If the document is found, it inserts a banner to inform the user of its