| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- (function () {
- "use strict";
- var TOKEN_API = "/website/secure-pdf/token/";
- var VIEWER_BASE = "/apps/space/pdf/secure-viewer.html";
- function buildTokenUrl(filePath) {
- return TOKEN_API + "?file=" + encodeURIComponent(filePath);
- }
- function resolveWebsiteId() {
- if (window.__ZQ_GUEST_GATE__ && window.__ZQ_GUEST_GATE__.websiteId) {
- return String(window.__ZQ_GUEST_GATE__.websiteId);
- }
- var body = document.querySelector(".article-body");
- if (body && body.getAttribute("data-website-id")) {
- return body.getAttribute("data-website-id");
- }
- return "";
- }
- function blockCopyShortcuts(root) {
- root.addEventListener("keydown", function (evt) {
- if (!evt.ctrlKey && !evt.metaKey) return;
- var key = (evt.key || "").toLowerCase();
- if (key === "p" || key === "s" || key === "c") {
- evt.preventDefault();
- evt.stopPropagation();
- }
- }, true);
- root.addEventListener("contextmenu", function (evt) {
- evt.preventDefault();
- });
- }
- function requestToken(filePath) {
- return fetch(buildTokenUrl(filePath), { credentials: "same-origin" })
- .then(function (res) { return res.json(); })
- .then(function (payload) {
- if (!payload || payload.code !== "success" || !payload.data || !payload.data.token) {
- throw new Error("token failed");
- }
- return payload.data.token;
- });
- }
- function showSecurePdfMessage(figure, text) {
- var message = figure.querySelector(".space-secure-pdf-message");
- if (!message) {
- message = document.createElement("div");
- message.className = "space-secure-pdf-message";
- figure.appendChild(message);
- }
- message.textContent = text;
- message.style.display = "block";
- }
- function hideSecurePdfMessage(figure) {
- var message = figure.querySelector(".space-secure-pdf-message");
- if (message) {
- message.style.display = "none";
- }
- }
- function activateSecureViewer(figure) {
- if (figure.getAttribute("data-space-pdf-activated") === "1") {
- return;
- }
- var src = figure.getAttribute("data-space-pdf-src");
- if (!src) {
- var iframe = figure.querySelector("iframe.space-editor-pdf-frame");
- src = iframe ? iframe.getAttribute("src") : "";
- }
- if (!src) {
- showSecurePdfMessage(figure, "安全 PDF 配置缺少文件地址,请重新插入 PDF。");
- return;
- }
- figure.setAttribute("data-space-pdf-activated", "1");
- blockCopyShortcuts(figure);
- var caption = figure.querySelector("figcaption");
- if (caption) {
- caption.style.display = "none";
- }
- var targetIframe = figure.querySelector("iframe.space-editor-pdf-frame");
- if (!targetIframe) {
- targetIframe = document.createElement("iframe");
- targetIframe.className = "space-editor-pdf-frame";
- targetIframe.setAttribute("loading", "lazy");
- targetIframe.setAttribute("frameborder", "0");
- targetIframe.setAttribute("title", "安全 PDF 预览");
- figure.insertBefore(targetIframe, figure.firstChild);
- }
- showSecurePdfMessage(figure, "正在加载安全 PDF…");
- requestToken(src).then(function (token) {
- var viewerUrl = VIEWER_BASE + "?v=20260709-pdf-secure10&token=" + encodeURIComponent(token);
- var websiteId = resolveWebsiteId();
- if (websiteId) {
- viewerUrl += "&websiteid=" + encodeURIComponent(websiteId);
- }
- hideSecurePdfMessage(figure);
- targetIframe.setAttribute("src", viewerUrl);
- }).catch(function () {
- if (caption) {
- caption.style.display = "";
- }
- figure.removeAttribute("data-space-pdf-activated");
- showSecurePdfMessage(figure, "安全 PDF 加载失败,请刷新页面后重试;若仍失败,请确认后台已重启并部署 secure-pdf 接口。");
- });
- }
- function init() {
- var figures = document.querySelectorAll('figure[data-space-pdf-secure="1"]');
- for (var i = 0; i < figures.length; i++) {
- activateSecureViewer(figures[i]);
- }
- }
- if (document.readyState === "loading") {
- document.addEventListener("DOMContentLoaded", init);
- } else {
- init();
- }
- function captureArticleImageWidths() {
- var root = document.querySelector(".article-body, [data-article-preview]");
- if (!root) return;
- var figures = root.querySelectorAll("figure.image, figure");
- for (var i = 0; i < figures.length; i++) {
- var figure = figures[i];
- if (!figure.hasAttribute("data-article-original-width")) {
- var width = figure.style.width || figure.getAttribute("width");
- if (width) figure.setAttribute("data-article-original-width", width);
- }
- }
- var images = root.querySelectorAll("img");
- for (var j = 0; j < images.length; j++) {
- var img = images[j];
- if (!img.hasAttribute("data-article-original-width")) {
- var imageWidth = img.style.width || img.getAttribute("width");
- if (imageWidth) img.setAttribute("data-article-original-width", imageWidth);
- }
- }
- }
- captureArticleImageWidths();
- function parseArticleImageLength(value) {
- var match = String(value || "").trim().match(/^(-?\d+(?:\.\d+)?)(px)?$/i);
- if (!match) return 0;
- var number = parseFloat(match[1]);
- return isFinite(number) && number > 0 ? number : 0;
- }
- function applyArticleImageLayout() {
- var root = document.querySelector(".article-body, [data-article-preview]");
- if (!root) return;
- var images = root.querySelectorAll("img");
- for (var i = 0; i < images.length; i++) {
- var img = images[i];
- var figure = img.closest("figure.image, figure");
- var classes = figure ? String(figure.className || "").split(/\s+/) : [];
- var isFull = classes.indexOf("image-style-align-full") >= 0 || classes.indexOf("image-style-full") >= 0;
- var isLeft = classes.indexOf("image-style-align-left") >= 0;
- var isRight = classes.indexOf("image-style-align-right") >= 0;
- var figureWidth = figure ? parseArticleImageLength(figure.getAttribute("data-original-width") || figure.getAttribute("data-article-original-width") || figure.style.width || figure.getAttribute("width")) : 0;
- var imageWidth = parseArticleImageLength(img.getAttribute("data-original-width") || img.getAttribute("data-article-original-width") || img.style.width || img.getAttribute("width"));
- img.style.setProperty("display", "block", "important");
- img.style.setProperty("height", "auto", "important");
- img.style.setProperty("max-width", "100%", "important");
- img.style.setProperty("margin", "18px auto", "important");
- if (isFull) {
- if (figure) figure.style.setProperty("width", "100%", "important");
- img.style.setProperty("width", "100%", "important");
- } else if (figureWidth) {
- figure.style.setProperty("width", figureWidth + "px", "important");
- img.style.setProperty("width", "100%", "important");
- } else if (imageWidth) {
- img.style.setProperty("width", imageWidth + "px", "important");
- } else {
- img.style.setProperty("width", "auto", "important");
- }
- if (figure) {
- figure.style.setProperty("display", "block", "important");
- figure.style.setProperty("max-width", "100%", "important");
- figure.style.setProperty("padding", "0", "important");
- figure.style.setProperty("text-indent", "0", "important");
- if (isLeft) {
- figure.style.setProperty("float", "left", "important");
- figure.style.setProperty("margin", "18px 20px 18px 0", "important");
- figure.style.setProperty("text-align", "left", "important");
- } else if (isRight) {
- figure.style.setProperty("float", "right", "important");
- figure.style.setProperty("margin", "18px 0 18px 20px", "important");
- figure.style.setProperty("text-align", "right", "important");
- } else {
- figure.style.setProperty("float", "none", "important");
- figure.style.setProperty("margin", "18px auto", "important");
- figure.style.setProperty("text-align", "center", "important");
- }
- }
- }
- }
- window.setTimeout(applyArticleImageLayout, 0);
- window.setTimeout(applyArticleImageLayout, 250);
- window.setTimeout(applyArticleImageLayout, 900);
- window.addEventListener("load", applyArticleImageLayout);
- })();
|