已打开页面标签栏改为按浏览器会话保留

之前存在 localStorage:跨浏览器重启也不会清,前几天点过的标签刷新后又出现
(用户反馈"没点过/刷新后其他标签又弹出来")。改成 sessionStorage:同一次浏览器
会话内刷新、来回切换都保留,关掉浏览器或另开窗口重新进来就是干净的;同时清掉
旧版本写在 localStorage 里的记录。
This commit is contained in:
renshen 2026-10-13 15:24:26 +08:00
parent add787edf5
commit a8a0c6d4c9

View File

@ -80,12 +80,21 @@
return;
}
const list = root.querySelector('[data-open-tabs-list]');
const storageKey = 'caiwu.open-pages.v1.' + (root.dataset.userId || 'anon');
const userId = root.dataset.userId || 'anon';
const storageKey = 'caiwu.open-pages.v1.' + userId;
// 只在"本次浏览器会话"里保留:同一个浏览器窗口内刷新/来回切换都在,
// 关掉浏览器或换一个窗口重新进来就是干净的,不会再把前几天的旧标签带出来。
const storage = window.sessionStorage;
const maxTabs = 15;
// 清理早期版本写在 localStorage 里的记录(那份会跨浏览器一直保留)。
try {
window.localStorage.removeItem(storageKey);
} catch (_) { /* 隐私模式下忽略 */ }
const read = () => {
try {
const parsed = JSON.parse(window.localStorage.getItem(storageKey) || '[]');
const parsed = JSON.parse(storage.getItem(storageKey) || '[]');
return Array.isArray(parsed) ? parsed.filter((item) => item && typeof item.url === 'string' && typeof item.path === 'string') : [];
} catch (_) {
return [];
@ -93,7 +102,7 @@
};
const save = (items) => {
try {
window.localStorage.setItem(storageKey, JSON.stringify(items));
storage.setItem(storageKey, JSON.stringify(items));
} catch (_) { /* 隐私模式下忽略 */ }
};