Created
October 27, 2017 21:25
-
-
Save DSDev-NickHogle/e2b09feee4430dfe16f45ec89b6a1ae6 to your computer and use it in GitHub Desktop.
Add a "download all" button to easily download all EOBs on mymoda.com. Names each PDF file according to service dates and providers.
This file contains 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
(() => { | |
class LibLoader { | |
constructor() { | |
const el = this.el = document.createElement('pre'); | |
this.b = document.getElementsByTagName('body')[0]; | |
this.otherjQuery = false; | |
el.style.position = 'fixed'; | |
el.style.top = '0'; | |
el.style.padding = '5px 10px'; | |
el.style.zIndex = 9001; | |
el.style.fontSize = '12px'; | |
el.style.color = '#222'; | |
el.style.backgroundColor = '#f99'; | |
} | |
showMsg(msg) { | |
if (!document.body.contains(this.el)) | |
this.b.appendChild(this.el); | |
this.el.appendChild( document.createTextNode(`${msg}\n`) ); | |
window.setTimeout(() => { | |
if (document.body.contains(this.el)) | |
this.b.removeChild(this.el); | |
}, 3000); | |
} | |
getScript(url, success) { | |
const script = document.createElement('script'); | |
script.src = url; | |
const head = document.getElementsByTagName('head')[0]; | |
let done = false; | |
script.onload = script.onreadystatechange = function () { | |
if (!done && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete')) { | |
done = true; | |
success(); | |
script.onload = script.onreadystatechange = null; | |
head.removeChild(script); | |
} | |
}; | |
head.appendChild(script); | |
} | |
loadLib(name, check, getVersion, uri) { | |
return new Promise((resolve, reject) => { | |
if (check()) { | |
const msg = `This page already using ${name} v${getVersion()}`; | |
this.showMsg( msg ); | |
resolve( msg ); | |
} | |
else { | |
this.getScript( uri, () => { | |
const ok = check(); | |
const msg = ok | |
? `This page is now has library ${name} with v${getVersion()}` | |
: `Sorry, but ${name} wasn\'t able to load`; | |
this.showMsg( msg ); | |
if (ok) | |
resolve(msg); | |
else | |
reject(msg); | |
}) | |
} | |
}) | |
} | |
async injectLibs(libs) { | |
return await Promise.all( | |
Object.entries(libs).map( ([name, {check, getVersion, uri}]) => | |
this.loadLib( name, check, getVersion, uri ) | |
) | |
); | |
} | |
loadLibs(libs) { | |
libs.forEach( (load) => load() ) | |
} | |
} | |
async function makeDownloadLinks() { | |
const loader = new LibLoader(); | |
const libs = { | |
jQuery: { | |
check: () => (typeof $ == 'function'), | |
getVersion: () => ( | |
`${jQuery.fn.jquery}${loader.otherjQuery ? ' and noConflict(). Use $jq(), not $().' : ''}` | |
), | |
uri: '//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js', | |
}, | |
loDash: { | |
check: () => (typeof window._ === 'function'), | |
getVersion: () => _.VERSION, | |
uri: '//cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js', | |
}, | |
FileSaver: { | |
check: () => (typeof saveAs === 'function'), | |
getVersion: () => ('✓'), | |
uri: '//cdn.rawgit.com/eligrey/FileSaver.js/c347c51f6e6e56221b2fa3d8c9b32e181f76355c/FileSaver.js', | |
}, | |
}; | |
await loader.injectLibs(libs); | |
const getFilename = (pdf) => { | |
const dateReg = new RegExp('\\d+\\/\\d+\\/\\d\\d\\d\\d'); | |
/* Get the texts from siblings*/ | |
const sibs = $(pdf).closest('tr').siblings(); | |
const texts = sibs.map( (i, s) => { | |
return $(s).text().trim().split('\n').slice(0,2).map( t => t.trim() ); | |
}); | |
const [allDates, allNames] = _.partition( texts.toArray(), t => t.match(dateReg) ); | |
/* Replace slashes with dashes for filename*/ | |
const dates = $.unique( _.map( allDates, (d) => d.replace( new RegExp('/', 'g'), '-' ) ) ).sort(); | |
/* Uniqueify names */ | |
const names = $.unique( allNames ).sort(); | |
/* Date range vs single date */ | |
if (dates.length > 1) | |
return `${dates[0]} to ${dates.pop()} - ${names.join(', ')}.pdf`; | |
return `${dates[0]} - ${names.join(', ')}.pdf`; | |
}; | |
/* Add "Download All" button */ | |
$("<button class='download-all modaBtn'>Download All</button>") | |
.appendTo( $('.eobs.title') ) | |
.click( (event) => { | |
_.each( allEobs, (entry) => { | |
/* Need to use raw XHR API instead of jQuery to set responseType to blob */ | |
const xhr = new XMLHttpRequest(); | |
xhr.open( "GET", entry.uri ); | |
xhr.responseType = "blob"; | |
xhr.onload = (e) => { | |
const blob = xhr.response; | |
saveAs(blob, entry.filename, true); | |
}; | |
xhr.send(); | |
}); | |
event.preventDefault(); | |
}); | |
/* Remove old download links */ | |
$('a.download').remove(); | |
/* Add new download links */ | |
let allEobs = $('a.pdf').map( (i, eob) => { | |
const filename = getFilename(eob); | |
const uri = $(eob).attr('href'); | |
$( `<a class='download' href='${uri}' download='${filename}'>Download</a>`).insertAfter( $(eob) ); | |
return { filename, uri }; | |
}); | |
/* Set download handler */ | |
$('a.download').click( (event) => { | |
const a = $(event.target); | |
const uri = a.attr('href'); | |
const filename = a.attr('download'); | |
/* Need to use raw XHR API instead of jQuery to set responseType to blob */ | |
const xhr = new XMLHttpRequest(); | |
xhr.open( "GET", uri ); | |
xhr.responseType = "blob"; | |
xhr.onload = (e) => { | |
const blob = xhr.response; | |
saveAs(blob, filename, true); | |
}; | |
xhr.send(); | |
event.preventDefault(); | |
}); | |
} | |
makeDownloadLinks(); | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment