Skip to content

Instantly share code, notes, and snippets.

@azagniotov
Last active September 7, 2024 18:24
Show Gist options
  • Save azagniotov/210c31540712c10206484d5297616842 to your computer and use it in GitHub Desktop.
Save azagniotov/210c31540712c10206484d5297616842 to your computer and use it in GitHub Desktop.
Downloads all pay-slips from ADP website
/*
This has been tested in Chrome v55.0.2883.95 (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/payStatements?adjustments=yes&numberoflastpaydates=300');
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.payStatements.length - 1; index >= 0; --index) {
var entry = rawData.payStatements[index];
var url = "https://my.adp.com" + entry.statementImageUri.href.substring(3);
var a = document.createElement('a');
var trueIndex = (rawData.payStatements.length - index);
if (trueIndex < 10) {
trueIndex = "00" + trueIndex;
} else if (trueIndex < 100) {
trueIndex = "0" + trueIndex;
}
a.download = "payslip.no." + trueIndex + ".from." + entry.payDate + ".pdf";
a.href = url;
document.body.appendChild(a);
a.click();
delete a;
}
} else {
console.log('Request failed. Returned status of ' + xhr.status);
}
};
xhr.send();
@dwt12777
Copy link

Thanks guys, this worked great (used Aatoz's update)!

@b-steel
Copy link

b-steel commented Nov 19, 2020

Just used Aatoz's version on Firefox 82.0.3 (64bit) and it worked like a charm.

Many thanks!

@raugustin93
Copy link

Thank you so much!

Still working!

@iampranavsethi
Copy link

iampranavsethi commented Feb 9, 2021

I modified the code that @LukeHandle posted to create the zip. I append the base64 zip contents to a href tag and then trigger the click.
Saves the files with only one download / save dialog.

Load in 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 block to synchronously download every slip and then download the payslips:

var zip = new JSZip();
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://my.adp.com/v1_0/O/A/payStatements?adjustments=yes&numberoflastpaydates=300');
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.payStatements.length - 1; index >= 0; --index) {
            var entry = rawData.payStatements[index];
            var date = entry.payDate
            var year = entry.payDate.split("-")[0]
            var url = "https://my.adp.com" + entry.statementImageUri.href.substring(3);

            var trueIndex = (rawData.payStatements.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.folder(year).file(date + ".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);
    }
};
xhr.send();

After the downloads have finished, paste this code in the console later -

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

@theSuda
Copy link

theSuda commented Feb 19, 2021

Just used this in Firefox 85.0.2.
Great script.
Had to change setting to Always Automatically Download files in Firefox settings because it just prompted a lot of Download File windows.
Please add this to the instructions.

@benblasco
Copy link

Just used this in Firefox 85.0.2.
Great script.
Had to change setting to Always Automatically Download files in Firefox settings because it just prompted a lot of Download File windows.
Please add this to the instructions.

I wasn't able to find this setting in Firefox, and the pasted code from @iampranavthesi generated the following errors:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://my.adp.com/v1_0/O/A/payStatements?adjustments=yes&numberoflastpaydates=300. (Reason: header ‘access-control-allow-origin’ is not allowed according to header ‘Access-Control-Allow-Headers’ from CORS preflight response).

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://my.adp.com/v1_0/O/A/payStatements?adjustments=yes&numberoflastpaydates=300. (Reason: CORS request did not succeed).

Any ideas?

@iampranavsethi
Copy link

Just used this in Firefox 85.0.2.
Great script.
Had to change setting to Always Automatically Download files in Firefox settings because it just prompted a lot of Download File windows.
Please add this to the instructions.

I wasn't able to find this setting in Firefox, and the pasted code from @iampranavthesi generated the following errors:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://my.adp.com/v1_0/O/A/payStatements?adjustments=yes&numberoflastpaydates=300. (Reason: header ‘access-control-allow-origin’ is not allowed according to header ‘Access-Control-Allow-Headers’ from CORS preflight response).

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://my.adp.com/v1_0/O/A/payStatements?adjustments=yes&numberoflastpaydates=300. (Reason: CORS request did not succeed).

Any ideas?

@eraser215 were you already logged in to my.adp.com?
You can try these steps if it helps you out -

  1. After login to my.adp.com open this link https://my.adp.com/v1_0/O/A/payStatements?adjustments=yes&numberoflastpaydates=300 in a new tab. (This will be a JSON response body)
    (You will not get CORS issue if you try on this link)
  2. Open Dev tools -> Console and then paste the first code block to load up the JSZip library
  3. Then paste the second code block to start the download of every payslip. (It is going to take a while depending on how many are there to download)
  4. Lastly, paste the last code block to trigger the download of the zip file.

Paste the 3 code blocks one at a time and not as one script together.

Tested in Chrome v89 can't say about Firefox but should be similar.

@r0guenj
Copy link

r0guenj commented Apr 23, 2021

This was helpful and worked as described after using @iampranavsethi modified instructions.

@Krthkyns
Copy link

Updated Script as of this date(Tested in chrome Version 94.0.4606.81 (Official Build) (64-bit))

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

var zip = new JSZip();
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://my.adp.com/v1_0/O/A/payStatements?adjustments=yes&numberoflastpaydates=300');
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.payStatements.length - 1; index >= 0; --index) {
var entry = rawData.payStatements[index];
var date = entry.payDate
var year = entry.payDate.split("-")[0]
var url = "https://my.adp.com/myadp_prefix/" + entry.statementImageUri.href;

        var trueIndex = (rawData.payStatements.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.folder(year).file(date + ".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);
}

};
xhr.send();

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

@cwlls
Copy link

cwlls commented Jan 6, 2022

This worked very nicely, thanks!

@pfoneill
Copy link

Works great after adding the sleep step - thank you!

@rcrocker
Copy link

rcrocker commented Jan 4, 2023

Thanks for this. I found that the Zip version didn't quite work if you had more than one paystub on the same day, so I made a small modification to the callback to append an index to the filename and now that works.

var zip = new JSZip();
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://my.adp.com/v1_0/O/A/payStatements?adjustments=yes&numberoflastpaydates=300');
xhr.setRequestHeader("Access-Control-Allow-Origin", "*"); // CORS policy
xhr.onload = async function() {
    if (xhr.status === 200) {
        var rawData = JSON.parse(xhr.responseText);
        var lastPaydate = ""
        var consecutiveEntries = 0
        for (var index = rawData.payStatements.length - 1; index >= 0; --index) {
            var entry = rawData.payStatements[index];
            var date = entry.payDate
            // Handle multiple paystubs on the same day
            var dateSuffix = ""
            if (date === lastPaydate) {
                consecutiveEntries++
                dateSuffix = `-${consecutiveEntries}`
            } else {
                lastPaydate = date
                consecutiveEntries = 0
            }
            var year = entry.payDate.split("-")[0]
            var url = "https://my.adp.com" + entry.statementImageUri.href.substring(3);

            var trueIndex = (rawData.payStatements.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.folder(year).file(date + dateSuffix + ".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);
    }
};
xhr.send();

@FrankTaylorLieder
Copy link

Hi, I've just used the original script in Chrome and as before it stops after 10 downloads. I added a simple fix to sleep between downloads and it then was able to download all 159 of my payslips without any problems. (Note I also added some logging so I can see progress.)

Here is the updated code:

var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://my.adp.com/v1_0/O/A/payStatements?adjustments=yes&numberoflastpaydates=300');
xhr.setRequestHeader("Access-Control-Allow-Origin", "*"); // CORS policy
xhr.onload = async function() {
    if (xhr.status === 200) {
        var rawData = JSON.parse(xhr.responseText);
        console.log('Number of items:', rawData.payStatements.length);
        for (var index = rawData.payStatements.length - 1; index >= 0; --index) {
            var entry = rawData.payStatements[index];
            var url = "https://my.adp.com" + entry.statementImageUri.href.substring(3);
            console.log("Downloading:", url);
            var a = document.createElement('a');

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

            a.download = "payslip.no." + trueIndex + ".from." + entry.payDate + ".pdf";
            a.href = url;
            document.body.appendChild(a);
            a.click();
            delete a;

            await new Promise(r => setTimeout(r, 1000));
        }
    } else {
        console.log('Request failed.  Returned status of ' + xhr.status);
    }
};
xhr.send();

@corymayer
Copy link

Updated version for 2024:

You need to get your OID by inspecting web requests, and replace YOUR_OID_HERE. I tried to make it generic by calling this API: https://my.adp.com/myadp_prefix/myadpapi/core/v1/version which returns the OID, but the API has some special CORs protection I couldn't figure out.

function sleep(ms) {
   return new Promise(resolve => setTimeout(resolve, ms));
}

var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://my.adp.com/myadp_prefix/payroll/v1/workers/YOUR_OID_HERE/pay-statements?adjustments=yes&numberoflastpaydates=300');
xhr.setRequestHeader("Access-Control-Allow-Origin", "*"); // CORS policy
xhr.onload = async function() {
    if (xhr.status === 200) {
        var rawData = JSON.parse(xhr.responseText);
        var lastPaydate = ""
        var consecutiveEntries = 0
        for (var index = rawData.payStatements.length - 1; index >= 0; --index) {
            var entry = rawData.payStatements[index];
            var date = entry.payDate
            // Handle multiple paystubs on the same day
            var dateSuffix = ""
            if (date === lastPaydate) {
                consecutiveEntries++
                dateSuffix = `-${consecutiveEntries}`
            } else {
                lastPaydate = date
                consecutiveEntries = 0
            }
            var year = entry.payDate.split("-")[0]
            var url = "https://my.adp.com/myadp_prefix" + entry.statementImageUri.href + "?rolecode=employee";

            var a = document.createElement('a');

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

            a.download = "payslip.no." + trueIndex + ".from." + entry.payDate + ".pdf";
            a.href = url;
            document.body.appendChild(a);

            await sleep(500);
            a.click();
            delete a;
        }
    } else {
        console.log('Request failed.  Returned status of ' + xhr.status);
    }
};
xhr.send();

@jewelhuq
Copy link

jewelhuq commented Sep 5, 2024

Is there any full code can be run from the backend in 2024

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