(function(){ var OFFICE_EXT = ["docx","pptx","ppt","xlsx","xls"]; var PREVIEW_BASE = "http://" + location.hostname + ":8080"; var LAST_OPEN = { path: "", ts: 0 }; var OPEN_GUARD_MS = 1200; function startsWithFiles(p){ return (p || "").toLowerCase().indexOf("/files/") === 0; } function normalizeFilePath(path){ var p = path || ""; if (!startsWithFiles(p)) return ""; p = p.slice(7); try { p = decodeURIComponent(p); } catch(e) {} while (p.length > 0 && p.charAt(0) === "/") p = p.slice(1); return p; } function isOffice(path){ var s = (path || "").toLowerCase(); for (var i = 0; i < OFFICE_EXT.length; i++) { if (s.endsWith("." + OFFICE_EXT[i])) return true; } return false; } function encodePathSegments(path){ return (path || "").split("/").map(function(seg){ return encodeURIComponent(seg); }).join("/"); } function buildRawPath(rel){ var low = (rel || "").toLowerCase(); if (low.indexOf("words/") === 0) { return "/raw/words/" + encodePathSegments((rel || "").slice(6)); } if (low.indexOf("excels/") === 0) { return "/raw/excels/" + encodePathSegments((rel || "").slice(7)); } return "/raw/files/" + encodePathSegments(rel || ""); } function buildPreviewUrlFromPath(pathname){ var rel = normalizeFilePath(pathname); if (!rel || !isOffice(rel)) return ""; var ext = (rel.split(".").pop() || "").toLowerCase(); var rawUrl = PREVIEW_BASE + buildRawPath(rel); var nonce = Date.now().toString() + "-" + Math.random().toString(16).slice(2, 8); var mode = (ext === "xlsx" || ext === "xls") ? "edit" : "view"; return PREVIEW_BASE + "/oo-preview.html?url=" + encodeURIComponent(rawUrl) + "&type=" + encodeURIComponent(ext) + "&mode=" + encodeURIComponent(mode) + "&title=" + encodeURIComponent(rel) + "&_fbv=" + encodeURIComponent(nonce); } function parentDirPath(pathname){ var p = pathname || ""; if (!startsWithFiles(p)) return "/files/"; var idx = p.lastIndexOf("/"); if (idx <= 6) return "/files/"; return p.slice(0, idx + 1); } function openPreviewInNewTab(pathname){ var p = pathname || ""; var u = buildPreviewUrlFromPath(p); if (!u) return false; var now = Date.now(); if (LAST_OPEN.path === p && (now - LAST_OPEN.ts) < OPEN_GUARD_MS) return true; LAST_OPEN.path = p; LAST_OPEN.ts = now; var w = window.open(u, "_blank", "noopener"); if (!w) { // Popup blocked fallback location.assign(u); } return true; } function interceptFileClick(){ document.addEventListener("click", function(e){ var a = e.target && e.target.closest && e.target.closest("a[href]"); if (!a) return; var href = a.getAttribute("href") || ""; if (!href) return; var pathname = ""; try { pathname = new URL(href, location.origin).pathname || ""; } catch (err) { pathname = href; } if (!startsWithFiles(pathname)) return; if (!buildPreviewUrlFromPath(pathname)) return; e.preventDefault(); e.stopPropagation(); if (e.stopImmediatePropagation) e.stopImmediatePropagation(); openPreviewInNewTab(pathname); }, true); } function maybeJump(){ var p = location.pathname || ""; if (!buildPreviewUrlFromPath(p)) return; if (location.pathname === "/oo-preview.html") return; if (openPreviewInNewTab(p)) { location.replace(parentDirPath(p)); } } var pushed = history.pushState; history.pushState = function(){ pushed.apply(history, arguments); setTimeout(maybeJump, 0); }; var replaced = history.replaceState; history.replaceState = function(){ replaced.apply(history, arguments); setTimeout(maybeJump, 0); }; window.addEventListener("popstate", function(){ setTimeout(maybeJump, 0); }); window.addEventListener("hashchange", function(){ setTimeout(maybeJump, 0); }); interceptFileClick(); setTimeout(maybeJump, 0); })();