Skip to content

Instantly share code, notes, and snippets.

@willasaywhat
Created February 13, 2025 03:48
Show Gist options
  • Save willasaywhat/31c60cccaa88ce1db7b9bd99b56aa517 to your computer and use it in GitHub Desktop.
Save willasaywhat/31c60cccaa88ce1db7b9bd99b56aa517 to your computer and use it in GitHub Desktop.
Quick JS console cut and paste export of TripCase PDFs
/**
DISCLAIMER: I wrote some code, and it was giving me blank PDFs off and on so I did the terrible, no good thing and asked ChatGPT to fix it.
Yeah, it didn't work the first try either. However, it did eventually make something that didn't completely suck which is better than me spending the night trying to fix it because Sabre decided to sunset their app with no export functionality.
If you're interested in the chat log, for proof I'm really that lazy, it's here: https://chatgpt.com/share/67ad6ad0-7ed0-800c-87a9-86f67267c3a0
USAGE NOTES: Login to tripcase.com, expand your 'Past Trips', then click 'See all remaining trips', then paste the below script into your JavaScript Console. After a little time for the AJAX to run, you'll get a download prompt for each itinerary as a numerical order PDF.
WHY: Sure, you could click a bunch of export links and save each PDF with more than one click but that wouldn't be nearly as easy as spamming left click on a Save button. Okay, yeah, it's very lazy. Still. It works.
RIP TripCase, we loved you. <3
**/
function addScript(url, callback) {
var script = document.createElement('script');
script.type = 'application/javascript';
script.src = url;
script.onload = callback;
document.head.appendChild(script);
}
addScript('https://cdnjs.cloudflare.com/ajax/libs/html2pdf.js/0.10.1/html2pdf.bundle.min.js', function() {
let links = document.querySelectorAll('h3 > a');
let files = Array.from(links).map(link => link.href + '/print');
async function generatePDF(file, index) {
return new Promise((resolve, reject) => {
$.ajax({
url: file,
cache: false,
success: function(responseHTML) {
// Remove onload="print()" if present
let modifiedHTML = responseHTML.replace(/onload\s*=\s*["']?\s*print\s*\(\s*\)\s*["']?/gi, '');
let iframe = document.createElement('iframe');
iframe.style.display = 'none';
document.body.appendChild(iframe);
iframe.onload = function() {
try {
let doc = iframe.contentDocument;
doc.open();
doc.write(modifiedHTML);
doc.close(); // Load modified content without print()
setTimeout(() => {
html2pdf().from(doc.body).save('file' + index + '.pdf').then(() => {
iframe.remove();
resolve();
});
}, 3000);
} catch (err) {
console.error("PDF Generation Error:", err);
reject(err);
}
};
// Ensure the iframe doesn't execute any preloaded script
iframe.src = "about:blank";
},
error: function(xhr, status, error) {
console.error("Failed to load:", file, error);
reject(error);
}
});
});
}
async function processFiles() {
for (let i = 0; i < files.length; i++) {
try {
await generatePDF(files[i], i);
} catch (error) {
console.warn("Skipping file due to error:", files[i]);
}
}
}
processFiles();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment