国家中小学智慧教育平台下载教材pdf

打开教材页重定向到pdf文件页面

// ==UserScript==
// @name         国家中小学智慧教育平台下载教材pdf
// @namespace    http://tampermonkey.net/
// @version      1.21
// @description  打开教材页重定向到pdf文件页面
// @author       Teng
// @match        https://www.zxx.edu.cn/tchMaterial/*
// @icon         https://www.zxx.edu.cn/favicon.ico
// @license MIT

// ==/UserScript==

(function() {
    'use strict';
    var window_width = window.innerWidth;
    var download_btn = document.createElement('input')
    download_btn.setAttribute('style',`position: fixed; z-index: 10000; left:${window.innerWidth/2}px;top:${window.innerHeight/2}px;height:36px;width:76px; border: 1px solid;`)
    download_btn.setAttribute('type','button')
    download_btn.setAttribute('id','download_btn')
    download_btn.setAttribute('value','下载pdf')
    download_btn.addEventListener('click',()=>{
        if(performance.getEntriesByType('resource').filter(entry =>{return /.*pdf\.pdf/.test(entry.name);})[0]){

        var pdf_url = performance.getEntriesByType('resource').filter(entry =>{return /.*pdf\.pdf/.test(entry.name);})[0].name;
        var pdf_name = document.querySelector('h3').textContent
        console.log(pdf_url);
        let xhr = new XMLHttpRequest();
        xhr.open('GET', pdf_url, true);
        xhr.responseType = 'blob';
        xhr.send()
        xhr.onreadystatechange = function () {
        if (xhr.readyState === 4 && xhr.status === 200) {
            const aTag = document.createElement('a')
            aTag.href = URL.createObjectURL(this.response)
            aTag.download = `${pdf_name}.pdf`;
            aTag.click()
        }
    }
        window.alert('后台下载。。。')}else{window.alert('网页加载中,请等待2秒再试');}
    })
    document.body.append(download_btn)

})();