mhtml2html.html 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>MHTML Renderer</title>
  6. <script src="https://unpkg.com/mhtml2html@3.0.0/dist/mhtml2html.js"></script>
  7. <style>
  8. /* 保持原有样式不变 */
  9. #loading {
  10. position: fixed;
  11. top: 50%;
  12. left: 50%;
  13. transform: translate(-50%, -50%);
  14. }
  15. iframe {
  16. width: 100%;
  17. height: 100vh;
  18. border: none;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <div id="loading">加载中...</div>
  24. <div id="content"></div>
  25. <script>
  26. document.addEventListener('DOMContentLoaded', () => {
  27. const loader = document.getElementById('loading');
  28. const contentDiv = document.getElementById('content');
  29. // 新增:解析URL参数
  30. function getUrlParameter(name) {
  31. const params = new URLSearchParams(window.location.search);
  32. return params.get(name);
  33. }
  34. async function renderMHTML(url) {
  35. // 保持原有渲染逻辑不变
  36. try {
  37. const res = await fetch(url);
  38. if (!res.ok) throw new Error(`HTTP ${res.status}`);
  39. const mhtml = await res.text();
  40. const windowProxy = mhtml2html.convert(mhtml, {
  41. convertIframes: true,
  42. parseDOM: html => {
  43. const frame = document.createElement('iframe');
  44. frame.style.display = 'none';
  45. document.body.appendChild(frame);
  46. frame.contentDocument.write(html);
  47. return frame.contentWindow;
  48. }
  49. });
  50. if (!windowProxy?.document?.documentElement) {
  51. throw new Error('文档结构解析失败');
  52. }
  53. const renderFrame = document.createElement('iframe');
  54. renderFrame.sandbox = 'allow-same-origin allow-scripts';
  55. Object.assign(renderFrame.style, {
  56. width: '100%',
  57. height: '100vh'
  58. });
  59. contentDiv.innerHTML = '';
  60. contentDiv.appendChild(renderFrame);
  61. loader.style.display = 'none';
  62. renderFrame.contentDocument.replaceChild(
  63. windowProxy.document.documentElement,
  64. renderFrame.contentDocument.documentElement
  65. );
  66. } catch (error) {
  67. loader.innerHTML = `
  68. <div style="color: red; padding: 20px; text-align: center">
  69. <h3>渲染失败</h3>
  70. <p>${error.message}</p>
  71. <button onclick="location.reload()">点击重试</button>
  72. </div>`;
  73. console.error('[RENDER ERROR]', error);
  74. }
  75. }
  76. // 修改:从URL参数获取mhtml地址
  77. const mhtmlUrl = getUrlParameter('url');
  78. if (mhtmlUrl) {
  79. renderMHTML(mhtmlUrl);
  80. } else {
  81. loader.innerHTML = `
  82. <div style="padding: 20px; text-align: center">
  83. <h3>缺少URL参数</h3>
  84. <p>请使用 ?url= 参数指定MHTML文件地址</p>
  85. <p>示例:${window.location.href}?url=http://s3.vs1.lan/public/amazone/copywriting_production/output/B0B658JC22/B0B658JC22.mhtml</p>
  86. </div>`;
  87. }
  88. });
  89. </script>
  90. </body>
  91. </html>