BCOI Problem Content to PDF (Print Preview)

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

  1. // ==UserScript==
  2. // @name BCOI Problem Content to PDF (Print Preview)
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.3
  5. // @description 将 BCOI 问题内容通过打印预览导出为 PDF
  6. // @description Add a copy button to code blocks with improved styling
  7. // @author Y.V
  8. // @license AGPL-3.0-or-later
  9. // @match https://www.bcoi.cn/*
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. // 等待页面加载完成
  17. window.addEventListener('load', function() {
  18. // 获取问题内容容器
  19. const problemContentContainer = document.querySelector('div.problem-content-container');
  20. if (!problemContentContainer) return;
  21.  
  22. // 创建下载按钮
  23. const downloadButton = document.createElement('button');
  24. downloadButton.innerText = '导出 PDF';
  25. downloadButton.style.position = 'fixed';
  26. downloadButton.style.right = '20px';
  27. downloadButton.style.top = '20px';
  28. downloadButton.style.zIndex = 1000;
  29. downloadButton.style.padding = '10px';
  30. downloadButton.style.backgroundColor = '#007bff';
  31. downloadButton.style.color = '#fff';
  32. downloadButton.style.border = 'none';
  33. downloadButton.style.borderRadius = '5px';
  34. downloadButton.style.cursor = 'pointer';
  35.  
  36. // 添加按钮到页面
  37. document.body.appendChild(downloadButton);
  38.  
  39. // 按钮点击事件
  40. downloadButton.addEventListener('click', function() {
  41. // 创建一个新的 iframe
  42. const iframe = document.createElement('iframe');
  43. iframe.style.display = 'none';
  44. document.body.appendChild(iframe);
  45.  
  46. // 将问题内容复制到 iframe 中
  47. const iframeDoc = iframe.contentDocument || iframe.contentWindow.document;
  48. iframeDoc.open();
  49. iframeDoc.write(`
  50. <!DOCTYPE html>
  51. <html lang="zh-CN">
  52. <head>
  53. <meta charset="UTF-8">
  54. <title>问题内容</title>
  55. <style>
  56. body { font-family: Arial, sans-serif; }
  57. .problem-content-container { margin: 20px; }
  58. </style>
  59. </head>
  60. <body>
  61. <div class="problem-content-container">${problemContentContainer.innerHTML}</div>
  62. </body>
  63. </html>
  64. `);
  65. iframeDoc.close();
  66.  
  67. // 等待 iframe 内容加载完成
  68. iframe.onload = function() {
  69. // 触发打印预览
  70. iframe.contentWindow.print();
  71.  
  72. // 移除 iframe
  73. document.body.removeChild(iframe);
  74. };
  75. });
  76. });
  77. })();