Greasy Fork

Greasy Fork is available in English.

高考直通车PDF下载

高考直通车添加直接下载PDF的按钮

当前为 2024-04-30 提交的版本,查看 最新版本

// ==UserScript==
// @name         高考直通车PDF下载
// @namespace    http://tampermonkey.net/
// @version      0.1.2
// @description  高考直通车添加直接下载PDF的按钮
// @author       braveteen
// @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';
    var styles = `
  background-color: DodgerBlue;
  border: none;
  color: white;
  padding: 10px 20px;
  cursor: pointer;
  font-size: 18px;
`;
    var url = document.getElementsByTagName("script")[5].textContent.match(/auto_pdf_url\s*:\s*['"]([^'"]+)['"]/)[1];
    // console.log(url);
    url = url.replace("http","https");
    console.log(url);


    var link = document.createElement("button");
    link.innerHTML = "下载PDF";
    link.style.cssText = styles
    var vipDiv = document.getElementsByClassName("vip_icon")[0]
    vipDiv.parentNode.replaceChild(link, vipDiv);
    link.onclick = () =>{
        if(url){
            window.URL = window.URL || window.webkitURL;

            var xhr = new XMLHttpRequest(),
                a = document.createElement('a'), file;

            xhr.open('GET', url, true);
            xhr.responseType = 'blob';
            xhr.onload = function () {
                file = new Blob([xhr.response], { type : 'application/pdf' });
                a.href = window.URL.createObjectURL(file);
                a.download = document.title.concat(".pdf");
                a.click();
            };
            xhr.send();
            alert("成功开始下载");
        }else{
            alert("下载失败 如需帮助 请联系脚本作者");
        }
    }
})();