Greasy Fork

Greasy Fork is available in English.

国家中小学智慧教育平台教材PDF链接工具

教材预览页面添加了PDF按钮,可免登录查看或下载电子课本

当前为 2023-06-19 提交的版本,查看 最新版本

// ==UserScript==
// @name         国家中小学智慧教育平台教材PDF链接工具
// @namespace    http://tampermonkey.net
// @version      1.1
// @description  教材预览页面添加了PDF按钮,可免登录查看或下载电子课本
// @match        *://basic.smartedu.cn/*
// @match        *://www.zxx.edu.cn/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const pattern = /contentId=([a-zA-Z0-9_-]+)/; // 匹配 contentId 的正则表达式
    const contentId = window.location.href.match(pattern)[1]; // 获取 contentId 的数值
    let link = `https://r3-ndr.ykt.cbern.com.cn/edu_product/esp/assets_document/${contentId}.pkg/pdf.pdf`;
    // window.location.href = link; // 直接重定向到该网址,经测试可用

    const maxTimeToCheck = 5000; // 最多检查 5000 毫秒,即 5 秒
    let elapsedTime = 0;

    function checkConfirmButtons() {
        const confirmBtns = document.querySelector('.fish-modal-confirm-btns');
        if (confirmBtns) {
            confirmBtns.insertAdjacentHTML('beforeend', `<a href="${link}" class="fish-btn">查看PDF</a>`);
            clearInterval(intervalId1); // 清除定时器
        } else {
            elapsedTime += 100;
            if (elapsedTime >= maxTimeToCheck) {
                clearInterval(intervalId1); // 清除定时器,停止检查
            }
        }
    }
    const intervalId1 = setInterval(checkConfirmButtons, 100); // 每隔 100 毫秒检查一次 fish-modal-confirm-btns 元素是否存在

    function checkIndexModule() {
        const container = document.querySelector('.index-module_extra_tUQog'); // 找到要添加按钮的容器元素
        if (container) {
            const a = document.createElement('a');
            a.innerHTML = '📓 查看PDF'; // 文本内容
            a.display = 'flex'; // 弹性容器布局
            a.style.alignItems = 'center'; // 居中对齐
            a.style.marginLeft = '24px'; // 左边距(“点赞数”左右边距各 10,“建议”左边距 14,故按钮间距为 24)
            a.style.fontSize = '14px'; // 字体大小
            a.style.cursor = 'pointer'; // 指针变手型
            a.style.color = '#888'; // 文本颜色
            a.href = link; // 链接
            // a.target = '_blank'; // 新标签页打开
            container.appendChild(a); // 将按钮添加到网页中
            a.addEventListener('mouseover', function() {
                this.innerHTML = '📘 查看PDF';
                this.style.color = '#226dec'; // 鼠标移入时修改元素的样式
            });
            a.addEventListener('mouseout', function() {
                this.innerHTML = '📓 查看PDF';
                this.style.color = '#888'; // 鼠标移出时恢复原来的样式
            });
            a.addEventListener('mousedown', function() {
                this.innerHTML = '📕 查看PDF';
                this.style.color = '#e82020'; // 鼠标按下时修改元素的样式
            });
            // a.addEventListener('mouseup', function() {
                // this.innerHTML = '📓 查看PDF';
                // this.style.color = '#888'; // 鼠标抬起时修改元素的样式
            // });
            clearInterval(intervalId2); // 清除定时器
        } else {
            elapsedTime += 100;
            if (elapsedTime >= maxTimeToCheck) {
                clearInterval(intervalId2); // 清除定时器,停止检查
            }
        }
    }
    const intervalId2 = setInterval(checkIndexModule, 100); // 每隔 100 毫秒检查一次 index-module_extra_tUQog 元素是否存在
})();