PDF Viewer Embedder

Embeds PDFs directly in the page instead of downloading, with fallback and cleanup

目前为 2025-04-29 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name PDF Viewer Embedder
  3. // @namespace *
  4. // @version 1.6
  5. // @author zinchaiku
  6. // @description Embeds PDFs directly in the page instead of downloading, with fallback and cleanup
  7. // @match *://*/*
  8. // @grant none
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. (function () {
  13. 'use strict';
  14.  
  15. let currentBlobUrl = null;
  16.  
  17. function showPDF(srcUrl, isBlob = false) {
  18. const existing = document.getElementById('tampermonkey-pdf-viewer');
  19. if (existing) existing.remove();
  20.  
  21. const overlay = document.createElement('div');
  22. overlay.id = 'tampermonkey-pdf-viewer';
  23. overlay.style.position = 'fixed';
  24. overlay.style.top = 0;
  25. overlay.style.left = 0;
  26. overlay.style.width = '100vw';
  27. overlay.style.height = '100vh';
  28. overlay.style.background = 'rgba(0,0,0,0.8)';
  29. overlay.style.zIndex = 99999;
  30. overlay.style.display = 'flex';
  31. overlay.style.alignItems = 'center';
  32. overlay.style.justifyContent = 'center';
  33.  
  34. const iframe = document.createElement('iframe');
  35. iframe.src = srcUrl;
  36. iframe.style.width = '80vw';
  37. iframe.style.height = '90vh';
  38. iframe.style.border = 'none';
  39. iframe.style.background = '#fff';
  40. overlay.appendChild(iframe);
  41.  
  42. // Fallback if iframe fails to load (e.g. X-Frame-Options)
  43. iframe.onerror = () => {
  44. overlay.remove();
  45. if (isBlob && currentBlobUrl) {
  46. URL.revokeObjectURL(currentBlobUrl);
  47. currentBlobUrl = null;
  48. }
  49. window.open(srcUrl, '_blank');
  50. };
  51.  
  52. const closeBtn = document.createElement('button');
  53. closeBtn.textContent = 'Close PDF';
  54. closeBtn.style.position = 'absolute';
  55. closeBtn.style.top = '20px';
  56. closeBtn.style.right = '40px';
  57. closeBtn.style.zIndex = 100000;
  58. closeBtn.style.padding = '2px 6px';
  59. closeBtn.style.fontSize = '1.2em';
  60. closeBtn.onclick = () => {
  61. overlay.remove();
  62. if (isBlob && currentBlobUrl) {
  63. URL.revokeObjectURL(currentBlobUrl);
  64. currentBlobUrl = null;
  65. }
  66. };
  67. overlay.appendChild(closeBtn);
  68.  
  69. document.body.appendChild(overlay);
  70.  
  71. // Track for cleanup
  72. if (isBlob) {
  73. currentBlobUrl = srcUrl;
  74. } else {
  75. currentBlobUrl = null;
  76. }
  77. }
  78.  
  79. function isPDFLink(href) {
  80. try {
  81. const url = new URL(href, window.location.href);
  82. return url.pathname.toLowerCase().endsWith('.pdf');
  83. } catch {
  84. return false;
  85. }
  86. }
  87.  
  88. function attachPDFInterceptors() {
  89. document.querySelectorAll('a[href]').forEach(link => {
  90. const href = link.getAttribute('href');
  91. if (!href || link.dataset.tmPdfBound) return;
  92.  
  93. const isWrapper = href.includes('_download.html');
  94. const isDirectPDF = isPDFLink(href);
  95.  
  96. if (isWrapper || isDirectPDF) {
  97. link.dataset.tmPdfBound = "1";
  98.  
  99. link.addEventListener('click', function (e) {
  100. e.preventDefault();
  101.  
  102. if (isDirectPDF) {
  103. showPDF(link.href);
  104. } else {
  105. fetch(link.href, { credentials: 'include' })
  106. .then(res => {
  107. const contentType = res.headers.get('Content-Type') || '';
  108. if (contentType.includes('pdf')) {
  109. return res.blob().then(blob => {
  110. const blobUrl = URL.createObjectURL(blob);
  111. showPDF(blobUrl, true);
  112. });
  113. } else {
  114. window.location.href = link.href;
  115. }
  116. })
  117. .catch(err => {
  118. alert("Could not open PDF: " + err);
  119. window.location.href = link.href;
  120. });
  121. }
  122. });
  123. }
  124. });
  125. }
  126.  
  127. window.addEventListener('keydown', (e) => {
  128. if (e.key === 'Escape') {
  129. const viewer = document.getElementById('tampermonkey-pdf-viewer');
  130. if (viewer) {
  131. viewer.remove();
  132. if (currentBlobUrl) {
  133. URL.revokeObjectURL(currentBlobUrl);
  134. currentBlobUrl = null;
  135. }
  136. }
  137. }
  138. });
  139.  
  140. attachPDFInterceptors();
  141. const observer = new MutationObserver(attachPDFInterceptors);
  142. observer.observe(document.body, { childList: true, subtree: true });
  143. })();