secure-pdf.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. (function () {
  2. "use strict";
  3. var TOKEN_API = "/website/secure-pdf/token/";
  4. var VIEWER_BASE = "/apps/space/pdf/secure-viewer.html";
  5. function buildTokenUrl(filePath) {
  6. return TOKEN_API + "?file=" + encodeURIComponent(filePath);
  7. }
  8. function resolveWebsiteId() {
  9. if (window.__ZQ_GUEST_GATE__ && window.__ZQ_GUEST_GATE__.websiteId) {
  10. return String(window.__ZQ_GUEST_GATE__.websiteId);
  11. }
  12. var body = document.querySelector(".article-body");
  13. if (body && body.getAttribute("data-website-id")) {
  14. return body.getAttribute("data-website-id");
  15. }
  16. return "";
  17. }
  18. function blockCopyShortcuts(root) {
  19. root.addEventListener("keydown", function (evt) {
  20. if (!evt.ctrlKey && !evt.metaKey) return;
  21. var key = (evt.key || "").toLowerCase();
  22. if (key === "p" || key === "s" || key === "c") {
  23. evt.preventDefault();
  24. evt.stopPropagation();
  25. }
  26. }, true);
  27. root.addEventListener("contextmenu", function (evt) {
  28. evt.preventDefault();
  29. });
  30. }
  31. function requestToken(filePath) {
  32. return fetch(buildTokenUrl(filePath), { credentials: "same-origin" })
  33. .then(function (res) { return res.json(); })
  34. .then(function (payload) {
  35. if (!payload || payload.code !== "success" || !payload.data || !payload.data.token) {
  36. throw new Error("token failed");
  37. }
  38. return payload.data.token;
  39. });
  40. }
  41. function showSecurePdfMessage(figure, text) {
  42. var message = figure.querySelector(".space-secure-pdf-message");
  43. if (!message) {
  44. message = document.createElement("div");
  45. message.className = "space-secure-pdf-message";
  46. figure.appendChild(message);
  47. }
  48. message.textContent = text;
  49. message.style.display = "block";
  50. }
  51. function hideSecurePdfMessage(figure) {
  52. var message = figure.querySelector(".space-secure-pdf-message");
  53. if (message) {
  54. message.style.display = "none";
  55. }
  56. }
  57. function activateSecureViewer(figure) {
  58. if (figure.getAttribute("data-space-pdf-activated") === "1") {
  59. return;
  60. }
  61. var src = figure.getAttribute("data-space-pdf-src");
  62. if (!src) {
  63. var iframe = figure.querySelector("iframe.space-editor-pdf-frame");
  64. src = iframe ? iframe.getAttribute("src") : "";
  65. }
  66. if (!src) {
  67. showSecurePdfMessage(figure, "安全 PDF 配置缺少文件地址,请重新插入 PDF。");
  68. return;
  69. }
  70. figure.setAttribute("data-space-pdf-activated", "1");
  71. blockCopyShortcuts(figure);
  72. var caption = figure.querySelector("figcaption");
  73. if (caption) {
  74. caption.style.display = "none";
  75. }
  76. var targetIframe = figure.querySelector("iframe.space-editor-pdf-frame");
  77. if (!targetIframe) {
  78. targetIframe = document.createElement("iframe");
  79. targetIframe.className = "space-editor-pdf-frame";
  80. targetIframe.setAttribute("loading", "lazy");
  81. targetIframe.setAttribute("frameborder", "0");
  82. targetIframe.setAttribute("title", "安全 PDF 预览");
  83. figure.insertBefore(targetIframe, figure.firstChild);
  84. }
  85. showSecurePdfMessage(figure, "正在加载安全 PDF…");
  86. requestToken(src).then(function (token) {
  87. var viewerUrl = VIEWER_BASE + "?v=20260709-pdf-secure10&token=" + encodeURIComponent(token);
  88. var websiteId = resolveWebsiteId();
  89. if (websiteId) {
  90. viewerUrl += "&websiteid=" + encodeURIComponent(websiteId);
  91. }
  92. hideSecurePdfMessage(figure);
  93. targetIframe.setAttribute("src", viewerUrl);
  94. }).catch(function () {
  95. if (caption) {
  96. caption.style.display = "";
  97. }
  98. figure.removeAttribute("data-space-pdf-activated");
  99. showSecurePdfMessage(figure, "安全 PDF 加载失败,请刷新页面后重试;若仍失败,请确认后台已重启并部署 secure-pdf 接口。");
  100. });
  101. }
  102. function init() {
  103. var figures = document.querySelectorAll('figure[data-space-pdf-secure="1"]');
  104. for (var i = 0; i < figures.length; i++) {
  105. activateSecureViewer(figures[i]);
  106. }
  107. }
  108. if (document.readyState === "loading") {
  109. document.addEventListener("DOMContentLoaded", init);
  110. } else {
  111. init();
  112. }
  113. function captureArticleImageWidths() {
  114. var root = document.querySelector(".article-body, [data-article-preview]");
  115. if (!root) return;
  116. var figures = root.querySelectorAll("figure.image, figure");
  117. for (var i = 0; i < figures.length; i++) {
  118. var figure = figures[i];
  119. if (!figure.hasAttribute("data-article-original-width")) {
  120. var width = figure.style.width || figure.getAttribute("width");
  121. if (width) figure.setAttribute("data-article-original-width", width);
  122. }
  123. }
  124. var images = root.querySelectorAll("img");
  125. for (var j = 0; j < images.length; j++) {
  126. var img = images[j];
  127. if (!img.hasAttribute("data-article-original-width")) {
  128. var imageWidth = img.style.width || img.getAttribute("width");
  129. if (imageWidth) img.setAttribute("data-article-original-width", imageWidth);
  130. }
  131. }
  132. }
  133. captureArticleImageWidths();
  134. function parseArticleImageLength(value) {
  135. var match = String(value || "").trim().match(/^(-?\d+(?:\.\d+)?)(px)?$/i);
  136. if (!match) return 0;
  137. var number = parseFloat(match[1]);
  138. return isFinite(number) && number > 0 ? number : 0;
  139. }
  140. function applyArticleImageLayout() {
  141. var root = document.querySelector(".article-body, [data-article-preview]");
  142. if (!root) return;
  143. var images = root.querySelectorAll("img");
  144. for (var i = 0; i < images.length; i++) {
  145. var img = images[i];
  146. var figure = img.closest("figure.image, figure");
  147. var classes = figure ? String(figure.className || "").split(/\s+/) : [];
  148. var isFull = classes.indexOf("image-style-align-full") >= 0 || classes.indexOf("image-style-full") >= 0;
  149. var isLeft = classes.indexOf("image-style-align-left") >= 0;
  150. var isRight = classes.indexOf("image-style-align-right") >= 0;
  151. var figureWidth = figure ? parseArticleImageLength(figure.getAttribute("data-original-width") || figure.getAttribute("data-article-original-width") || figure.style.width || figure.getAttribute("width")) : 0;
  152. var imageWidth = parseArticleImageLength(img.getAttribute("data-original-width") || img.getAttribute("data-article-original-width") || img.style.width || img.getAttribute("width"));
  153. img.style.setProperty("display", "block", "important");
  154. img.style.setProperty("height", "auto", "important");
  155. img.style.setProperty("max-width", "100%", "important");
  156. img.style.setProperty("margin", "18px auto", "important");
  157. if (isFull) {
  158. if (figure) figure.style.setProperty("width", "100%", "important");
  159. img.style.setProperty("width", "100%", "important");
  160. } else if (figureWidth) {
  161. figure.style.setProperty("width", figureWidth + "px", "important");
  162. img.style.setProperty("width", "100%", "important");
  163. } else if (imageWidth) {
  164. img.style.setProperty("width", imageWidth + "px", "important");
  165. } else {
  166. img.style.setProperty("width", "auto", "important");
  167. }
  168. if (figure) {
  169. figure.style.setProperty("display", "block", "important");
  170. figure.style.setProperty("max-width", "100%", "important");
  171. figure.style.setProperty("padding", "0", "important");
  172. figure.style.setProperty("text-indent", "0", "important");
  173. if (isLeft) {
  174. figure.style.setProperty("float", "left", "important");
  175. figure.style.setProperty("margin", "18px 20px 18px 0", "important");
  176. figure.style.setProperty("text-align", "left", "important");
  177. } else if (isRight) {
  178. figure.style.setProperty("float", "right", "important");
  179. figure.style.setProperty("margin", "18px 0 18px 20px", "important");
  180. figure.style.setProperty("text-align", "right", "important");
  181. } else {
  182. figure.style.setProperty("float", "none", "important");
  183. figure.style.setProperty("margin", "18px auto", "important");
  184. figure.style.setProperty("text-align", "center", "important");
  185. }
  186. }
  187. }
  188. }
  189. window.setTimeout(applyArticleImageLayout, 0);
  190. window.setTimeout(applyArticleImageLayout, 250);
  191. window.setTimeout(applyArticleImageLayout, 900);
  192. window.addEventListener("load", applyArticleImageLayout);
  193. })();