Greasy Fork

Greasy Fork is available in English.

组卷网-组卷中心一键清理

[2024.11.30]删除影响阅读的不必要元素,微调样式以方便观看,为一些题型预留空白,新增了打印按钮

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

// ==UserScript==
// @name         组卷网-组卷中心一键清理
// @namespace    http://tampermonkey.net/
// @version      0.23
// @description  [2024.11.30]删除影响阅读的不必要元素,微调样式以方便观看,为一些题型预留空白,新增了打印按钮
// @author       TheOneAdonis
// @match        https://zujuan.xkw.com/zujuan
// @grant        none
// @license      MIT
// ==/UserScript==
(function() {
    'use strict';

    let globalSpecialQuestions = {};

    // 记录需要留空的题目
    const recordSpecialQuestions = () => {
        // 定义需要记录的题目类型
        const specialTypes = [
            '简答题',
            '材料题',
            '综合题',
            '解答题',
            '辨析题',
            '情境探究题',
            '分析说明题'
        ];

        const specialQuestions = {};

        // 查找所有 ques-type 元素
        const quesTypeElements = document.querySelectorAll('.ques-type');

        quesTypeElements.forEach(element => {
            const typeName = element.getAttribute('name');

            // 检查是否是需要留空的题目类型
            if (specialTypes.includes(typeName)) {
                // 在 specialQuestions 中创建对应类型的数组
                if (!specialQuestions[typeName]) {
                    specialQuestions[typeName] = [];
                }

                // 查找该题目类型下的所有题目
                const questionBody = element.querySelector('.questype-body');
                if (questionBody) {
                    const questions = questionBody.querySelectorAll('.ques-item');

                    questions.forEach((question, index) => {
                        // 查找题号
                        const questionNumberElement = question.querySelector('.quesindex');
                        const questionNumber = questionNumberElement
                            ? questionNumberElement.textContent.trim()
                            : `未知题号 ${index + 1}`;

                        // 记录题目信息,包括题号
                        specialQuestions[typeName].push({
                            questionNumber: questionNumber,
                            index: index + 1,
                            element: question,
                            text: question.textContent.trim()
                        });
                    });
                }
            }
        });

        // 将记录保存到全局变量
        globalSpecialQuestions = specialQuestions;

        console.log('特殊题目记录:', specialQuestions);

        return specialQuestions;
    };

    // 页面加载完成后立即记录
    window.addEventListener('load', recordSpecialQuestions);

    // 创建按钮
    const button = document.createElement('button');
    button.textContent = '清理';
    button.style.position = 'fixed';
    button.style.right = '10px';
    button.style.top = '10px';
    button.style.zIndex = '1000';
    button.style.padding = '10px';
    button.style.backgroundColor = '#2877ff';
    button.style.color = 'white';
    button.style.border = 'none';
    button.style.borderRadius = '5px';
    button.style.cursor = 'pointer';

    // 将按钮添加到body
    document.body.appendChild(button);

    // 按钮点击事件
    button.addEventListener('click', function() {
        // 列出需要删除的元素选择器
        const elementsToDelete = [
            'body > header',
            'body > div.bread-nav',
            'body > div.fiexd-nav',
            'body > main > aside',
            'body > main > article > div.seal-line',
            'body > main > article > div.paper-main > div.paper-head',
            'body > main > article > div.paper-main > div.paper-body > div:nth-child(1) > div.part-head',
            '#part-head-box2',
            'body > main > article > div.paper-main > div.deleted-box',
            'body > div.footer'
        ];

        // 删除指定的元素
        elementsToDelete.forEach(selector => {
            const element = document.querySelector(selector);
            if (element) {
                element.remove();
            }
        });

        // 删除所有class="questype-head"的元素
        const questypeHeads = document.querySelectorAll('.questype-head');
        questypeHeads.forEach(element => {
            element.remove();
        });

        // 为部分题型添加底部空白
        Object.values(globalSpecialQuestions).forEach(typeQuestions => {
            typeQuestions.forEach(question => {
                question.element.style.paddingBottom = '120px';
            });
        });

        // 设置所有元素的字体大小为14px
        function setFontSize() {
            const allElements = document.querySelectorAll('*');
            allElements.forEach(el => {
                el.style.fontSize = '14px';
            });
        }
        setFontSize();

        // 将class="paper-cnt clearfix"的max-width设置为100%
        function setMaxWidth() {
            const paperCnt = document.querySelector('.paper-cnt.clearfix');
            if (paperCnt) {
                paperCnt.style.maxWidth = '100%';
            }
        }
        setMaxWidth();

        // 将除/main以外的元素的margin置为0
        function resetMargins() {
            const allElements = document.querySelectorAll('*');
            allElements.forEach(el => {
                if (el !== document.querySelector('body > main')) {
                    el.style.margin = '0px';
                }
            });
        }
        resetMargins();

        // 将所有class="ques-item"的上下padding置为0
        function resetPadding() {
            const quesItems = document.querySelectorAll('.ques-item');
            quesItems.forEach(item => {
                item.style.paddingTop = '0';
                if (!item.style.paddingBottom || item.style.paddingBottom === '') {
                    item.style.paddingBottom = '0';
                }
            });
        }
        resetPadding();

        // 转换为打印按钮
        button.textContent = '打印';
        button.style.backgroundColor = '#4CAF50';
        button.onclick = function() {
            // 先删除按钮,再调用浏览器打印功能
            button.remove();
            window.print();
        };
    });
})();