preview.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <!doctype html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>预览</title>
  7. <style>
  8. html,
  9. body {
  10. margin: 0;
  11. padding: 0;
  12. height: 100%;
  13. background: #1f1f1f;
  14. color: #fff;
  15. font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
  16. "Microsoft YaHei", sans-serif;
  17. overflow: hidden;
  18. user-select: none;
  19. }
  20. .center {
  21. display: flex;
  22. align-items: center;
  23. justify-content: center;
  24. height: 100vh;
  25. width: 100vw;
  26. }
  27. img,
  28. video {
  29. max-width: 100%;
  30. max-height: 100%;
  31. display: block;
  32. background: #000;
  33. }
  34. .msg {
  35. padding: 24px;
  36. text-align: center;
  37. color: #ccc;
  38. font-size: 14px;
  39. }
  40. .err {
  41. color: #ff7875;
  42. }
  43. .hint {
  44. position: fixed;
  45. bottom: 8px;
  46. left: 0;
  47. right: 0;
  48. text-align: center;
  49. font-size: 12px;
  50. color: #888;
  51. pointer-events: none;
  52. }
  53. </style>
  54. </head>
  55. <body>
  56. <!-- 入口容器:根据 URL ?type=image|video & path=<abs path> 渲染对应元素 -->
  57. <div class="center" id="root">
  58. <div class="msg">加载中…</div>
  59. </div>
  60. <div class="hint" id="path-hint"></div>
  61. <script>
  62. (function () {
  63. var params = new URLSearchParams(location.search);
  64. var type = params.get("type");
  65. var path = params.get("path");
  66. var root = document.getElementById("root");
  67. var hint = document.getElementById("path-hint");
  68. if (!path || (type !== "image" && type !== "video")) {
  69. root.innerHTML =
  70. '<div class="msg err">参数错误:缺少 type 或 path</div>';
  71. return;
  72. }
  73. hint.textContent = path;
  74. // Tauri v2 在 window 注入 __TAURI__ 全局对象(依赖
  75. // tauri.conf.json#app.withGlobalTauri = true)。convertFileSrc
  76. // 会按 OS 把 fs 路径转成 asset://localhost/... 或
  77. // https://asset.localhost/...,再由 webview 通过 asset 协议加载文件
  78. var convertFileSrc =
  79. window.__TAURI__ &&
  80. window.__TAURI__.core &&
  81. window.__TAURI__.core.convertFileSrc;
  82. if (!convertFileSrc) {
  83. root.innerHTML =
  84. '<div class="msg err">未找到 __TAURI__.core.convertFileSrc,' +
  85. "请检查 tauri.conf.json 是否启用 withGlobalTauri</div>";
  86. return;
  87. }
  88. var src = convertFileSrc(path);
  89. root.innerHTML = "";
  90. if (type === "image") {
  91. var img = document.createElement("img");
  92. img.src = src;
  93. img.alt = path;
  94. img.onerror = function () {
  95. root.innerHTML =
  96. '<div class="msg err">图片加载失败<br/>' + path + "</div>";
  97. };
  98. root.appendChild(img);
  99. } else {
  100. var video = document.createElement("video");
  101. video.src = src;
  102. video.controls = true;
  103. video.autoplay = true;
  104. video.onerror = function () {
  105. root.innerHTML =
  106. '<div class="msg err">视频加载失败<br/>' + path + "</div>";
  107. };
  108. root.appendChild(video);
  109. }
  110. })();
  111. </script>
  112. </body>
  113. </html>