host-nav:跳转标记只用真实 localStorage(maternal-bridge 镜像层会换掉 window.localStorage)+ cookie 兜底

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
deploy
2026-11-09 15:38:14 +00:00
parent c189813d28
commit 1c18dedf92
18 changed files with 50 additions and 21 deletions

View File

@@ -34,14 +34,43 @@
u.searchParams.sort && u.searchParams.sort();
return u.pathname + u.search;
}
/* 11-09 产后恢复会话报:接了 maternal-bridge 的应用里 window.localStorage 被换成云端镜像(内存 Map),
标记写进镜像、新容器页读不到 → 误判成「直接打开」。这里只用浏览器真实的 localStorage:
加载时是原生的就抓住;已被换掉就从同源空白 iframe 里拿一份;标记另外再写一份 cookie 兜底。 */
var nat = null;
function isNative(x) { try { return !!x && Object.prototype.toString.call(x) === '[object Storage]'; } catch (e) { return false; } }
try { if (isNative(g.localStorage)) nat = g.localStorage; } catch (e) {}
function LS() {
if (nat) return nat;
try {
var f = document.createElement('iframe');
f.style.display = 'none'; f.setAttribute('aria-hidden', 'true');
(document.body || document.documentElement).appendChild(f);
if (isNative(f.contentWindow.localStorage)) nat = f.contentWindow.localStorage;
} catch (e) {}
return nat;
}
function getMark() {
try { var l = LS(), v = l && l.getItem(OK); if (v) return JSON.parse(v); } catch (e) {}
try { var m = document.cookie.match(/(?:^|;\s*)mbhost_open=([^;]*)/); if (m) return JSON.parse(decodeURIComponent(m[1])); } catch (e) {}
return null;
}
function setMark(v) {
try { var l = LS(); if (l) l.setItem(OK, v); } catch (e) {}
try { document.cookie = 'mbhost_open=' + encodeURIComponent(v) + ';path=/;max-age=30;SameSite=Lax'; } catch (e) {}
}
function clearMark() {
try { var l = LS(); if (l) l.removeItem(OK); } catch (e) {}
try { document.cookie = 'mbhost_open=;path=/;max-age=0'; } catch (e) {}
}
function isChild() { try { return sessionStorage.getItem(CK) === '1'; } catch (e) { return false; } }
(function () {
if (!inView()) return;
try {
var m = JSON.parse(localStorage.getItem(OK) || 'null');
if (m && Date.now() - m.at < 30000 && key(m.u) === key(location.href)) { sessionStorage.setItem(CK, '1'); localStorage.removeItem(OK); }
var m = getMark();
if (m && Date.now() - m.at < 30000 && key(m.u) === key(location.href)) { sessionStorage.setItem(CK, '1'); clearMark(); }
/* 11-09 用户:复制链接 / 分享直接打开的页面,返回首页就停在顶部,不要回到「自己上次点进笔记时」的首页位置 */
if (!isChild()) localStorage.removeItem(POS);
if (!isChild()) { var l = LS(); if (l) l.removeItem(POS); }
} catch (e) {}
})();
function toHome(fallback) {
@@ -72,7 +101,7 @@
try { c = clean(url); } catch (e) { return false; }
at = Date.now();
try { if (typeof H.onLeave === 'function') H.onLeave(); } catch (e) {}
try { localStorage.setItem(OK, JSON.stringify({ u: c, at: at })); } catch (e) {}
setMark(JSON.stringify({ u: c, at: at }));
m.navigateTo({
url: VIEW + '?url=' + encodeURIComponent(c),
fail: function () { at = 0; if (fallback) fallback(); else location.href = url; }