BCOI Problem Content to PDF (Print Preview)

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

目前為 2024-12-30 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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);
            };
        });
    });
})();