Skip to content

Instantly share code, notes, and snippets.

@nafassait
Last active October 6, 2022 14:53
Show Gist options
  • Save nafassait/b3462e2c862614e4b9b01940199732b1 to your computer and use it in GitHub Desktop.
Save nafassait/b3462e2c862614e4b9b01940199732b1 to your computer and use it in GitHub Desktop.
Downloads All Tax Statements from ADP website
/*
This has been tested in Chrome Version 63.0.3239.132 (64-bit)
1. Log into ADP website using the URL: https://my.adp.com/static/redbox/login.html (Make sure you are NOT in Incognito mode)
2. Open Developer Tools console
3. Run the following code in the console:
*/
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://my.adp.com/v1_0/O/A/workerTaxStatements');
xhr.setRequestHeader("Access-Control-Allow-Origin", "*"); // CORS policy
xhr.onload = function() {
if (xhr.status === 200) {
var rawData = JSON.parse(xhr.responseText);
for (var index = rawData.workerTaxStatements.length - 1; index >= 0; --index) {
var entry = rawData.workerTaxStatements[index];
var url = "https://my.adp.com" + entry.statementImageUri.href.substring(3);
var a = document.createElement('a');
var trueIndex = (rawData.workerTaxStatements.length - index);
if (trueIndex < 10) {
trueIndex = "00" + trueIndex;
} else if (trueIndex < 100) {
trueIndex = "0" + trueIndex;
}
a.download = "taxstatement.no." + trueIndex + ".from." + entry.statementName + ".pdf";
a.href = url;
document.body.appendChild(a);
a.click();
delete a;
}
} else {
console.log('Request failed. Returned status of ' + xhr.status);
}
};
xhr.send();
@benlye
Copy link

benlye commented Oct 6, 2022

Here's an updated version, based on the code from this Gist.

Run this first to load the JSZip library:

var script = document.createElement('script');
script.src = 'https://cdnjs.cloudflare.com/ajax/libs/jszip/3.2.0/jszip.min.js';
document.head.appendChild(script); 

Then run this to synchronously download each file and create a zip file containing all of them:

var zip = new JSZip();
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://my.adp.com/v1_0/O/A/workerTaxStatements');
xhr.setRequestHeader("Access-Control-Allow-Origin", "*"); // CORS policy
xhr.onload = async function() {
    if (xhr.status === 200) {
        var rawData = JSON.parse(xhr.responseText);
        for (var index = rawData.workerTaxStatements.length - 1; index >= 0; --index) {
            var entry = rawData.workerTaxStatements[index];
            var date = entry.statementYear.year;
            var url = "https://my.adp.com" + entry.statementImageUri.href.substring(3);
            var a = document.createElement('a');

            var trueIndex = (rawData.workerTaxStatements.length - index);
            if (trueIndex < 10) {
                trueIndex = "00" + trueIndex;
            } else if (trueIndex < 100) {
                trueIndex = "0" + trueIndex;
            }

            var xhttp = new XMLHttpRequest();
            xhttp.onreadystatechange = function() {
                if(this.readyState == 4 && this.status == 200) {
                    zip.file( entry.form.code.replace('/', '-') + ".pdf", this.response)
                }
            };
            xhttp.open('GET', url, true);
            xhttp.responseType = "blob";
            xhttp.send();
            while (xhttp.readyState != 4) {
                console.log("Waiting for download " + trueIndex + " from " + date);
                await new Promise(r => setTimeout(r, 500));
            }
        }
    } else {
        console.log('Request failed.  Returned status of ' + xhr.status);
    }
    console.log('Zip archive complete')
};
xhr.send();

Finally, run this to download the zip file:

zip.generateAsync({type : 'base64'}).then(function (base64) {
    var a = document.createElement('a');
    a.download = 'taxdocs.zip'; 
    a.href = "data:application/zip;base64," + base64;
    document.body.appendChild(a);
    a.click();
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment