PDF Finder and Downloader

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

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

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