Find and add a download button for PDFs on webpages, including embedded PDFs in iframes.
当前为
// ==UserScript==
// @name PDF Finder and Downloader
// @version 1.2
// @description Find and add a download button for PDFs on webpages, including embedded PDFs in iframes.
// @license MIT
// @namespace pdf downloader
// @match *://*/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Function to check if a link ends with .pdf
function isPDFLink(link) {
return link.toLowerCase().endsWith('.pdf');
}
// Function to add a download button next to a PDF link
function addDownloadButton(element, pdfLink) {
const downloadButton = document.createElement('button');
downloadButton.innerText = 'PDF ⬇️';
downloadButton.style.backgroundColor = 'red';
downloadButton.style.color = 'white';
downloadButton.style.border = 'none';
downloadButton.style.padding = '5px 10px';
downloadButton.style.marginLeft = '5px';
downloadButton.style.cursor = 'pointer';
downloadButton.addEventListener('click', function(event) {
event.preventDefault();
window.open(pdfLink, '_blank');
});
// Insert the download button next to the PDF link or iframe
element.parentNode.insertBefore(downloadButton, element.nextSibling);
}
// Function to find PDF links on the page and add download buttons
function findAndAddPDFDownloadButtons() {
const elements = document.querySelectorAll('a, iframe');
elements.forEach(element => {
let pdfLink;
// Check if the element is an iframe
if (element.tagName.toLowerCase() === 'iframe') {
// Extract PDF link from iframe src attribute
pdfLink = element.src;
} else if (isPDFLink(element.href)) {
pdfLink = element.href;
}
// If a PDF link is found, add a download button
if (pdfLink) {
addDownloadButton(element, pdfLink);
}
});
}
// Run the function when the page is fully loaded
window.addEventListener('load', findAndAddPDFDownloadButtons);
})();