PDF Finder and Downloader

Find and add a download button for PDFs on webpages, including embedded PDFs in iframes.

当前为 2024-03-25 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name PDF Finder and Downloader
  3. // @version 1.2
  4. // @description Find and add a download button for PDFs on webpages, including embedded PDFs in iframes.
  5. // @author iamnobody
  6. // @license MIT
  7. // @match *://*/*
  8. // @grant none
  9. // @icon https://upload.wikimedia.org/wikipedia/commons/thumb/8/87/PDF_file_icon.svg/400px-PDF_file_icon.svg.png
  10. // @namespace pdf downloader
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. // Function to check if a link ends with .pdf
  17. function isPDFLink(link) {
  18. return link.toLowerCase().endsWith('.pdf');
  19. }
  20.  
  21. // Function to add a download button next to a PDF link
  22. function addDownloadButton(element, pdfLink) {
  23. const downloadButton = document.createElement('button');
  24. downloadButton.innerText = 'PDF ⬇️';
  25. downloadButton.style.backgroundColor = 'violet'; // Change color to violet
  26. downloadButton.style.color = 'white';
  27. downloadButton.style.border = 'none';
  28. downloadButton.style.padding = '5px 10px';
  29. downloadButton.style.marginLeft = '5px';
  30. downloadButton.style.cursor = 'pointer';
  31.  
  32. downloadButton.addEventListener('click', function(event) {
  33. event.preventDefault();
  34. window.open(pdfLink, '_blank');
  35. });
  36.  
  37. // Insert the download button next to the PDF link or iframe
  38. element.parentNode.insertBefore(downloadButton, element.nextSibling);
  39.  
  40. // Add circular button beside the PDF ⬇️ button
  41. addCircularButton(downloadButton);
  42. }
  43.  
  44. // Function to add a circular button beside the PDF ⬇️ button
  45. function addCircularButton(downloadButton) {
  46. const circularButton = document.createElement('button');
  47. circularButton.innerText = '-';
  48. circularButton.style.backgroundColor = 'green'; // Set color to green
  49. circularButton.style.color = 'white';
  50. circularButton.style.border = 'none';
  51. circularButton.style.borderRadius = '50%'; // Make it circular
  52. circularButton.style.width = '20px';
  53. circularButton.style.height = '20px';
  54. circularButton.style.marginLeft = '5px';
  55. circularButton.style.cursor = 'pointer';
  56.  
  57. // Add click event listener to exclude the website
  58. circularButton.addEventListener('click', function(event) {
  59. event.preventDefault();
  60. excludeWebsiteFromScript();
  61. });
  62.  
  63. // Insert the circular button next to the PDF ⬇️ button
  64. downloadButton.parentNode.insertBefore(circularButton, downloadButton.nextSibling);
  65. }
  66.  
  67. // Function to exclude the current website's domain from running the script
  68. function excludeWebsiteFromScript() {
  69. const currentDomain = window.location.hostname;
  70. let excludedDomains = JSON.parse(localStorage.getItem('excludedDomains')) || [];
  71. if (!excludedDomains.includes(currentDomain)) {
  72. excludedDomains.push(currentDomain);
  73. localStorage.setItem('excludedDomains', JSON.stringify(excludedDomains));
  74. }
  75. }
  76.  
  77. // Function to check if the current website's domain should be excluded from running the script
  78. function shouldExcludeWebsite() {
  79. const currentDomain = window.location.hostname;
  80. const excludedDomains = JSON.parse(localStorage.getItem('excludedDomains')) || [];
  81. return excludedDomains.includes(currentDomain);
  82. }
  83.  
  84. // Function to find PDF links on the page and add download buttons
  85. function findAndAddPDFDownloadButtons() {
  86. if (!shouldExcludeWebsite()) {
  87. const links = document.querySelectorAll('a, iframe');
  88. links.forEach(link => {
  89. let pdfLink;
  90. if (link.tagName.toLowerCase() === 'iframe') {
  91. const dataSource = link.getAttribute('data-source');
  92. if (dataSource) {
  93. pdfLink = getPDFLinkFromDataSource(dataSource);
  94. }
  95. } else if (isPDFLink(link.href)) {
  96. pdfLink = link.href;
  97. }
  98. if (pdfLink) {
  99. addDownloadButton(link, pdfLink);
  100. }
  101. });
  102. }
  103. }
  104.  
  105. // Function to extract PDF link from data-source attribute
  106. function getPDFLinkFromDataSource(dataSource) {
  107. const match = dataSource.match(/file=([^&]+)/);
  108. if (match && match[1]) {
  109. return decodeURIComponent(match[1]);
  110. }
  111. return null;
  112. }
  113.  
  114. // Run the function when the page is fully loaded
  115. window.addEventListener('load', findAndAddPDFDownloadButtons);
  116.  
  117. })();