(function(){ var OFFICE_EXT = ["docx","pptx","ppt","xlsx","xls"]; var PREVIEW_BASE = "http://" + location.hostname + ":8080"; 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); return PREVIEW_BASE + "/oo-preview.html?url=" + encodeURIComponent(rawUrl) + "&type=" + encodeURIComponent(ext) + "&title=" + encodeURIComponent(rel) + "&_fbv=" + encodeURIComponent(nonce); } function maybeJump(){ var p = location.pathname || ""; var u = buildPreviewUrlFromPath(p); if (!u) return; if (location.pathname === "/oo-preview.html") return; location.replace(u); } 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); }); setTimeout(maybeJump, 0); })();