Last active
July 20, 2023 08:17
-
-
Save AJV009/776159b1f6dcd0729e40689a739f410e to your computer and use it in GitHub Desktop.
Download PDF.js PDF files from any site
This file contains hidden or 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
| 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