/* nomadro 小红书小工具 — 对齐 H5 门户首页,纯离线 SPA */ (function () { "use strict"; var portal = window.PORTAL_DATA || (typeof PORTAL_DATA !== "undefined" ? PORTAL_DATA : null) || {}; var cfg = portal.config || {}; var nav = portal.nav || window.NAV_DATA || (typeof NAV_DATA !== "undefined" ? NAV_DATA : {}) || {}; var book = portal.book || { title: "小册", chapters: [] }; var cover = portal.cover || {}; var community = portal.community || { meetups: [] }; var vip = portal.vip || {}; var categories = (nav.categories || []).filter(function (c) { return !c.hidden; }); var state = { view: "home", theme: "dark", activeId: categories[0] ? categories[0].id : "", drawerOpen: false, sheetOpen: false, currentLink: null, jumping: false, bookMode: "cover", // cover | toc | read bookChapter: null, faqOpen: 0, oaOpen: false, }; var twTimers = {}; var els = {}; var BOOK_VISUAL_CSS = '
"; var SOCIAL_MESH_CSS = '"; /* 保留变量名兼容旧引用;XHS WebView 渐变 fill 常失效,改用 CSS 实心视觉 */ var BOOK_VISUAL_SVG = BOOK_VISUAL_CSS; var SOCIAL_MESH_SVG = SOCIAL_MESH_CSS; var VIP_LOGO_SVG = ''; function $(id) { return document.getElementById(id); } function escapeHtml(str) { return String(str == null ? "" : str) .replace(/&/g, "&") .replace(//g, ">") .replace(/"/g, """); } /** 小红书 WebView 可能没有 Element.closest */ function closestAttr(node, attrName) { var el = node; if (el && el.nodeType === 3) el = el.parentElement || el.parentNode; while (el && el.nodeType === 1) { if (el.getAttribute && el.getAttribute(attrName) != null) return el; el = el.parentElement || el.parentNode; } return null; } function hasClass(el, name) { if (!el) return false; return (" " + String(el.className || "") + " ").indexOf(" " + name + " ") >= 0; } function addClass(el, name) { if (!el || hasClass(el, name)) return; el.className = (String(el.className || "") + " " + name).replace(/^\s+/, ""); } function removeClass(el, name) { if (!el || !hasClass(el, name)) return; el.className = (" " + String(el.className || "") + " ") .replace(" " + name + " ", " ") .replace(/\s+/g, " ") .replace(/^\s+|\s+$/g, ""); } /** 切页后短暂锁输入,避免 touch 穿透到新页按钮/弹层 */ var inputLockUntil = 0; function lockInput(ms) { var wait = ms || 480; inputLockUntil = Date.now() + wait; try { document.documentElement.classList.add("xhs-input-lock"); clearTimeout(lockInput._t); lockInput._t = setTimeout(function () { try { document.documentElement.classList.remove("xhs-input-lock"); } catch (e) {} }, wait); } catch (e2) {} } function isInputLocked() { return Date.now() < inputLockUntil; } /** * 直绑点击:禁止 touchstart 导航(会点穿下一页)。 * 优先 touchend,再兜底 click;同一手势只触发一次。 */ function onTap(target, handler) { if (!target) return; var fired = false; function run(e) { if (isInputLocked()) return; if (fired) return; fired = true; setTimeout(function () { fired = false; }, 520); lockInput(520); try { if (e) { if (e.preventDefault) e.preventDefault(); if (e.stopPropagation) e.stopPropagation(); } } catch (err) {} handler(e); } try { target.addEventListener("touchend", run, false); target.addEventListener("click", run, false); } catch (err2) { target.onclick = run; } } function bindPortalCards() { var root = els.homeCards || $("home-cards"); if (!root) return; var cards = root.querySelectorAll ? root.querySelectorAll("[data-go]") : root.getElementsByTagName("*"); for (var i = 0; i < cards.length; i++) { (function (card) { if (!card || !card.getAttribute) return; var key = card.getAttribute("data-go"); if (!key) return; if (card.getAttribute("data-bound") === "1") return; card.setAttribute("data-bound", "1"); // 去掉内联 onclick,避免与 onTap 双触发 try { card.removeAttribute("onclick"); } catch (e) {} onTap(card, function () { showView(key); }); })(cards[i]); } } function applySafeArea() { var padTop = 88; var padLeft = 16; try { var probe = document.createElement("div"); probe.style.cssText = "position:absolute;visibility:hidden;pointer-events:none;padding-top:constant(safe-area-inset-top);padding-top:env(safe-area-inset-top);"; document.body.appendChild(probe); var envTop = parseFloat(window.getComputedStyle(probe).paddingTop) || 0; document.body.removeChild(probe); // 状态栏 + 宿主返回/分享行;至少 88,避免 logo 与返回箭头同行 padTop = Math.max(envTop, 44) + 44; } catch (e0) {} try { var root = document.documentElement; root.style.setProperty("--app-pad-top", padTop + "px"); root.style.setProperty("--app-pad-left", padLeft + "px"); root.style.setProperty("--hub-safe-top", padTop + "px"); root.style.setProperty("--safe-status-bar", Math.max(padTop - 44, 44) + "px"); root.style.setProperty("--safe-header", padTop + 44 + "px"); // 同步写 body,兼容部分 WebView 不继承 :root 变量 if (document.body) { document.body.style.setProperty("--app-pad-top", padTop + "px"); document.body.style.setProperty("--hub-safe-top", padTop + "px"); } } catch (e1) {} // 关键:直接写 inline,不依赖 CSS var function pinTop(el, sticky) { if (!el) return; try { el.style.setProperty("top", padTop + "px", "important"); el.style.setProperty("padding-top", "0px", "important"); el.style.setProperty("position", sticky ? "sticky" : "fixed", "important"); el.style.setProperty("z-index", "1000", "important"); } catch (ePin) {} } pinTop($("hub-navbar"), false); // 导航顶栏:fixed + padding-top 含安全区,避免 sticky 空隙漏内容 try { var tb = $("topbar"); if (tb) { tb.style.setProperty("position", "fixed", "important"); tb.style.setProperty("top", "0px", "important"); tb.style.setProperty("left", "0", "important"); tb.style.setProperty("right", "0", "important"); tb.style.setProperty("padding-top", padTop + "px", "important"); tb.style.setProperty("z-index", "1000", "important"); tb.style.setProperty("background", state.theme === "light" ? "#e9eef4" : "#0a0f14", "important"); } } catch (eTb) {} try { var navs = document.querySelectorAll( ".xhs-hero-page .navbar, .book-flow .navbar, .community-flow .navbar, .vip-header, #view-book .ebook-topbar", ); for (var n = 0; n < navs.length; n++) { var isSticky = navs[n].classList.contains("vip-header") || navs[n].classList.contains("ebook-topbar"); pinTop(navs[n], isSticky); } } catch (eNav) {} try { var vn = $("view-nav"); if (vn) vn.style.setProperty("padding-top", "0px", "important"); var page = $("page"); if (page) { page.style.setProperty("padding-top", padTop + 52 + "px", "important"); } } catch (e3) {} try { var heroPages = document.querySelectorAll(".xhs-hero-page"); for (var h = 0; h < heroPages.length; h++) { heroPages[h].style.setProperty( "padding-top", padTop + 44 + "px", "important", ); } } catch (e4) {} try { var hubInner = document.querySelector(".hub-hero-inner"); if (hubInner) { hubInner.style.setProperty( "padding-top", padTop + 44 + 4 + "px", "important", ); } } catch (e5) {} try { var vipRoot = document.querySelector(".vip-root"); if (vipRoot) { vipRoot.style.setProperty("padding-top", padTop + "px", "important"); } } catch (e6) {} applySafeArea._padTop = padTop; injectSafeAreaStyle(padTop); } /** 注入硬编码顶距样式,双保险(不依赖 var 解析) */ function injectSafeAreaStyle(padTop) { padTop = padTop || 88; var css = "#hub-navbar,.hub-navbar,.xhs-hero-page .navbar,.book-flow .navbar,.community-flow .navbar{" + "position:fixed!important;top:" + padTop + "px!important;padding-top:0!important;height:44px!important;z-index:1000!important;" + "background:transparent!important;border-bottom:none!important;}" + /* 导航顶栏含安全区,实色遮罩,上滑内容不再从顶栏上方漏出 */ "#topbar,.topbar{position:fixed!important;top:0!important;left:0!important;right:0!important;" + "padding-top:" + padTop + "px!important;z-index:1000!important;background:#0a0f14!important;}" + "html[data-theme=light] #topbar,html[data-theme=light] .topbar{background:#e9eef4!important;}" + "#view-nav{padding-top:0!important;}" + "#view-nav .page,#page{padding-top:" + (padTop + 52) + "px!important;}" + ".xhs-hero-page{padding-top:" + (padTop + 44) + "px!important;}" + ".hub-hero-inner{padding-top:" + (padTop + 48) + "px!important;}" + ".vip-root{padding-top:" + padTop + "px!important;}" + ".vip-header,#view-book .ebook-topbar{position:sticky!important;top:" + padTop + "px!important;}" + ".xhs-home-back{flex-shrink:0;width:40px;height:40px;margin:0;padding:0;border:0;" + "border-radius:12px;background:rgba(255,255,255,.08);color:#f1f5f9;font-size:1.25rem;" + "line-height:40px;text-align:center;cursor:pointer;-webkit-tap-highlight-color:transparent;}"; try { var el = document.getElementById("xhs-safe-style"); if (!el) { el = document.createElement("style"); el.id = "xhs-safe-style"; document.head.appendChild(el); } el.textContent = css; } catch (e) {} } var histSeq = 0; function pushTrap() { try { histSeq += 1; history.pushState({ xhs: 1, n: histSeq }, ""); } catch (e) {} } function replaceTrap() { try { histSeq += 1; history.replaceState({ xhs: 1, n: histSeq }, ""); } catch (e) {} } /** 宿主返回:XHS 仅允许单 index.html,只能靠 pushState + popstate 回首页 */ function goHomeFromHostBack() { if (state.oaOpen) { setOaModal(false); return true; } if (state.sheetOpen) { setSheet(false); return true; } if (state.drawerOpen) { setDrawer(false); return true; } if (state.view === "book" && state.bookMode === "read") { state.bookMode = "cover"; state.bookChapter = null; renderBook(); return true; } if (state.view !== "home") { showView("home", { fromPop: true, forceSpa: true }); return true; } return false; } function setTheme(theme) { state.theme = theme; document.documentElement.setAttribute("data-theme", theme); try { localStorage.setItem("nomadro-xhs-theme", theme); } catch (e) {} var ic = theme === "dark" ? "☀" : "☾"; if (els.homeThemeIcon) els.homeThemeIcon.textContent = ic; if (els.themeIcon) els.themeIcon.textContent = ic; try { var tb = $("topbar"); if (tb) { tb.style.setProperty( "background", theme === "light" ? "#e9eef4" : "#0a0f14", "important", ); } } catch (e2) {} } function showToast(msg) { if (!els.toast) return; els.toast.textContent = msg; els.toast.classList.add("show"); clearTimeout(showToast._t); showToast._t = setTimeout(function () { els.toast.classList.remove("show"); }, 1600); } /** 系统返回 → 固定回首页(不退出) */ function handleAppBack() { goHomeFromHostBack(); return true; } function showView(name, opts) { if (!name) return; opts = opts || {}; var prev = state.view; lockInput(560); state.view = name; if (name !== "community") setOaModal(false); if (name !== "nav") { try { if (typeof setSheet === "function") setSheet(false); } catch (e0) {} } var views = ["home", "nav", "book", "community", "vip"]; for (var i = 0; i < views.length; i++) { var el = $("view-" + views[i]); if (!el) continue; if (views[i] === name) { addClass(el, "is-active"); el.style.display = "block"; } else { removeClass(el, "is-active"); el.style.display = "none"; } } try { window.scrollTo(0, 0); } catch (e) {} if (name === "book") { var bl = $("book-list"); if (bl) bl.innerHTML = ""; var br = $("book-reader"); if (br) { addClass(br, "is-hidden"); br.innerHTML = ""; } } if (name === "community") { var cl = $("community-list"); if (cl) cl.innerHTML = ""; } // 子页压栈:宿主返回触发 popstate → 回首页(禁止改 URL/hash,平台管控跳转) if (!opts.fromPop) { if (opts.replace || prev === name) replaceTrap(); else pushTrap(); if (name !== "home") pushTrap(); } else { replaceTrap(); } try { applySafeArea(); } catch (eSafe) {} setTimeout(function () { if (state.view !== name) return; if (name === "nav") renderNav(); if (name === "book") { if (!opts.keepBookMode) { state.bookMode = "cover"; state.bookChapter = null; } renderBook(); } if (name === "community") renderCommunity(); if (name === "vip") renderVip(); try { applySafeArea(); } catch (eSafe2) {} lockInput(280); }, 40); } // 内联 onclick 兜底:需经锁,避免连点/穿透 window.__nomadroGo = function (name) { if (isInputLocked() && state.view !== "home") return; showView(name); }; window.__nomadroBack = function () { goHomeFromHostBack(); pushTrap(); }; function initHistory() { try { replaceTrap(); pushTrap(); pushTrap(); } catch (e) {} window.addEventListener("popstate", function () { var consumed = goHomeFromHostBack(); // 子页回首页后立刻再压栈,避免下一次直接退出 setTimeout(function () { try { if (state.view !== "home" || consumed) { pushTrap(); if (state.view !== "home") pushTrap(); } } catch (e2) {} }, 0); }); function armTrap() { pushTrap(); if (state.view !== "home") pushTrap(); document.removeEventListener("touchend", armTrap, true); document.removeEventListener("click", armTrap, true); } try { document.addEventListener("touchend", armTrap, true); document.addEventListener("click", armTrap, true); } catch (e4) {} } function initParticles() { var canvas = $("particle-canvas"); if (!canvas || !canvas.getContext) return; var ctx = canvas.getContext("2d"); if (!ctx) return; var particles = []; var w = 0; var h = 0; var raf = 0; function resize() { w = canvas.width = window.innerWidth || 375; h = canvas.height = window.innerHeight || 667; var n = Math.min(48, Math.floor((w * h) / 24000)); particles = []; for (var i = 0; i < n; i++) { particles.push({ x: Math.random() * w, y: Math.random() * h, r: Math.random() * 1.6 + 0.4, vx: (Math.random() - 0.5) * 0.28, vy: (Math.random() - 0.5) * 0.28, a: Math.random() * 0.4 + 0.12, }); } } function draw() { ctx.clearRect(0, 0, w, h); for (var i = 0; i < particles.length; i++) { var p = particles[i]; p.x += p.vx; p.y += p.vy; if (p.x < 0 || p.x > w) p.vx *= -1; if (p.y < 0 || p.y > h) p.vy *= -1; ctx.beginPath(); ctx.arc(p.x, p.y, p.r, 0, Math.PI * 2); ctx.fillStyle = "rgba(255,255,255," + p.a + ")"; ctx.fill(); for (var j = i + 1; j < particles.length; j++) { var q = particles[j]; var dx = p.x - q.x; var dy = p.y - q.y; var d = Math.sqrt(dx * dx + dy * dy); if (d < 100) { ctx.beginPath(); ctx.moveTo(p.x, p.y); ctx.lineTo(q.x, q.y); ctx.strokeStyle = "rgba(78,205,196," + 0.14 * (1 - d / 100) + ")"; ctx.stroke(); } } } raf = window.requestAnimationFrame ? requestAnimationFrame(draw) : setTimeout(draw, 33); } resize(); draw(); try { window.addEventListener("resize", resize, { passive: true }); } catch (e) { window.addEventListener("resize", resize); } } function hideLoader() { var loader = $("hub-loader"); if (!loader) return; loader.classList.add("hidden"); loader.style.cssText = "display:none!important;pointer-events:none!important;visibility:hidden!important;"; try { if (loader.parentNode) loader.parentNode.removeChild(loader); } catch (e) {} } /* ----- 首页(对齐微信 hub:保留 hub-portal-card,不拆成别的类) ----- */ function renderHome() { var brand = cfg.homeBrand || "nomadro"; if (els.homeBrand) els.homeBrand.textContent = brand; var mods = cfg.modules || {}; // 与微信 src/pages/index PORTALS 完全一致(默认无 VIP 入口) var order = [ { key: "nav", emoji: "🧭", title: "出海导航", main: false }, { key: "book", emoji: "📖", title: "数字游民", main: false }, { key: "community", emoji: "🌏", title: "旅居社群", main: true }, ]; var html = ""; for (var i = 0; i < order.length; i++) { var o = order[i]; var m = mods[o.key] || {}; if (m.enabled === false) continue; // 微信:cfg.modules[key].title || portal.name var title = m.title || o.title; html += '模块已关闭
'; bindPortalCards(); } function stopTypewriter(key) { if (twTimers[key]) { clearTimeout(twTimers[key]); twTimers[key] = null; } } function startTypewriter(elId, phrases, key) { stopTypewriter(key); var el = $(elId); if (!el || !phrases || !phrases.length) return; var phraseIdx = 0; var charIdx = 0; var deleting = false; function tick() { var current = phrases[phraseIdx] || ""; if (!deleting) { el.textContent = current.substring(0, charIdx + 1); charIdx++; if (charIdx >= current.length) { deleting = true; twTimers[key] = setTimeout(tick, 2400); return; } twTimers[key] = setTimeout(tick, 48); } else { el.textContent = current.substring(0, Math.max(0, charIdx - 1)); charIdx--; if (charIdx <= 0) { deleting = false; phraseIdx = (phraseIdx + 1) % phrases.length; } twTimers[key] = setTimeout(tick, 24); } } twTimers[key] = setTimeout(tick, 300); } function communityNavbar() { // 仅品牌回首页,不放第二颗 ←(宿主已有返回箭头) return ( '' + escapeHtml(vip.eyebrow || "nomadro") + '
' + escapeHtml(vip.lead || "") + "
' + escapeHtml(faq[f].a) + "
📦 小红书容器内请前往微信小程序 / 门户开通
开通请用微信小程序或 nomadro.cn/vip
暂无分类数据
'; } function renderNavContent() { if (!els.content) return; if (!categories.length) { els.content.innerHTML = '离线导航数据未打包。请重新 assemble。
'; return; } var html = ""; for (var i = 0; i < categories.length; i++) { var cat = categories[i]; html += '渲染模块未加载
'; return; } var chapters = book.chapters || []; var totalMins = 0; for (var t = 0; t < chapters.length; t++) { totalMins += chapterMinutes(chapters[t].body || chapters[t].content); } totalMins = Math.max(1, totalMins); var words = 0; for (var w = 0; w < chapters.length; w++) { words += String(chapters[w].body || chapters[w].content || "").length; } var html = []; html.push('' + escapeHtml(book.title || cover.title || "小册") + '
阅读中
点一下,直接进入正文