Skip to content

Instantly share code, notes, and snippets.

@AJV009
Last active July 20, 2023 08:17
Show Gist options
  • Select an option

  • Save AJV009/776159b1f6dcd0729e40689a739f410e to your computer and use it in GitHub Desktop.

Select an option

Save AJV009/776159b1f6dcd0729e40689a739f410e to your computer and use it in GitHub Desktop.
Download PDF.js PDF files from any site
function downloadPDFs() {
// Query all iframe elements
var iframes = document.querySelectorAll("iframe");
// Function to extract PDF URL
function getPDFUrl(src) {
// Check if it's a PDF.js URL
if (src.includes('pdfjs')) {
// Extract file parameter
var fileParam = new URL(src).searchParams.get('file');
if (fileParam) {
// Decode the URL and return
return decodeURIComponent(fileParam);
}
}
return null;
}
// Function to create a download link and click it
function downloadFile(url) {
var link = document.createElement('a');
link.href = url;
link.download = url.split('/').pop();
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
// Iterate through each iframe
iframes.forEach(function(iframe) {
// Get the PDF URL
var pdfUrl = getPDFUrl(iframe.src);
if (pdfUrl) {
// Download the PDF
downloadFile(pdfUrl);
}
});
}
// trigger the function
downloadPDFs()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment