Skip to content

Commit

Permalink
Merge pull request #348 from freelawproject/352-fix-zip-upload-in-dis…
Browse files Browse the repository at this point in the history
…trict-court

fix(content_delegate): Fix zip upload in district courts
  • Loading branch information
mlissner authored Nov 15, 2023
2 parents 0071652 + 342cdb9 commit 3455bbb
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 13 deletions.
2 changes: 1 addition & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Features:
- None yet

Changes:
- None yet
- Enhanced logic to retrieve Zip URLs([#352](https://github.com/freelawproject/recap/issues/352)).

Fixes:
- None yet
Expand Down
57 changes: 45 additions & 12 deletions src/content_delegate.js
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,16 @@ ContentDelegate.prototype.onDownloadAllSubmit = async function (event) {
const page = document.createElement('html');
page.innerHTML = html;
const frames = page.querySelectorAll('iframe');
return frames[0].src;
if (frames.length) {
return frames[0].src;
}
// Try to extract the PDF URL from the HTML
const showTempURL = html.match(/\/cgi-bin\/show_temp.pl?(.*)/);
if (!showTempURL) {
return null;
}
// Clean the match found in the HTML
return showTempURL[0].replace(';"', '');
};

// helper function - convert string to html document
Expand Down Expand Up @@ -586,9 +595,32 @@ ContentDelegate.prototype.onDownloadAllSubmit = async function (event) {
const browserSpecificFetch =
navigator.userAgent.indexOf('Safari') + navigator.userAgent.indexOf('Chrome') < 0 ? content.fetch : window.fetch;

const options = await getItemsFromStorage('options');
const pacerCaseId = event.data.id.match(/caseid=(\d*)/)[1];
const filename = generateFileName(options, pacerCaseId);

// show loading message
let mainDiv = document.getElementById('cmecfMainContent');
let loadingMessageWrapper = document.createElement('div');
loadingMessageWrapper.setAttribute('id', 'loading-message');
loadingMessageWrapper.style.textAlign = 'center';

const spinner = document.createElement('i');
spinner.classList.add('fa', 'fa-spinner', 'fa-spin');
spinner.setAttribute('id', 'recap-button-spinner');

let spanText = document.createElement('span');
spanText.style.fontFamily = 'helvetica,arial,serif';
spanText.style.fontSize = '13px';
spanText.style.padding = '0px 10px';
spanText.innerHTML = `Download in progress for file ${filename}`;

loadingMessageWrapper.appendChild(spinner);
loadingMessageWrapper.appendChild(spanText);
mainDiv.append(loadingMessageWrapper);

// fetch the html page which contains the <iframe> link to the zip document.
const htmlPage = await browserSpecificFetch(event.data.id).then((res) => res.text());
console.log('RECAP: Successfully submitted zip file request');
const zipUrl = extractUrl(htmlPage);
//download zip file and save it to chrome storage
const blob = await fetch(zipUrl).then((res) => res.blob());
Expand All @@ -602,12 +634,6 @@ ContentDelegate.prototype.onDownloadAllSubmit = async function (event) {

// create the blob and inject it into the page
const blobUrl = URL.createObjectURL(blob);
const pacerCaseId = event.data.id.match(/caseid\=\d*/)[0].replace(/caseid\=/, '');

// load options
const options = await getItemsFromStorage('options');
// generate the filename
const filename = generateFileName(options, pacerCaseId);

if (options['recap_enabled'] && !this.restricted) {
this.recap.uploadZipFile(
Expand All @@ -622,10 +648,17 @@ ContentDelegate.prototype.onDownloadAllSubmit = async function (event) {
const link = `<a id="recap-download" href=${blobUrl} download=${filename} width="0" height="0"/>`;
const htmlBody = stringToDocBody(htmlPage);
const frame = htmlBody.querySelector('iframe');
frame.insertAdjacentHTML('beforebegin', link);
frame.src = '';
frame.onload = () => document.getElementById('recap-download').click();
document.body = htmlBody;
if (frame) {
frame.insertAdjacentHTML('beforebegin', link);
frame.src = '';
frame.onload = () => document.getElementById('recap-download').click();
document.body = htmlBody;
} else {
let loadingMessage = document.getElementById('loading-message');
loadingMessage.remove();
document.body.insertAdjacentHTML('beforebegin', link);
document.getElementById('recap-download').click();
}
history.pushState({ content: document.body.innerHTML }, '');
}
}
Expand Down

0 comments on commit 3455bbb

Please sign in to comment.