ExamTopic Print

Print function for ExamTopics

当前为 2024-06-24 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         ExamTopic Print
// @namespace    http://tampermonkey.net/
// @version      2024-06-24
// @description  Print function for ExamTopics
// @author       You
// @icon         https://cdn-icons-png.flaticon.com/512/839/839184.png
// @match        https://www.examtopics.com/*
// @license MIT
// @grant        none
// ==/UserScript==

function getVoteSummary(element) {
    var voteBars = element.getElementsByClassName('vote-bar');
    var summary = {};
    var totalVotes = 0;

    for (var i = 0; i < voteBars.length; i++) {
        var voteBar = voteBars[i];
        var tooltipText = voteBar.getAttribute('data-original-title');
        var votes;

        if (tooltipText.includes('%')) {
            var percentage = parseFloat(tooltipText);
            votes = Math.round((percentage / 100) * totalVotes);
        } else {
            votes = parseInt(tooltipText);
        }

        if (!isNaN(votes)) {
            var option = voteBar.textContent.trim();
            summary[option] = votes;
            totalVotes += votes;
        }
    }

    return {
        'total': totalVotes,
        'votes': summary
    };
}

function extractQuestionNumber(str) {
    const match = str.match(/question (\d+)/);
    if (match) {
        return 'question_' + match[1];
    } else {
        return 'something_wrong_in_parse_title';
    }
}


(function() {
    'use strict';
    window.addEventListener('load', function() {
        var printButton = document.createElement("button");
        printButton.innerHTML = "Print";
        printButton.style.marginLeft = "10px";
        printButton.onclick = printFunction;

        var correctAnswerBox = document.querySelector(".correct-answer-box br");
        correctAnswerBox.parentNode.insertBefore(printButton, correctAnswerBox);

        function printFunction() {
            var selectedElement = document.querySelectorAll(".col-12")[3];
            var clonedElement = selectedElement.cloneNode(true);

            // calculate vote
            const voteSummary = getVoteSummary(clonedElement);

            console.log(voteSummary);

            let voteSummaryDiv = document.createElement("div");

            // Create HTML content
            let voteSummaryHTML = `<b>Total: ${voteSummary.total}</b><ul>`;
            for (let voteType in voteSummary.votes) {
                voteSummaryHTML += `<li>${voteType}: ${voteSummary.votes[voteType]}</li>`;
            }
            voteSummaryHTML += "</ul>";

            // Set the HTML content to the new div
            voteSummaryDiv.innerHTML = voteSummaryHTML;

            // Append the new div to the last element with class 'question-answer'
            let questionAnswerElements = clonedElement.getElementsByClassName('question-answer');
            let lastQuestionAnswerElement = questionAnswerElements[questionAnswerElements.length - 1];
            lastQuestionAnswerElement.appendChild(voteSummaryDiv);


            var listItemElements = clonedElement.querySelectorAll("li");
            listItemElements.forEach(function (listItem) {
                listItem.className = "multi-choice-item";
            });

            var garbege = [
                '.all-questions-link',
                '.correct-answer-box',
                '.disclaimer-box',
                'span.badge.badge-success.most-voted-answer-badge',
                '.correct-answer',
                '.vote-answer-button',
                '.voting-summary',
            ];

            garbege.forEach(function(selector) {
                var elements = clonedElement.querySelectorAll(selector);
                elements.forEach(function(element) {
                    element.parentNode.removeChild(element);
                });
            });

            var mywindow = window.open("", "PRINT", "height=1754,width=1240");

            mywindow.document.write(`
                <html>
                <head>
                <title>${extractQuestionNumber(document.title)}</title>
            `);

            Array.prototype.forEach.call(document.styleSheets, function (sheet) {
                mywindow.document.write(
                    `<style type="text/css">
                        body {
                            font-family: Arial !important;
                        }
                        @page {
                            margin-left: 1.3in;
                            margin-right: 1.3in;
                        }
                    </style>
                    <link rel="stylesheet" href="${sheet.href}" type="text/css" />`
                );
            });

            mywindow.document.write(`
                </head>
                <body>
                ${clonedElement.innerHTML}
                </body>
                </html>
            `);

            setTimeout(function() {
                mywindow.print();
                mywindow.close();
            }, 500);


            return true;
        }
    }, false);
})();