BCOI Problem Content to PDF (Print Preview)

将 BCOI 问题内容通过打印预览导出为 PDF

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         BCOI Problem Content to PDF (Print Preview)
// @namespace    http://tampermonkey.net/
// @version      1.3
// @description  将 BCOI 问题内容通过打印预览导出为 PDF
// @description  Add a copy button to code blocks with improved styling
// @author       Y.V
// @license      AGPL-3.0-or-later
// @match        https://www.bcoi.cn/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // 等待页面加载完成
    window.addEventListener('load', function() {
        // 获取问题内容容器
        const problemContentContainer = document.querySelector('div.problem-content-container');
        if (!problemContentContainer) return;

        // 创建下载按钮
        const downloadButton = document.createElement('button');
        downloadButton.innerText = '导出 PDF';
        downloadButton.style.position = 'fixed';
        downloadButton.style.right = '20px';
        downloadButton.style.top = '20px';
        downloadButton.style.zIndex = 1000;
        downloadButton.style.padding = '10px';
        downloadButton.style.backgroundColor = '#007bff';
        downloadButton.style.color = '#fff';
        downloadButton.style.border = 'none';
        downloadButton.style.borderRadius = '5px';
        downloadButton.style.cursor = 'pointer';

        // 添加按钮到页面
        document.body.appendChild(downloadButton);

        // 按钮点击事件
        downloadButton.addEventListener('click', function() {
            // 创建一个新的 iframe
            const iframe = document.createElement('iframe');
            iframe.style.display = 'none';
            document.body.appendChild(iframe);

            // 将问题内容复制到 iframe 中
            const iframeDoc = iframe.contentDocument || iframe.contentWindow.document;
            iframeDoc.open();
            iframeDoc.write(`
                <!DOCTYPE html>
                <html lang="zh-CN">
                <head>
                    <meta charset="UTF-8">
                    <title>问题内容</title>
                    <style>
                        body { font-family: Arial, sans-serif; }
                        .problem-content-container { margin: 20px; }
                    </style>
                </head>
                <body>
                    <div class="problem-content-container">${problemContentContainer.innerHTML}</div>
                </body>
                </html>
            `);
            iframeDoc.close();

            // 等待 iframe 内容加载完成
            iframe.onload = function() {
                // 触发打印预览
                iframe.contentWindow.print();

                // 移除 iframe
                document.body.removeChild(iframe);
            };
        });
    });
})();