Greasy Fork

Greasy Fork is available in English.

高考直通车PDF下载优化

高考直通车添加直接下载PDF的按钮(修复提取链接和按钮显示问题)

当前为 2024-12-29 提交的版本,查看 最新版本

// ==UserScript==
// @name         高考直通车PDF下载优化
// @namespace    http://tampermonkey.net/
// @version      0.2.0
// @description  高考直通车添加直接下载PDF的按钮(修复提取链接和按钮显示问题)
// @author       braveteen & SeaSurgeX
// @match        https://app.gaokaozhitongche.com/newsexam/h/*
// @icon         https://app.gaokaozhitongche.com/css/sharewb/img/head-img.png
// @license      MIT
// @compatible   chrome
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    // 定义按钮样式
    const styles = `
        background-color: DodgerBlue;
        border: none;
        color: white;
        padding: 10px 20px;
        cursor: pointer;
        font-size: 18px;
        margin-top: 10px;
    `;

    try {
        // 尝试从页面脚本中提取 `auto_pdf_url`
        const scriptTags = document.getElementsByTagName('script');
        let pdfUrl = null;

        for (let i = 0; i < scriptTags.length; i++) {
            const scriptContent = scriptTags[i].textContent;
            const match = scriptContent.match(/auto_pdf_url\s*:\s*['"]([^'"]+)['"]/);
            if (match) {
                pdfUrl = match[1];
                break;
            }
        }

        if (!pdfUrl) {
            console.error("未找到PDF下载链接");
            alert("未能提取PDF下载链接。如需帮助,请联系脚本作者。");
            return;
        }

        // 确保链接为HTTPS
        pdfUrl = pdfUrl.replace(/^http:/, 'https:');

        // 创建按钮
        const downloadButton = document.createElement('button');
        downloadButton.innerHTML = "下载PDF";
        downloadButton.style.cssText = styles;

        // 按钮点击事件
        downloadButton.onclick = () => {
            if (pdfUrl) {
                // 下载逻辑
                const xhr = new XMLHttpRequest();
                xhr.open('GET', pdfUrl, true);
                xhr.responseType = 'blob';

                xhr.onload = function () {
                    if (xhr.status === 200) {
                        const blob = new Blob([xhr.response], { type: 'application/pdf' });
                        const downloadLink = document.createElement('a');
                        downloadLink.href = window.URL.createObjectURL(blob);
                        downloadLink.download = document.title.concat(".pdf");
                        downloadLink.click();
                        alert("成功开始下载!");
                    } else {
                        alert("下载失败,请检查网络连接或联系脚本作者。");
                    }
                };

                xhr.onerror = function () {
                    alert("下载请求失败,请重试。");
                };

                xhr.send();
            } else {
                alert("无效的PDF链接。如需帮助,请联系脚本作者。");
            }
        };

        // 插入按钮到页面
        const vipDiv = document.querySelector(".vip_icon");
        if (vipDiv && vipDiv.parentNode) {
            vipDiv.parentNode.replaceChild(downloadButton, vipDiv);
        } else {
            // 如果找不到 `vip_icon` 元素,直接插入到页面顶部
            document.body.insertBefore(downloadButton, document.body.firstChild);
        }
    } catch (e) {
        console.error("脚本运行错误:", e);
        alert("脚本运行时出现错误。如需帮助,请联系脚本作者。");
    }
})();