您需要先安装一个扩展,例如 篡改猴、Greasemonkey 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 Userscripts ,之后才能安装此脚本。
您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。
您需要先安装用户脚本管理器扩展后才能安装此脚本。
嗅探全国团体标准信息平台的PDF链接,添加下载按钮
// ==UserScript== // @name 全国团体标准PDF下载助手 // @namespace http://tampermonkey.net/ // @version 2.1 // @description 嗅探全国团体标准信息平台的PDF链接,添加下载按钮 // @author Dflying & Claude // @match https://www.ttbz.org.cn/Pdfs/Index/* // @grant none // @run-at document-idle // @license MIT // ==/UserScript== (function() { 'use strict'; // 创建样式 const style = document.createElement('style'); style.textContent = ` .pdf-download-container { position: fixed; /* 固定位置,不随滚动变化 */ z-index: 10000; display: flex; flex-direction: column; gap: 10px; right: 50px; top: 50px; } .pdf-download-btn { background-color: #4CAF50; color: white; padding: 10px 14px; border: none; border-radius: 4px; cursor: pointer; font-size: 14px; font-weight: bold; box-shadow: 0 2px 5px rgba(0,0,0,0.3); transition: all 0.2s; white-space: nowrap; } .pdf-download-btn:hover { background-color: #45a049; transform: translateY(-1px); box-shadow: 0 3px 7px rgba(0,0,0,0.3); } .download-status { position: fixed; bottom: 20px; right: 20px; background-color: rgba(0,0,0,0.8); color: white; padding: 12px 16px; border-radius: 4px; z-index: 10001; display: none; font-weight: bold; } `; document.head.appendChild(style); // 创建UI元素 const btnContainer = document.createElement('div'); btnContainer.className = 'pdf-download-container'; document.body.appendChild(btnContainer); const statusEl = document.createElement('div'); statusEl.className = 'download-status'; document.body.appendChild(statusEl); // 显示状态消息 function showStatus(message, isError = false) { statusEl.textContent = message; statusEl.style.backgroundColor = isError ? 'rgba(220,53,69,0.9)' : 'rgba(0,0,0,0.8)'; statusEl.style.display = 'block'; setTimeout(() => statusEl.style.display = 'none', 3000); } // 初始化函数 function init() { console.log("**PDF下载助手已启动**"); findPdfLinks(); } // 查找PDF下载链接 function findPdfLinks() { let pdfUrl = null; // 方法1: 从iframe src提取 const myIframe = document.getElementById('myiframe'); if (myIframe) { const src = myIframe.src || myIframe.getAttribute('src'); if (src) { const match = src.match(/file=(\/Home\/PdfFileStreamGet\/[^&]+)/); if (match && match[1]) { pdfUrl = `https://www.ttbz.org.cn${match[1]}`; console.log(`**从iframe src提取到PDF链接**:`, pdfUrl); } } } // 方法2: 从URL参数构造 if (!pdfUrl) { const currentUrl = window.location.href; const urlMatch = currentUrl.match(/\/Pdfs\/Index\/\?ftype=([^&]+)&pms=(\d+)/); if (urlMatch && urlMatch[1] && urlMatch[2]) { const ftype = urlMatch[1]; const pms = urlMatch[2]; const encodedParam = btoa(`${ftype},${pms}`); pdfUrl = `https://www.ttbz.org.cn/Home/PdfFileStreamGet/${encodedParam}`; console.log(`**从URL参数构造的PDF链接**:`, pdfUrl); } } // 添加下载按钮 const button = document.createElement('button'); button.className = 'pdf-download-btn'; button.textContent = '下载PDF文件'; button.addEventListener('click', () => { if (pdfUrl) { downloadPDF(pdfUrl); } else { showStatus("无法找到PDF链接", true); } }); btnContainer.appendChild(button); } // 下载PDF并处理文件名 function downloadPDF(url) { showStatus("正在下载PDF文件..."); fetch(url) .then(response => { const responseClone = response.clone(); // 尝试获取文件名 let filename = ""; const contentDisposition = response.headers.get('Content-Disposition'); if (contentDisposition) { const filenameMatch = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/.exec(contentDisposition); if (filenameMatch && filenameMatch[1]) { filename = filenameMatch[1].replace(/['"]/g, ''); } } // 从URL中提取文件名 if (!filename) { const urlParts = url.split('/'); filename = urlParts[urlParts.length - 1].split('_rnd=')[0]; if (!filename.toLowerCase().endsWith('.pdf')) { filename += '.pdf'; } } return responseClone.blob().then(blob => ({ blob, filename })); }) .then(({ blob, filename }) => { console.log(`**下载文件名**: ${filename}`); // 创建下载链接 const downloadUrl = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = downloadUrl; a.download = filename; document.body.appendChild(a); a.click(); document.body.removeChild(a); URL.revokeObjectURL(downloadUrl); showStatus("PDF文件下载成功!"); }) .catch(error => { console.error("**下载失败**:", error); showStatus("下载失败,请查看控制台获取详细信息", true); }); } // 启动脚本 init(); })();