// ==UserScript== // @name Discourse Sidebar Feed Panel // @namespace https://github.com/YsLtr // @version 3.4.8 // @author YsLtr // @description 将 Discourse 原生侧边栏改造为信息流面板,支持分类筛选、已读/未读过滤、拖拽调整宽度 // @license MIT // @icon https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/discourse/24/148734_2.png // @match https://linux.do/* // @match https://www.nodeloc.com/* // @match https://forum.chrultrabook.com/* // @match https://community.openai.com/* // @match https://meta.discourse.org/* // @grant GM_addStyle // @grant GM_deleteValue // @grant GM_getValue // @grant GM_registerMenuCommand // @grant GM_setValue // @grant unsafeWindow // @run-at document-idle // ==/UserScript== (function() { "use strict"; var __defProp = Object.defineProperty; var __exportAll = (all, no_symbols) => { let target = {}; for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" }); return target; }; var I18N = { "zh-CN": { all: "全部", orderActivity: "最新活动", orderCreated: "最新发布", orderViews: "最多浏览", orderPosts: "最多回复", orderLikes: "最多点赞", orderOpLikes: "楼主点赞", periodAll: "全部", periodDaily: "每日", periodWeekly: "每周", periodMonthly: "每月", periodQuarterly: "每季", periodYearly: "每年", filterAll: "全部", filterUnseen: "未读", filterRead: "已读", hidePinned: "隐藏置顶", toggleTitle: "切换侧边栏信息流", refresh: "刷新", settings: "设置", incomingHintLabel: "新活动数量", incomingHintTip: "在刷新按钮中显示当前板块范围内的新活动候选数量。离开首屏时点击数量会先回到顶部,再应用这些候选。", autoSilentLabel: "自动静默刷新", autoSilentTip: "在最新活动视图且停留首屏时按间隔自动应用新话题;离开首屏后暂停,只累计候选。", hidePinnedTip: "只在最新活动视图加载首页时,排除列表顶部已读的置顶话题。新活动队列和加载更多不会应用这个设置。", silentIntervalLabel: "静默刷新间隔", silentIntervalTip: "单位为秒,最小为 0。设为 0 时,有新活动会立即静默应用;大于 0 时按倒计时批量应用。", autoRefreshLabel: "自动刷新", autoRefreshTip: "用于非最新活动的排序视图,停留首屏时按间隔重新拉取当前列表。不要设置太快,频繁请求可能触发站点速率限制。", autoRefreshIntervalLabel: "自动刷新间隔", autoRefreshIntervalTip: "单位为秒,最小为 1。到达间隔后刷新当前筛选和排序下的列表。", secondsSuffix: "s", helpSuffix: "说明", tabMore: "展开板块 / 排序", tabPanelTitle: "拖动板块调整顺序", close: "关闭", loading: "加载中...", emptyTopics: "暂无话题", loadFailed: "加载失败", retry: "重试", noMatchingTopics: "无匹配话题", noUnreadInCategory: "该板块暂无未读话题", noUnread: "暂无未读话题", noRead: "暂无已读话题", currentPagePrefix: "当前页", nextPageNoMatch: "下一页无符合条件的话题", applyIncoming: "应用 {count} 个新的或更新的话题", backToTop: "回到顶部", hot: "热门", pinned: "已置顶", topicUnavailable: "话题异常", topicUnavailableTip: "此话题已从服务器列表中消失,可能被取消公开、隐藏或删除。点击可自行确认实际情况。", closedTitle: "此话题已被关闭;不再接受新回复", newReplies: "{count} 条新回复", loadMore: "加载更多", noMore: "— 已经到底了 —", requestFailed: "请求失败", readLaterTitle: "稍后再读", readLaterDropHint: "拖进加入稍后再读", readLaterDropRelease: "松开加入稍后再读", linkPreview: "链接预览" }, en: { all: "All", orderActivity: "Latest activity", orderCreated: "Newest", orderViews: "Most viewed", orderPosts: "Most replies", orderLikes: "Most liked", orderOpLikes: "OP likes", periodAll: "All", periodDaily: "Daily", periodWeekly: "Weekly", periodMonthly: "Monthly", periodQuarterly: "Quarterly", periodYearly: "Yearly", filterAll: "All", filterUnseen: "Unread", filterRead: "Read", hidePinned: "Hide pinned", toggleTitle: "Toggle sidebar feed", refresh: "Refresh", settings: "Settings", incomingHintLabel: "New activity count", incomingHintTip: "Show incoming candidates for the current category scope in the refresh button. Away from the head, clicking the count returns to the top before applying them.", autoSilentLabel: "Auto silent refresh", autoSilentTip: "In the latest activity view, automatically apply incoming topics while the feed is at the head. Away from the head, pause applying and keep accumulating candidates.", hidePinnedTip: "Only in the latest activity view, exclude read pinned topics at the top when loading the first page. Incoming topics and load more are not affected.", silentIntervalLabel: "Silent interval", silentIntervalTip: "Seconds. Minimum 0. With 0, incoming activity is applied immediately; otherwise candidates are batched by countdown.", autoRefreshLabel: "Auto refresh", autoRefreshTip: "For non-latest sorting views, re-fetch the current list while the feed is at the head. Avoid very short intervals to reduce rate-limit risk.", autoRefreshIntervalLabel: "Refresh interval", autoRefreshIntervalTip: "Seconds. Minimum 1. Refreshes the current filter and sorting view when the interval elapses.", secondsSuffix: "s", helpSuffix: " help", tabMore: "Expand categories / order", tabPanelTitle: "Drag categories to reorder", close: "Close", loading: "Loading...", emptyTopics: "No topics", loadFailed: "Load failed", retry: "Retry", noMatchingTopics: "No matching topics", noUnreadInCategory: "No unread topics in this category", noUnread: "No unread topics", noRead: "No read topics", currentPagePrefix: "Current page: ", nextPageNoMatch: "Next page has no matching topics", applyIncoming: "Apply {count} new or updated topics", backToTop: "Back to top", hot: "Hot", pinned: "Pinned", topicUnavailable: "Unavailable", topicUnavailableTip: "This topic disappeared from the server list; it may be unlisted, hidden, or deleted. Click to check what happened.", closedTitle: "This topic is closed; it no longer accepts replies", newReplies: "{count} new replies", loadMore: "Load more", noMore: "No more topics", requestFailed: "Request failed", readLaterTitle: "Read later", readLaterDropHint: "Drag into Read later", readLaterDropRelease: "Release to add to Read later", linkPreview: "Link preview" } }; function createI18n(getDiscourse) { function getUiLocale() { const language = [ document.documentElement?.getAttribute("lang"), document.documentElement?.getAttribute("xml:lang"), getDiscourse()?.SiteSettings?.default_locale, navigator.language ].find((value) => String(value || "").trim()) || "en"; return /^zh/i.test(language) ? "zh-CN" : "en"; } function t(key, params = {}) { const template = (I18N[getUiLocale()] || I18N.en)[key] || I18N.en[key] || key; return String(template).replace(/\{(\w+)\}/g, (_, name) => { return params[name] === void 0 ? `{${name}}` : String(params[name]); }); } function formatRelativeTime(dateStr) { const date = new Date(dateStr || ""); if (Number.isNaN(date.getTime())) return ""; const diff = Math.max(0, new Date().getTime() - date.getTime()); const seconds = Math.floor(diff / 1e3); const minutes = Math.floor(seconds / 60); const hours = Math.floor(minutes / 60); const days = Math.floor(hours / 24); if (getUiLocale() !== "zh-CN") { const rtf = new Intl.RelativeTimeFormat("en", { numeric: "auto" }); if (seconds < 60) return rtf.format(-Math.max(1, seconds), "second"); if (minutes < 60) return rtf.format(-minutes, "minute"); if (hours < 24) return rtf.format(-hours, "hour"); if (days < 30) return rtf.format(-days, "day"); const months = Math.floor(days / 30); if (months < 12) return rtf.format(-months, "month"); return rtf.format(-Math.floor(months / 12), "year"); } if (seconds < 60) return `${Math.max(1, seconds)}秒前`; if (minutes < 60) return `${minutes}分钟前`; if (hours < 24) return `${hours}小时前`; if (days < 30) return `${days}天前`; const months = Math.floor(days / 30); if (months < 12) return `${months}个月前`; return `${Math.floor(months / 12)}年前`; } return { getUiLocale, t, formatRelativeTime }; } var AUTO_LOAD_RATE_WINDOW_MS = 5e3; var READ_LATER_ONBOX_RETRY_MS = 2e3; var Lifetime = class { cleanups = new Set(); timeouts = new Set(); frames = new Set(); ended = false; aborter = new AbortController(); get disposed() { return this.ended; } get signal() { return this.aborter.signal; } defer(cleanup) { if (this.ended) { cleanup(); return () => {}; } this.cleanups.add(cleanup); return () => { this.cleanups.delete(cleanup); }; } listen(target, type, fn, options) { if (this.ended) return () => {}; target.addEventListener(type, fn, options); const cleanup = () => target.removeEventListener(type, fn, options); const forget = this.defer(cleanup); return () => { cleanup(); forget(); }; } timeout(fn, ms) { if (this.ended) return 0; const id = window.setTimeout(() => { this.timeouts.delete(id); if (!this.ended) fn(); }, ms); this.timeouts.add(id); return id; } clearTimeout(id) { window.clearTimeout(id); this.timeouts.delete(id); } frame(fn) { if (this.ended) return 0; const id = window.requestAnimationFrame((time) => { this.frames.delete(id); if (!this.ended) fn(time); }); this.frames.add(id); return id; } cancelFrame(id) { window.cancelAnimationFrame(id); this.frames.delete(id); } dispose() { if (this.ended) return; this.ended = true; this.aborter.abort(); for (const cleanup of [...this.cleanups].reverse()) cleanup(); this.cleanups.clear(); for (const id of this.timeouts) window.clearTimeout(id); for (const id of this.frames) window.cancelAnimationFrame(id); this.timeouts.clear(); this.frames.clear(); } }; function createRouteWatcher(callbacks) { let stopCurrent; function stop() { stopCurrent?.(); stopCurrent = void 0; } function start() { if (stopCurrent) return; let lastUrl = location.href; let active = true; const checkUrl = () => { if (!active || location.href === lastUrl) return; lastUrl = location.href; callbacks.onRouteChange(); }; const originalPush = history.pushState; const originalReplace = history.replaceState; const push = function(...args) { originalPush.apply(this, args); checkUrl(); }; const replace = function(...args) { originalReplace.apply(this, args); checkUrl(); }; history.pushState = push; history.replaceState = replace; window.addEventListener("popstate", checkUrl); const observer = new MutationObserver(() => { if (!active) return; checkUrl(); callbacks.onMutation(); }); observer.observe(document.body, { childList: true, subtree: true }); stopCurrent = () => { active = false; observer.disconnect(); window.removeEventListener("popstate", checkUrl); if (history.pushState === push) history.pushState = originalPush; if (history.replaceState === replace) history.replaceState = originalReplace; }; } return { start, stop }; } var READ_LATER_STORAGE_KEY = "sfp_read_later_v1"; var READ_LATER_HEIGHT_STORAGE_KEY = "sfp_read_later_height_v1"; function collapse(value) { return String(value || "").replace(/\s+/g, " ").trim(); } function truncateText(value, max) { return value.length > max ? `${value.slice(0, Math.max(0, max - 1))}…` : value; } function parseReadLaterLink(raw, origin) { const value = String(raw || "").trim(); if (!value) return null; let url; try { url = new URL(value, `${origin}/`); } catch { return null; } if (url.origin !== origin) return null; if (!/^https?:$/.test(url.protocol)) return null; const path = url.pathname; if (path === "/" && !url.search) return null; let topicId; const segments = path.split("/").filter(Boolean); if (segments[0] === "t" && segments.length >= 2) { const first = Number(segments[1]); if (Number.isInteger(first) && first > 0) topicId = first; else { const second = Number(segments[2]); if (Number.isInteger(second) && second > 0) topicId = second; } } const relative = `${path}${url.search}${url.hash}`; return topicId ? { url: relative, kind: "topic", topicId } : { url: relative, kind: "link" }; } function readLaterFallbackTitle(url) { const segments = String(url || "").split(/[?#]/)[0].split("/").filter(Boolean); return segments.length ? decodeURIComponent(segments[segments.length - 1]) : url; } function validTopic(value) { if (!value || typeof value !== "object") return void 0; const topic = value; return Number.isFinite(topic.id) ? topic : void 0; } function validUser(value) { if (!value || typeof value !== "object") return void 0; const user = value; return Number.isFinite(user.id) ? user : void 0; } function validPreview(value) { if (!value || typeof value !== "object") return void 0; const preview = value; const result = {}; if (typeof preview.title === "string" && preview.title) result.title = preview.title; if (typeof preview.name === "string" && preview.name) result.name = preview.name; if (typeof preview.excerpt === "string" && preview.excerpt) result.excerpt = preview.excerpt; if (typeof preview.imageUrl === "string" && /^https?:\/\//i.test(preview.imageUrl)) result.imageUrl = preview.imageUrl; return Object.keys(result).length ? result : void 0; } function normalizeItem(value, origin) { if (!value || typeof value !== "object") return null; const raw = value; if (typeof raw.url !== "string") return null; const parsed = parseReadLaterLink(raw.url, origin); if (!parsed) return null; const addedAt = Number(raw.addedAt); if (!Number.isFinite(addedAt)) return null; return { url: parsed.url, kind: parsed.kind, topicId: parsed.topicId, addedAt, title: typeof raw.title === "string" ? raw.title : "", topic: validTopic(raw.topic), user: validUser(raw.user), preview: validPreview(raw.preview) }; } function resolveReadLaterStorage() { try { const probe = "__sfp_read_later_probe__"; localStorage.setItem(probe, "1"); localStorage.removeItem(probe); return localStorage; } catch { const memory = new Map(); return { getItem: (key) => memory.get(key) ?? null, setItem: (key, value) => void memory.set(key, value), removeItem: (key) => void memory.delete(key) }; } } function createReadLaterStore({ storage, origin, now = () => Date.now(), limit = 200 }) { let memoryOnly = false; let items = []; let height = null; function readItems() { let raw = null; try { raw = storage.getItem(READ_LATER_STORAGE_KEY); } catch { memoryOnly = true; return []; } if (!raw) return []; let parsed; try { parsed = JSON.parse(raw); } catch { return []; } const file = parsed; if (!file || typeof file !== "object") return []; const legacy = file.version === 1; if (!legacy && file.version !== 2) return []; if (!Array.isArray(file.items)) return []; const result = []; for (const entry of file.items) { const item = normalizeItem(entry, origin); if (item) result.push(item); } if (legacy) result.reverse(); return result.slice(0, limit); } function readHeight() { try { const raw = storage.getItem(READ_LATER_HEIGHT_STORAGE_KEY); if (!raw) return null; const value = Number(raw); return Number.isFinite(value) && value > 0 ? value : null; } catch { return null; } } function writeItems(next) { if (memoryOnly) return next; const write = (list) => storage.setItem(READ_LATER_STORAGE_KEY, JSON.stringify({ version: 2, items: list })); try { write(next); return next; } catch (error) { const trimmed = next.slice(0, Math.ceil(next.length / 2)); try { write(trimmed); return trimmed; } catch { memoryOnly = true; console.warn("[SFP] read later storage unavailable:", error); return next; } } } items = readItems(); height = readHeight(); return { items: () => items, height: () => height, setHeight(value) { height = value; try { if (value === null) storage.removeItem(READ_LATER_HEIGHT_STORAGE_KEY); else storage.setItem(READ_LATER_HEIGHT_STORAGE_KEY, String(value)); } catch (error) { console.warn("[SFP] read later height not persisted:", error); } }, add(item) { const existing = items.find((entry) => entry.url === item.url); items = writeItems([{ ...existing, ...item, addedAt: item.addedAt ?? now(), preview: item.preview ?? existing?.preview, topic: item.topic ?? existing?.topic, user: item.user ?? existing?.user }, ...items.filter((other) => other.url !== item.url)].slice(0, limit)); return items; }, move(url, toIndex) { const from = items.findIndex((item) => item.url === url); if (from < 0) return items; const next = items.slice(); const [moved] = next.splice(from, 1); const target = Math.max(0, Math.min(toIndex, next.length)); next.splice(target, 0, moved); items = writeItems(next); return items; }, remove(url) { items = writeItems(items.filter((item) => item.url !== url)); return items; }, update(next) { items = writeItems(next.slice(0, limit)); return items; }, reload() { items = readItems(); height = readHeight(); return items; } }; } function mergeTopicSnapshot(items, topics, users, selectTitle) { if (!items.length || !topics.length) return null; const byId = new Map(topics.map((topic) => [topic.id, topic])); const userById = new Map(users.map((user) => [user.id, user])); let changed = false; const next = items.map((item) => { const fresh = item.topicId === void 0 ? void 0 : byId.get(item.topicId); if (!fresh) return item; changed = true; const posterId = fresh.posters?.[0]?.user_id; return { ...item, topic: fresh, user: (posterId === void 0 ? void 0 : userById.get(posterId)) || item.user, title: selectTitle(fresh) || item.title }; }); return changed ? next : null; } function cleanText(value) { return collapse(value); } function textOf(value) { return typeof value === "string" && value ? value : void 0; } function numOf(value) { const parsed = Number(value); return Number.isFinite(parsed) ? parsed : void 0; } function topicFromView(value) { if (!value || typeof value !== "object") return null; const view = value; const id = numOf(view.id); if (!id || id <= 0) return null; const createdBy = view.details?.created_by; const firstPost = view.post_stream?.posts?.[0]; const rawUser = createdBy || firstPost; const userId = numOf(createdBy?.id ?? firstPost?.user_id); const user = userId === void 0 ? void 0 : { id: userId, username: textOf(rawUser?.username), name: textOf(rawUser?.name), avatar_template: textOf(rawUser?.avatar_template) }; return { topic: { id, slug: textOf(view.slug), title: textOf(view.title), fancy_title: textOf(view.fancy_title), category_id: numOf(view.category_id), archetype: textOf(view.archetype), created_at: textOf(view.created_at), last_posted_at: textOf(view.last_posted_at), views: numOf(view.views), posts_count: numOf(view.posts_count), like_count: numOf(view.like_count), last_read_post_number: numOf(view.last_read_post_number) ?? null, highest_post_number: numOf(view.highest_post_number), closed: view.closed === true || void 0, pinned: view.pinned === true || void 0, pinned_globally: view.pinned_globally === true || void 0, tags: Array.isArray(view.tags) ? view.tags.filter((tag) => typeof tag === "string" || !!tag && typeof tag === "object") : void 0, posters: user ? [{ user_id: user.id }] : void 0 }, user }; } function extractPreview(root, origin) { const box = root.querySelector("aside, .onebox"); if (!box) return {}; const rawImage = box.querySelector("img")?.getAttribute("src") || ""; let imageUrl; if (rawImage) try { const absolute = new URL(rawImage, `${origin}/`).href; if (/^https?:\/\//i.test(absolute)) imageUrl = absolute; } catch { imageUrl = void 0; } const titleEl = box.querySelector(".quote-title__text-content a") || box.querySelector(".onebox-body h3 a") || box.querySelector("a"); const nameEl = box.querySelector(".full-name"); const excerptEl = box.querySelector("blockquote") || box.querySelector(".onebox-body p") || box.querySelector(".description p"); const title = cleanText(titleEl?.textContent); const name = cleanText(nameEl?.textContent); const preview = { title: title || void 0, name: name && name !== title ? name : void 0, excerpt: truncateText(cleanText(excerptEl?.textContent), 200) || void 0, imageUrl }; if (!preview.title && !preview.name && !preview.excerpt && !preview.imageUrl) return {}; return preview; } function extractOpenGraph(root, origin) { const meta = (property) => { return cleanText(root.querySelector(`meta[property="${property}"]`)?.getAttribute("content")); }; const rawImage = meta("og:image"); let imageUrl; if (rawImage) try { const absolute = new URL(rawImage, `${origin}/`).href; if (/^https?:\/\//i.test(absolute)) imageUrl = absolute; } catch { imageUrl = void 0; } const preview = { title: meta("og:title") || cleanText(root.querySelector("title")?.textContent) || void 0, excerpt: truncateText(meta("og:description"), 200) || void 0, imageUrl }; if (!preview.title && !preview.excerpt && !preview.imageUrl) return {}; return preview; } var storage_keys_exports = __exportAll({ AUTO_REFRESH_ENABLED_KEY: () => AUTO_REFRESH_ENABLED_KEY, AUTO_REFRESH_INTERVAL_KEY: () => AUTO_REFRESH_INTERVAL_KEY, AUTO_SILENT_REFRESH_INTERVAL_KEY: () => AUTO_SILENT_REFRESH_INTERVAL_KEY, AUTO_SILENT_REFRESH_KEY: () => AUTO_SILENT_REFRESH_KEY, CATEGORY_DATA_CACHE_KEY: () => CATEGORY_DATA_CACHE_KEY, FILTER_KEY: () => FILTER_KEY, HIDE_PINNED_KEY: () => HIDE_PINNED_KEY, LEGACY_LINUXDO_ORIGIN: () => LEGACY_LINUXDO_ORIGIN, ORDER_KEY: () => ORDER_KEY, PERIOD_KEY: () => PERIOD_KEY, SHOW_INCOMING_HINT_KEY: () => SHOW_INCOMING_HINT_KEY, SITE_SCOPED_STORAGE_KEYS: () => SITE_SCOPED_STORAGE_KEYS, SITE_STORAGE_PREFIX: () => SITE_STORAGE_PREFIX, STATE_KEY: () => STATE_KEY, STORAGE_MISSING: () => STORAGE_MISSING, TAB_KEY: () => TAB_KEY, TAB_ORDER_KEY: () => TAB_ORDER_KEY, TAG_STYLE_CACHE_KEY: () => TAG_STYLE_CACHE_KEY, WIDTH_KEY: () => WIDTH_KEY }); var STATE_KEY = "sfp_feed_mode_enabled"; var ORDER_KEY = "sfp_current_order"; var PERIOD_KEY = "sfp_current_period"; var WIDTH_KEY = "sfp_sidebar_width"; var TAB_KEY = "sfp_current_tab"; var TAB_ORDER_KEY = "sfp_tab_order"; var FILTER_KEY = "sfp_current_filter"; var HIDE_PINNED_KEY = "sfp_activity_hide_pinned"; var SHOW_INCOMING_HINT_KEY = "sfp_show_incoming_hint"; var AUTO_SILENT_REFRESH_KEY = "sfp_auto_silent_refresh"; var AUTO_SILENT_REFRESH_INTERVAL_KEY = "sfp_auto_silent_refresh_interval"; var AUTO_REFRESH_ENABLED_KEY = "sfp_auto_refresh_enabled"; var AUTO_REFRESH_INTERVAL_KEY = "sfp_auto_refresh_interval"; var CATEGORY_DATA_CACHE_KEY = "sfp_category_data_cache_v1"; var TAG_STYLE_CACHE_KEY = "sfp_tag_style_cache_v1"; var SITE_STORAGE_PREFIX = "sfp_site"; var LEGACY_LINUXDO_ORIGIN = "https://linux.do"; var STORAGE_MISSING = "__SFP_STORAGE_MISSING__"; var SITE_SCOPED_STORAGE_KEYS = [ STATE_KEY, ORDER_KEY, PERIOD_KEY, WIDTH_KEY, TAB_KEY, TAB_ORDER_KEY, FILTER_KEY, HIDE_PINNED_KEY, SHOW_INCOMING_HINT_KEY, AUTO_SILENT_REFRESH_KEY, AUTO_SILENT_REFRESH_INTERVAL_KEY, AUTO_REFRESH_ENABLED_KEY, AUTO_REFRESH_INTERVAL_KEY, CATEGORY_DATA_CACHE_KEY, TAG_STYLE_CACHE_KEY ]; function buildCategoryPath(categoryId, getCategoryMeta) { const targetCategoryId = Number(categoryId); if (!Number.isFinite(targetCategoryId)) return ""; const chain = []; const seen = new Set(); let meta = getCategoryMeta(targetCategoryId); while (meta && !seen.has(meta.id)) { seen.add(meta.id); chain.unshift(meta); const parentId = Number(meta.parent_category_id); if (!Number.isFinite(parentId) || parentId === meta.id) break; meta = getCategoryMeta(parentId); } const slugs = chain.map((cat) => cat.slug).filter((slug) => !!slug); if (slugs.length === 0) slugs.push("category"); return `/c/${slugs.map((slug) => encodeURIComponent(slug)).join("/")}/${targetCategoryId}`; } function _needsPeriodForUrl(order) { return [ "views", "posts", "likes", "op_likes" ].includes(order); } function _usesPeriodScopedTopList(order, period) { return period !== "all" && _needsPeriodForUrl(order); } function feedQueryKey(query) { return [ query.tab, query.categoryId || "", query.order, query.period, query.filter ].join("|"); } function buildFeedUrl(query, page, getCategoryMeta) { const useTopList = _usesPeriodScopedTopList(query.order, query.period); if (query.tab !== "all" && query.categoryId) { const categoryPath = buildCategoryPath(query.categoryId, getCategoryMeta); const params = [`page=${page}`, "include_subcategories=true"]; if (useTopList) { params.push(`period=${encodeURIComponent(query.period)}`); params.push(`order=${encodeURIComponent(query.order)}`); return `${categoryPath}/l/top.json?${params.join("&")}`; } params.push(`order=${encodeURIComponent(query.order)}`); return `${categoryPath}/l/latest.json?${params.join("&")}`; } if (useTopList) return `/top.json?${[ `period=${encodeURIComponent(query.period)}`, `order=${encodeURIComponent(query.order)}`, `page=${page}` ].join("&")}`; const params = [`order=${encodeURIComponent(query.order)}`, `page=${page}`]; if (query.period !== "all" && _needsPeriodForUrl(query.order)) params.push(`period=${encodeURIComponent(query.period)}`); return `/latest.json?${params.join("&")}`; } function objectPayload(value) { return value !== null && typeof value === "object" && !Array.isArray(value) ? value : null; } function errorMessage(error) { return error instanceof Error ? error.message : String(error); } var SAFE_ICON_RE = /^[A-Za-z0-9_-]+$/; var SAFE_COLOR_RE = /^#?[A-Fa-f0-9]{3,8}$/; function _normalizeHexColor(color, fallback = "888") { if (!color) return fallback; const raw = String(color).trim(); if (!SAFE_COLOR_RE.test(raw)) return fallback; return raw.startsWith("#") ? raw.slice(1) : raw; } function _isSafeIconName(icon) { return typeof icon === "string" && SAFE_ICON_RE.test(icon); } function _safeIconName(icon) { return _isSafeIconName(icon) ? icon : ""; } function _safeCategoryStyleType(styleType, hasIcon) { return [ "icon", "emoji", "square" ].includes(styleType || "") ? styleType : hasIcon ? "icon" : "square"; } function withLocaleEntry(locales, locale, entry) { const next = { ...locales, [locale]: entry }; const keys = Object.keys(next); if (keys.length <= 2) return next; keys.sort((a, b) => (next[a].savedAt || 0) - (next[b].savedAt || 0)).slice(0, keys.length - 2).forEach((key) => delete next[key]); return next; } var ORDER_OPTION_DEFS = [ { labelKey: "orderActivity", value: "activity", capability: "latest" }, { labelKey: "orderCreated", value: "created", capability: "new" }, { labelKey: "orderViews", value: "views", capability: "top" }, { labelKey: "orderPosts", value: "posts", capability: "top" }, { labelKey: "orderLikes", value: "likes", capability: "top" }, { labelKey: "orderOpLikes", value: "op_likes", capability: "top" } ]; var PERIOD_OPTION_DEFS = [ { labelKey: "periodAll", value: "all" }, { labelKey: "periodDaily", value: "daily" }, { labelKey: "periodWeekly", value: "weekly" }, { labelKey: "periodMonthly", value: "monthly" }, { labelKey: "periodQuarterly", value: "quarterly" }, { labelKey: "periodYearly", value: "yearly" } ]; var FILTER_OPTION_DEFS = [ { labelKey: "filterAll", value: "all" }, { labelKey: "filterUnseen", value: "unseen" }, { labelKey: "filterRead", value: "read" } ]; function createSiteData({ storage, getCsrfToken, getPageLocale, onChange }) { let scope = new Lifetime(); const _getSiteValue = storage.get; const _setSiteValue = storage.set; const categoryMetaById = new Map(); let tabCategories = []; let siteCapabilities = _createDefaultSiteCapabilities(); let categoryMetaPromise = null; let categoryMetaLoaded = false; let siteDataPromise = null; let siteDataLoaded = false; let siteDataCache = null; let categoriesAndLatestPromise = null; let categoriesAndLatestLoaded = false; let categoriesAndLatestCache = null; function _createDefaultSiteCapabilities() { return { orderValues: new Set(ORDER_OPTION_DEFS.map((option) => option.value)), periodValues: new Set(PERIOD_OPTION_DEFS.map((option) => option.value)), filterValues: new Set(FILTER_OPTION_DEFS.map((option) => option.value)), rawFilters: new Set(), rawTopMenuItems: new Set() }; } function _stringSet(values) { return new Set((Array.isArray(values) ? values : []).map((value) => String(value || "").trim()).filter(Boolean)); } function _updateSiteCapabilities(site) { const rawFilters = _stringSet(site?.filters); const rawTopMenuItems = _stringSet(site?.top_menu_items); _stringSet(site?.anonymous_top_menu_items).forEach((item) => rawTopMenuItems.add(item)); rawTopMenuItems.forEach((item) => rawFilters.add(item)); const supportsLatest = rawFilters.size === 0 || rawFilters.has("latest"); const supportsNew = rawFilters.has("new"); const supportsTop = rawFilters.has("top"); const orderValues = new Set(); if (supportsLatest) orderValues.add("activity"); if (supportsNew || supportsLatest) orderValues.add("created"); if (supportsTop) [ "views", "posts", "likes", "op_likes" ].forEach((order) => orderValues.add(order)); if (orderValues.size === 0) orderValues.add("activity"); const sitePeriods = _stringSet(site?.periods); const periodValues = sitePeriods.size > 0 ? new Set(PERIOD_OPTION_DEFS.map((option) => option.value).filter((value) => sitePeriods.has(value))) : new Set(PERIOD_OPTION_DEFS.map((option) => option.value)); if (periodValues.size === 0) periodValues.add("all"); const filterValues = new Set(["all"]); if (rawFilters.has("unseen") || rawFilters.has("unread") || rawFilters.has("new")) filterValues.add("unseen"); if (rawFilters.has("read")) filterValues.add("read"); siteCapabilities = { orderValues, periodValues, filterValues, rawFilters, rawTopMenuItems }; } function _categoryTabId(id) { return `cat-${Number(id)}`; } function _findTabCategoryByTabId(tabId) { return tabCategories.find((cat) => { return cat.tabId === tabId || cat.legacyTabIds?.includes(tabId); }) || null; } function _categoryIdFromPath(value) { if (!value || typeof value !== "string") return null; let path = value; try { path = new URL(value, location.origin).pathname; } catch (e) { path = value.split("?")[0].split("#")[0]; } const parts = path.split("/").filter(Boolean); if (parts[0] !== "c") return null; for (let index = parts.length - 1; index >= 1; index--) { const id = Number(parts[index]); if (Number.isInteger(id) && id > 0) return id; } return null; } function _categoryListCategories(data) { return Array.isArray(data?.category_list?.categories) ? data.category_list.categories : []; } function _mergeCategoryRecords(...categoryLists) { const rawById = new Map(); categoryLists.forEach((categoryList) => { if (!Array.isArray(categoryList)) return; categoryList.forEach((cat) => { const id = Number(cat?.id); if (!Number.isFinite(id)) return; rawById.set(id, { ...rawById.get(id) || {}, ...cat, id }); }); }); return Array.from(rawById.values()); } function _collectNavigationCategoryIds(site, rawById, categoryListCategories = []) { const ids = []; const seen = new Set(); const addId = (value) => { const id = Number(value); if (!Number.isInteger(id) || id <= 0 || !rawById.has(id) || seen.has(id)) return; seen.add(id); ids.push(id); }; const addRecord = (record) => { if (!record) return; if (typeof record === "number" || typeof record === "string") { addId(record); const pathId = _categoryIdFromPath(String(record)); if (pathId) addId(pathId); return; } if (typeof record !== "object") return; addId(record.category_id ?? record.categoryId ?? record.category?.id); if ((String(record.type || record.section_type || "").toLowerCase() === "category" || record.topic_count !== void 0 || record.parent_category_id !== void 0 || record.read_restricted !== void 0) && record.id !== void 0) addId(record.id); [ record.value, record.url, record.href, record.path, record.link, record.route ].forEach((path) => { const pathId = _categoryIdFromPath(path); if (pathId) addId(pathId); }); }; const addRecords = (records) => { if (!Array.isArray(records)) return; records.forEach(addRecord); }; [ site?.navigation_menu_categories, site?.navigation_menu_site_categories, site?.default_navigation_menu_categories, site?.anonymous_default_navigation_menu_categories ].forEach(addRecords); [ site?.anonymous_sidebar_sections, site?.sidebar_sections, site?.navigation_menu_sections ].forEach((sections) => { if (!Array.isArray(sections)) return; sections.forEach((section) => { addRecord(section); addRecords(section?.links); }); }); if (ids.length === 0) addRecords(categoryListCategories); return ids; } function _buildTabCategories(site, navigationCategories, rawById) { return _collectNavigationCategoryIds(site, rawById, navigationCategories).map((id) => _getCategoryMeta(id)).filter((meta) => !!meta?.name).map((meta) => ({ id: meta.id, tabId: _categoryTabId(meta.id), legacyTabIds: [meta.slug].filter(Boolean), name: meta.name, icon: meta.icon, color: `#${meta.color}`, slug: meta.slug })); } function _normalizeCategoryMeta(raw = {}, parent = null) { const id = Number(raw.id); const icon = _safeIconName(raw.icon || ""); return { id, name: raw.name || "", color: _normalizeHexColor(raw.color, "888"), text_color: _normalizeHexColor(raw.text_color, "FFFFFF"), icon, style_type: _safeCategoryStyleType(raw.style_type, !!icon), slug: raw.slug || "", parent_category_id: raw.parent_category_id || null, parent_color: parent ? _normalizeHexColor(parent.color, "888") : null, parent_text_color: parent ? _normalizeHexColor(parent.text_color, "FFFFFF") : null, read_restricted: !!raw.read_restricted, description_text: raw.description_text || raw.description_excerpt || raw.description || "", description_excerpt: raw.description_excerpt || raw.description_text || raw.description || "" }; } function _getCategoryMeta(id) { const numericId = Number(id); if (!Number.isFinite(numericId)) return null; return categoryMetaById.get(numericId) || null; } function _cloneJsonArray(value) { if (!Array.isArray(value)) return []; try { return JSON.parse(JSON.stringify(value)); } catch (e) { return []; } } function _createCategoryDataCacheSource(site = {}, navigationCategories = []) { return { site: { categories: _cloneJsonArray(site?.categories), filters: _cloneJsonArray(site?.filters), periods: _cloneJsonArray(site?.periods), top_menu_items: _cloneJsonArray(site?.top_menu_items), anonymous_top_menu_items: _cloneJsonArray(site?.anonymous_top_menu_items), top_tags: _cloneJsonArray(site?.top_tags), can_tag_topics: site?.can_tag_topics, navigation_menu_categories: _cloneJsonArray(site?.navigation_menu_categories), navigation_menu_site_categories: _cloneJsonArray(site?.navigation_menu_site_categories), default_navigation_menu_categories: _cloneJsonArray(site?.default_navigation_menu_categories), anonymous_default_navigation_menu_categories: _cloneJsonArray(site?.anonymous_default_navigation_menu_categories), anonymous_sidebar_sections: _cloneJsonArray(site?.anonymous_sidebar_sections), sidebar_sections: _cloneJsonArray(site?.sidebar_sections), navigation_menu_sections: _cloneJsonArray(site?.navigation_menu_sections) }, navigationCategories: _cloneJsonArray(navigationCategories) }; } function _applyCategoryDataSource(source, { primeSiteData = false } = {}) { const site = source?.site || {}; const navigationCategories = Array.isArray(source?.navigationCategories) ? source.navigationCategories : []; const categories = _mergeCategoryRecords(site?.categories, navigationCategories); const rawById = new Map(categories.map((cat) => [Number(cat.id), cat])); _updateSiteCapabilities(site); categoryMetaById.clear(); categories.forEach((cat) => { const id = Number(cat.id); if (!Number.isFinite(id)) return; const parent = cat.parent_category_id ? rawById.get(Number(cat.parent_category_id)) : null; categoryMetaById.set(id, _normalizeCategoryMeta(cat, parent)); }); tabCategories = _buildTabCategories(site, navigationCategories, rawById); onChange(); if (primeSiteData && !siteDataLoaded) { siteDataCache = site; siteDataLoaded = true; } categoryMetaLoaded = true; } function _loadCategoryDataCache() { try { const cache = _getSiteValue(CATEGORY_DATA_CACHE_KEY, null); const entry = cache?.locales?.[getPageLocale()]; if (!cache || cache.version !== 1 || !entry?.source) return null; const source = entry.source; if (!source.site || !Array.isArray(source.site.categories) || !Array.isArray(source.navigationCategories)) return null; return source; } catch (e) { console.warn("[SFP] load category data cache failed:", e); return null; } } function _saveCategoryDataCache(source) { try { const cached = _getSiteValue(CATEGORY_DATA_CACHE_KEY, null); _setSiteValue(CATEGORY_DATA_CACHE_KEY, { version: 1, locales: withLocaleEntry(cached?.version === 1 ? cached.locales : void 0, getPageLocale(), { savedAt: Date.now(), source }) }); } catch (e) { console.warn("[SFP] save category data cache failed:", e); } } function _resetRuntimeCategoryData() { scope.dispose(); scope = new Lifetime(); categoryMetaPromise = null; categoryMetaLoaded = false; categoryMetaById.clear(); tabCategories = []; siteDataPromise = null; siteDataLoaded = false; siteDataCache = null; categoriesAndLatestPromise = null; categoriesAndLatestLoaded = false; categoriesAndLatestCache = null; } function getCategoryTabMeta(cat) { const meta = _getCategoryMeta(cat.id); return { ...cat, name: meta?.name || cat.name, icon: meta?.icon || cat.icon, color: meta?.color ? `#${meta.color}` : cat.color }; } function _getSavedTabOrder() { const savedOrder = _getSiteValue(TAB_ORDER_KEY, []); if (!Array.isArray(savedOrder)) return []; return savedOrder.map((id) => Number(id)).filter((id) => Number.isFinite(id)); } function _getOrderedTabCategories() { const savedOrder = _getSavedTabOrder(); if (!savedOrder.length) return [...tabCategories]; return [...tabCategories].sort((a, b) => { const idxA = savedOrder.indexOf(a.id); const idxB = savedOrder.indexOf(b.id); if (idxA === -1 && idxB === -1) return 0; if (idxA === -1) return 1; if (idxB === -1) return -1; return idxA - idxB; }); } function _parsePreloadedPayload(raw) { if (!raw) return null; try { const decoded = raw.startsWith("%") ? decodeURIComponent(raw) : raw; return objectPayload(JSON.parse(decoded)); } catch (e) { return null; } } function _extractPreloadedSiteData() { const candidates = [...document.querySelectorAll("[data-preloaded]"), ...document.querySelectorAll("script[type='application/json']")]; for (const el of candidates) { const payload = _parsePreloadedPayload(el.getAttribute("data-preloaded") || el.textContent || ""); const site = payload?._site || payload?.site || payload; if (site?.categories || site?.top_tags) return site; } return null; } async function loadSiteData() { scope.signal.throwIfAborted(); if (siteDataLoaded) return siteDataCache; if (siteDataPromise) return siteDataPromise; const loadScope = scope; siteDataPromise = (async () => { const preloaded = _extractPreloadedSiteData(); if (preloaded) { siteDataCache = preloaded; siteDataLoaded = true; return siteDataCache; } const resp = await fetch("/site.json", { signal: loadScope.signal, headers: { "X-CSRF-Token": getCsrfToken() } }); if (!resp.ok) throw new Error(`site.json ${resp.status}`); const data = await resp.json(); loadScope.signal.throwIfAborted(); siteDataCache = objectPayload(data); siteDataLoaded = true; return siteDataCache; })().finally(() => { if (loadScope === scope) siteDataPromise = null; }); return siteDataPromise; } async function loadCategoriesAndLatestData() { scope.signal.throwIfAborted(); if (categoriesAndLatestLoaded) return categoriesAndLatestCache; if (categoriesAndLatestPromise) return categoriesAndLatestPromise; const loadScope = scope; categoriesAndLatestPromise = (async () => { const resp = await fetch("/categories_and_latest.json", { signal: loadScope.signal, headers: { "X-CSRF-Token": getCsrfToken() } }); if (!resp.ok) throw new Error(`categories_and_latest.json ${resp.status}`); const data = await resp.json(); loadScope.signal.throwIfAborted(); categoriesAndLatestCache = objectPayload(data); categoriesAndLatestLoaded = true; return categoriesAndLatestCache; })().finally(() => { if (loadScope === scope) categoriesAndLatestPromise = null; }); return categoriesAndLatestPromise; } async function loadCategoryMetadata() { scope.signal.throwIfAborted(); if (categoryMetaLoaded) return; if (categoryMetaPromise) return categoryMetaPromise; const loadScope = scope; categoryMetaPromise = (async () => { try { const cachedSource = _loadCategoryDataCache(); if (cachedSource) { _applyCategoryDataSource(cachedSource, { primeSiteData: true }); return; } const [siteResult, categoriesAndLatestResult] = await Promise.allSettled([loadSiteData(), loadCategoriesAndLatestData()]); loadScope.signal.throwIfAborted(); const site = siteResult.status === "fulfilled" ? siteResult.value : null; const categoriesAndLatest = categoriesAndLatestResult.status === "fulfilled" ? categoriesAndLatestResult.value : null; if (siteResult.status === "rejected") console.warn("[SFP] load site data failed:", siteResult.reason); if (categoriesAndLatestResult.status === "rejected") console.warn("[SFP] load categories_and_latest failed:", categoriesAndLatestResult.reason); if (!site && !categoriesAndLatest) throw new Error("site category metadata unavailable"); const navigationCategories = _categoryListCategories(categoriesAndLatest); const source = _createCategoryDataCacheSource(site || {}, navigationCategories); _applyCategoryDataSource(source); _saveCategoryDataCache(source); } catch (e) { if (loadScope.disposed) return; console.warn("[SFP] load category metadata failed:", e); tabCategories = []; onChange(); categoryMetaLoaded = true; } finally { if (loadScope === scope) categoryMetaPromise = null; } })(); return categoryMetaPromise; } return { get loaded() { return categoryMetaLoaded; }, get tabCategories() { return tabCategories; }, get capabilities() { return siteCapabilities; }, _getCategoryMeta, _findTabCategoryByTabId, getCategoryTabMeta, _getOrderedTabCategories, _extractPreloadedSiteData, loadSiteData, loadCategoryMetadata, reset: _resetRuntimeCategoryData, dispose: () => scope.dispose() }; } function closestTarget(event, selector) { return event.target instanceof Element ? event.target.closest(selector) : null; } function escapeHtml(text) { if (!text) return ""; const div = document.createElement("div"); div.textContent = String(text); return div.innerHTML; } function escapeAttr(text) { return escapeHtml(text).replace(/"/g, """).replace(/'/g, "'"); } function decodeHtmlEntities(text) { if (!text) return ""; const textarea = document.createElement("textarea"); textarea.innerHTML = String(text); return textarea.value; } var EMOJI_SHORTCODE = /:[\w\-+]+:/; function selectTopicTitle(topic) { const fancy = topic.fancy_title?.trim() || ""; const unicode = topic.unicode_title?.trim() || ""; if (fancy && !EMOJI_SHORTCODE.test(fancy)) return fancy; return unicode || fancy || topic.title?.trim() || ""; } function _svgIcon(icon, extraClass = "") { const safeIcon = _safeIconName(icon); if (!safeIcon) return ""; return ``; } function _categoryColorMarkerHtml(color) { return ``; } function createControls({ t, prefs, site, host, getScope, updatePreference, saveTabOrder, getCategoryId, setCategoryId, _resetAutoLoadState, loadTopics, _beginSidebarIncomingViewSettling, _syncDefaultViewControls, _syncRefreshButtonBusy, _handleHeadActionClick, _syncSidebarIncomingTracking, _syncHeadActionState, _isAutoSilentRefreshActive, _queueSidebarIncomingApply, _startAutoSilentRefresh, _startAutoRefresh, _isLatestActivityView, renderTopics, _finishSidebarIncomingViewSettling }) { const { getCategoryTabMeta, _getOrderedTabCategories } = site; let feedRefreshBtn = null; let globalHelpTooltip = null; let pendingTabBarScrollTab = null; const requestAnimationFrame = (fn) => getScope().frame(fn); const setTimeout = (fn, ms) => getScope().timeout(fn, ms); function clampNumber(value, min, max, fallback) { const numeric = Number(value); if (!Number.isFinite(numeric)) return fallback; return Math.min(max, Math.max(min, Math.round(numeric))); } function _getOrderOptions() { return ORDER_OPTION_DEFS.filter((option) => site.capabilities.orderValues.has(option.value)).map((option) => ({ label: t(option.labelKey), value: option.value })); } function _getPeriodOptions() { return PERIOD_OPTION_DEFS.filter((option) => site.capabilities.periodValues.has(option.value)).map((option) => ({ label: t(option.labelKey), value: option.value })); } function _getFilterOptions() { return FILTER_OPTION_DEFS.filter((option) => site.capabilities.filterValues.has(option.value)).map((option) => ({ label: t(option.labelKey), value: option.value })); } function _saveTabOrderFromGrid(grid) { saveTabOrder(Array.from(grid.querySelectorAll(".sfp-tab-grid-item[data-category-id]")).map((item) => Number(item.dataset.categoryId)).filter((id) => Number.isFinite(id))); } function _buildCategoryTabContent(cat) { const tabMeta = getCategoryTabMeta(cat); return `${tabMeta.icon ? _svgIcon(tabMeta.icon) : _categoryColorMarkerHtml(tabMeta.color)}${escapeHtml(tabMeta.name)}`; } function _cssEscape(value) { return typeof window.CSS?.escape === "function" ? CSS.escape(String(value)) : String(value).replace(/["\\]/g, "\\$&"); } function _closeFloatingPanels(exceptEl = null) { document.querySelectorAll(".sfp-custom-select.open, .sfp-settings-wrap.open, .sfp-tab-shell.open").forEach((el) => { if (el !== exceptEl) el.classList.remove("open"); }); } function _scrollTabIntoView(shell, tabId = prefs.currentTab, behavior = "auto") { const bar = shell?.querySelector(".sfp-tab-bar"); const activeTab = shell?.querySelector(`.sfp-tab-bar .sfp-tab-item[data-tab="${_cssEscape(tabId)}"]`); if (!bar || !activeTab) return; const maxScrollLeft = Math.max(0, bar.scrollWidth - bar.clientWidth); const targetLeft = activeTab.offsetLeft - (bar.clientWidth - activeTab.offsetWidth) / 2; const left = Math.min(maxScrollLeft, Math.max(0, targetLeft)); bar.scrollTo({ left, behavior }); } function _buildHeaderControls(header) { const orderOptions = _getOrderOptions(); const periodOptions = _getPeriodOptions(); const periodSelect = _buildCustomSelect(periodOptions, prefs.currentPeriod, (value) => { updatePreference("currentPeriod", value); _resetAutoLoadState(); loadTopics(); }); periodSelect.classList.add("sfp-period-select"); _updatePeriodVisibility(periodSelect); const orderSelect = _buildCustomSelect(orderOptions, prefs.currentOrder, (value) => { updatePreference("currentOrder", value); _updatePeriodVisibility(periodSelect); _beginSidebarIncomingViewSettling(); _syncDefaultViewControls(); _resetAutoLoadState(); loadTopics(); }); orderSelect.classList.add("sfp-order-select"); function _updatePeriodVisibility(ps) { ps.style.display = _needsPeriodForUrl(prefs.currentOrder) && periodOptions.length > 1 ? "" : "none"; } header.appendChild(orderSelect); header.appendChild(periodSelect); const spacer = document.createElement("span"); spacer.className = "sfp-header-spacer"; header.appendChild(spacer); header.appendChild(_buildSettingsControl()); const refreshBtn = document.createElement("button"); refreshBtn.className = "sfp-refresh-btn"; refreshBtn.type = "button"; refreshBtn.title = t("refresh"); refreshBtn.setAttribute("aria-label", t("refresh")); refreshBtn.innerHTML = _refreshIconHtml(); refreshBtn.addEventListener("click", _handleHeadActionClick); feedRefreshBtn = refreshBtn; _syncRefreshButtonBusy(); header.appendChild(refreshBtn); } function _buildSettingsControl() { const wrapper = document.createElement("span"); wrapper.className = "sfp-settings-wrap"; const isLatestActivityView = _isLatestActivityView(); const shell = document.createElement("span"); shell.className = "sfp-settings-shell"; const btn = document.createElement("button"); btn.className = "sfp-settings-btn"; btn.type = "button"; btn.title = t("settings"); btn.innerHTML = ` `; const panel = document.createElement("div"); panel.className = "sfp-settings-panel"; if (isLatestActivityView) panel.innerHTML = `
${_buildSettingLabelHtml(t("incomingHintLabel"), t("incomingHintTip"))}
${_buildSettingLabelHtml(t("hidePinned"), t("hidePinnedTip"))}
${_buildSettingLabelHtml(t("autoSilentLabel"), t("autoSilentTip"))}
${_buildSettingLabelHtml(t("silentIntervalLabel"), t("silentIntervalTip"))} ${escapeHtml(t("secondsSuffix"))}
`; else panel.innerHTML = `
${_buildSettingLabelHtml(t("autoRefreshLabel"), t("autoRefreshTip"))}
${_buildSettingLabelHtml(t("autoRefreshIntervalLabel"), t("autoRefreshIntervalTip"))} ${escapeHtml(t("secondsSuffix"))}
`; btn.addEventListener("click", (e) => { e.preventDefault(); e.stopPropagation(); const shouldOpen = !wrapper.classList.contains("open"); _closeFloatingPanels(wrapper); wrapper.classList.toggle("open", shouldOpen); _syncSettingsPanelState(wrapper); }); panel.addEventListener("click", (e) => e.stopPropagation()); panel.querySelectorAll(".sfp-setting-help").forEach((helpBtn) => { const showTooltip = () => { if (!globalHelpTooltip) return; const text = helpBtn.getAttribute("data-tooltip"); if (!text) return; globalHelpTooltip.textContent = text; globalHelpTooltip.style.display = ""; globalHelpTooltip.style.visibility = "hidden"; globalHelpTooltip?.classList.add("visible"); const btnRect = helpBtn.getBoundingClientRect(); const gap = 6; const tooltipHeight = globalHelpTooltip.offsetHeight; const tooltipWidth = globalHelpTooltip.offsetWidth; let left = btnRect.left; let top = btnRect.bottom + gap; if (left + tooltipWidth > window.innerWidth - 8) left = Math.max(8, btnRect.right - tooltipWidth); if (top + tooltipHeight > window.innerHeight - 8) top = btnRect.top - tooltipHeight - gap; globalHelpTooltip.style.left = left + "px"; globalHelpTooltip.style.top = top + "px"; globalHelpTooltip.style.visibility = ""; globalHelpTooltip.classList.remove("visible"); requestAnimationFrame(() => { requestAnimationFrame(() => { globalHelpTooltip?.classList.add("visible"); }); }); }; const hideTooltip = () => { if (globalHelpTooltip) { globalHelpTooltip.classList.remove("visible"); globalHelpTooltip.style.display = "none"; } }; helpBtn.addEventListener("mouseenter", showTooltip); helpBtn.addEventListener("mouseleave", hideTooltip); helpBtn.addEventListener("click", (e) => { e.preventDefault(); e.stopPropagation(); }); }); const incomingHintInput = panel.querySelector(".sfp-incoming-hint-input"); if (incomingHintInput) _bindCheckboxSetting(incomingHintInput, (checked) => { updatePreference("showIncomingHint", checked); _syncSettingsPanelState(wrapper); _syncSidebarIncomingTracking(); _syncHeadActionState(); if (_isAutoSilentRefreshActive() && prefs.autoSilentRefreshInterval === 0) _queueSidebarIncomingApply(); }); const hidePinnedInput = panel.querySelector(".sfp-hide-pinned-input"); if (hidePinnedInput) _bindCheckboxSetting(hidePinnedInput, (checked) => { updatePreference("hidePinned", checked); _syncSettingsPanelState(wrapper); _resetAutoLoadState(); loadTopics(); }); const autoSilentInput = panel.querySelector(".sfp-auto-silent-input"); const silentIntervalInput = panel.querySelector(".sfp-auto-silent-refresh-interval-input"); if (autoSilentInput) _bindCheckboxSetting(autoSilentInput, (checked) => { updatePreference("autoSilentRefreshEnabled", checked); _syncSettingsPanelState(wrapper); _syncSidebarIncomingTracking(); _startAutoSilentRefresh(); _syncHeadActionState(); if (_isAutoSilentRefreshActive() && prefs.autoSilentRefreshInterval === 0) _queueSidebarIncomingApply(); }); _bindNumberSetting(silentIntervalInput, 0, 0, (seconds) => { updatePreference("autoSilentRefreshInterval", seconds); _startAutoSilentRefresh(); _syncHeadActionState(); if (_isAutoSilentRefreshActive() && prefs.autoSilentRefreshInterval === 0) _queueSidebarIncomingApply(); }); const autoRefreshInput = panel.querySelector(".sfp-auto-refresh-input"); const intervalRow = panel.querySelector(".sfp-auto-refresh-interval"); const intervalInput = panel.querySelector(".sfp-auto-refresh-interval-input"); if (autoRefreshInput) _bindCheckboxSetting(autoRefreshInput, (checked) => { updatePreference("autoRefreshEnabled", checked); intervalRow?.classList.toggle("visible", prefs.autoRefreshEnabled); _syncSettingsPanelHeight(wrapper); _startAutoRefresh(); _syncHeadActionState(); }); _bindNumberSetting(intervalInput, 1, 10, (seconds) => { updatePreference("autoRefreshInterval", seconds); _startAutoRefresh(); _syncHeadActionState(); }); shell.appendChild(btn); shell.appendChild(panel); wrapper.appendChild(shell); _syncSettingsPanelState(wrapper); return wrapper; } function _buildSettingLabelHtml(label, tooltip) { return ` ${escapeHtml(label)} `; } function _syncSettingsPanelState(wrapper) { const panel = wrapper?.querySelector(".sfp-settings-panel"); if (!panel) return; const incomingHintInput = panel.querySelector(".sfp-incoming-hint-input"); const hidePinnedInput = panel.querySelector(".sfp-hide-pinned-input"); const autoSilentInput = panel.querySelector(".sfp-auto-silent-input"); const autoSilentRow = panel.querySelector(".sfp-auto-silent-row"); const autoSilentIntervalRow = panel.querySelector(".sfp-auto-silent-interval"); const autoRefreshInput = panel.querySelector(".sfp-auto-refresh-input"); const autoRefreshIntervalRow = panel.querySelector(".sfp-auto-refresh-interval"); if (incomingHintInput) incomingHintInput.checked = prefs.showIncomingHint; if (hidePinnedInput) hidePinnedInput.checked = prefs.hidePinned; if (autoSilentInput) autoSilentInput.checked = prefs.autoSilentRefreshEnabled; if (autoRefreshInput) autoRefreshInput.checked = prefs.autoRefreshEnabled; if (autoSilentRow) autoSilentRow.classList.remove("hidden"); if (autoSilentIntervalRow) { autoSilentIntervalRow.classList.remove("hidden"); autoSilentIntervalRow.classList.toggle("visible", prefs.autoSilentRefreshEnabled); } if (autoRefreshIntervalRow) autoRefreshIntervalRow.classList.toggle("visible", prefs.autoRefreshEnabled); _syncSettingsPanelHeight(wrapper); } function _syncSettingsPanelHeight(wrapper) { const shell = wrapper?.querySelector(".sfp-settings-shell"); const panel = wrapper?.querySelector(".sfp-settings-panel"); if (!shell || !panel) return; requestAnimationFrame(() => { const contentBottom = Array.from(panel.children).filter((child) => { return child instanceof HTMLElement && getComputedStyle(child).display !== "none"; }).reduce((bottom, row) => { return Math.max(bottom, row.offsetTop + row.offsetHeight); }, 28); const panelStyle = getComputedStyle(panel); const paddingBottom = Number.parseFloat(panelStyle.paddingBottom) || 0; const height = Math.max(28, Math.ceil(contentBottom + paddingBottom)); shell.style.setProperty("--sfp-settings-shell-height", `${height}px`); }); } function _bindCheckboxSetting(input, onChange) { if (!input) return; input.addEventListener("change", () => onChange(input.checked)); } function _bindNumberSetting(input, min, fallback, onChange, max = Infinity) { if (!input) return; input.addEventListener("change", () => { const nextValue = clampNumber(input.value, min, max, fallback); input.value = String(nextValue); onChange(nextValue); }); } function _buildCustomSelect(options, selectedValue, onChange) { const safeOptions = options.length ? options : [{ label: "", value: selectedValue || "" }]; const wrapper = document.createElement("span"); wrapper.className = "sfp-custom-select"; const btn = document.createElement("button"); btn.className = "sfp-custom-select-btn"; btn.type = "button"; btn.textContent = (safeOptions.find((o) => o.value === selectedValue) || safeOptions[0]).label; const dropdown = document.createElement("div"); dropdown.className = "sfp-custom-select-dropdown"; let _currentSelected = selectedValue; safeOptions.forEach((opt) => { const item = document.createElement("button"); item.className = "sfp-custom-select-option" + (opt.value === selectedValue ? " selected" : ""); item.type = "button"; item.textContent = opt.label; item.addEventListener("click", (e) => { e.stopPropagation(); if (opt.value === _currentSelected) { wrapper.classList.remove("open"); return; } btn.textContent = opt.label; dropdown.querySelectorAll(".sfp-custom-select-option").forEach((el) => el.classList.remove("selected")); item.classList.add("selected"); wrapper.classList.remove("open"); _currentSelected = opt.value; onChange(opt.value); }); dropdown.appendChild(item); }); btn.addEventListener("click", (e) => { e.stopPropagation(); const shouldOpen = !wrapper.classList.contains("open"); _closeFloatingPanels(wrapper); wrapper.classList.toggle("open", shouldOpen); }); wrapper.appendChild(btn); wrapper.appendChild(dropdown); return wrapper; } function _buildTabBar() { const shell = document.createElement("div"); shell.className = "sfp-tab-shell"; const bar = document.createElement("div"); bar.className = "sfp-tab-bar"; const moreBtn = document.createElement("button"); moreBtn.type = "button"; moreBtn.className = "sfp-tab-more-btn"; moreBtn.title = t("tabMore"); moreBtn.setAttribute("aria-label", t("tabMore")); moreBtn.innerHTML = ``; const panel = document.createElement("div"); panel.className = "sfp-tab-panel"; panel.innerHTML = `
${escapeHtml(t("tabPanelTitle"))}
`; const grid = panel.querySelector(".sfp-tab-grid"); const allTab = document.createElement("span"); allTab.className = "sfp-tab-item" + (prefs.currentTab === "all" ? " active" : ""); allTab.dataset.tab = "all"; allTab.innerHTML = `${escapeHtml(t("all"))}`; bar.appendChild(allTab); const allGridItem = document.createElement("span"); allGridItem.className = "sfp-tab-grid-item" + (prefs.currentTab === "all" ? " active" : ""); allGridItem.dataset.tab = "all"; allGridItem.innerHTML = allTab.innerHTML; grid.appendChild(allGridItem); _getOrderedTabCategories().forEach((cat) => { const tab = document.createElement("span"); tab.className = "sfp-tab-item" + (prefs.currentTab === cat.tabId ? " active" : ""); tab.dataset.tab = cat.tabId; tab.dataset.categoryId = String(cat.id); tab.innerHTML = _buildCategoryTabContent(cat); bar.appendChild(tab); const gridItem = document.createElement("span"); gridItem.className = "sfp-tab-grid-item" + (prefs.currentTab === cat.tabId ? " active" : ""); gridItem.draggable = true; gridItem.dataset.tab = cat.tabId; gridItem.dataset.categoryId = String(cat.id); gridItem.title = getCategoryTabMeta(cat).name; gridItem.innerHTML = _buildCategoryTabContent(cat); grid.appendChild(gridItem); }); shell.addEventListener("click", (e) => { e.stopPropagation(); _closeFloatingPanels(shell); if (closestTarget(e, ".sfp-tab-panel-close")) { e.stopPropagation(); shell.classList.remove("open"); return; } if (closestTarget(e, ".sfp-tab-more-btn")) { e.stopPropagation(); shell.classList.toggle("open", !shell.classList.contains("open")); return; } const tab = closestTarget(e, ".sfp-tab-item, .sfp-tab-grid-item"); if (!tab) return; const tabId = tab.dataset.tab || "all"; const catId = tab.dataset.categoryId ? Number(tab.dataset.categoryId) : null; const fromGrid = tab.classList.contains("sfp-tab-grid-item"); if (tabId === prefs.currentTab && catId === getCategoryId()) { if (fromGrid) { shell.classList.remove("open"); _scrollTabIntoView(shell, tabId, "smooth"); } return; } updatePreference("currentTab", tabId); setCategoryId(catId); _beginSidebarIncomingViewSettling(); _syncDefaultViewControls(); _resetAutoLoadState(); shell.querySelectorAll(".sfp-tab-item, .sfp-tab-grid-item").forEach((t) => { t.classList.remove("active"); }); shell.querySelectorAll(`[data-tab="${_cssEscape(tabId)}"]`).forEach((t) => t.classList.add("active")); if (fromGrid) { pendingTabBarScrollTab = tabId; shell.classList.remove("open"); _scrollTabIntoView(shell, tabId, "smooth"); } loadTopics(); }); moreBtn.addEventListener("mousedown", (e) => e.preventDefault()); let dragItem = null; let tabOrderChanged = false; grid.addEventListener("dragstart", (e) => { const item = closestTarget(e, ".sfp-tab-grid-item[data-category-id]"); if (!item) return; dragItem = item; tabOrderChanged = false; item.classList.add("dragging"); if (e.dataTransfer) e.dataTransfer.effectAllowed = "move"; e.dataTransfer?.setData("text/plain", item.dataset.categoryId || ""); }); grid.addEventListener("dragover", (e) => { if (!dragItem) return; const target = closestTarget(e, ".sfp-tab-grid-item[data-category-id]"); if (!target || target === dragItem) return; e.preventDefault(); grid.querySelectorAll(".drop-target").forEach((el) => el.classList.remove("drop-target")); target.classList.add("drop-target"); const rect = target.getBoundingClientRect(); const nextNode = e.clientY < rect.top + rect.height / 2 ? target : target.nextSibling; if (dragItem !== nextNode) { grid.insertBefore(dragItem, nextNode); tabOrderChanged = true; } }); grid.addEventListener("drop", (e) => { if (!dragItem) return; e.preventDefault(); }); grid.addEventListener("dragend", () => { if (dragItem) dragItem.classList.remove("dragging"); grid.querySelectorAll(".drop-target").forEach((el) => el.classList.remove("drop-target")); if (tabOrderChanged) { _saveTabOrderFromGrid(grid); _rerenderTabBar(shell, { keepOpen: true }); } dragItem = null; tabOrderChanged = false; }); bar.addEventListener("wheel", (e) => { if (Math.abs(e.deltaY) > Math.abs(e.deltaX)) { e.preventDefault(); bar.scrollLeft += e.deltaY; } }); shell.appendChild(bar); shell.appendChild(moreBtn); shell.appendChild(panel); return shell; } function _rerenderTabBar(oldShell, options = {}) { if (!oldShell?.parentNode) return null; const scrollLeft = oldShell.querySelector(".sfp-tab-bar")?.scrollLeft || 0; const keepOpen = options.keepOpen ?? oldShell.classList.contains("open"); const newShell = _buildTabBar(); if (keepOpen) newShell.classList.add("open"); oldShell.replaceWith(newShell); const newBar = newShell.querySelector(".sfp-tab-bar"); if (options.scrollTabId) requestAnimationFrame(() => { _scrollTabIntoView(newShell, options.scrollTabId || void 0, options.scrollBehavior || "auto"); }); else if (newBar) newBar.scrollLeft = scrollLeft; return newShell; } function _buildFilterBar() { const bar = document.createElement("div"); bar.className = "sfp-filter-bar"; _getFilterOptions().forEach((f) => { const item = document.createElement("span"); item.className = "sfp-filter-item" + (prefs.currentFilter === f.value ? " active" : ""); item.dataset.filter = f.value; item.textContent = f.label; bar.appendChild(item); }); bar.addEventListener("click", (e) => { const item = closestTarget(e, ".sfp-filter-item"); if (!item) return; const filterVal = item.dataset.filter || "all"; if (filterVal === prefs.currentFilter) return; updatePreference("currentFilter", filterVal); _beginSidebarIncomingViewSettling(); _syncDefaultViewControls(); _resetAutoLoadState(); bar.querySelectorAll(".sfp-filter-item[data-filter]").forEach((i) => i.classList.remove("active")); item.classList.add("active"); renderTopics(); _finishSidebarIncomingViewSettling(); }); return bar; } function _refreshCategoryTabs() { const shell = host.feedContainer?.querySelector(".sfp-tab-shell"); if (!shell) return; const scrollTabId = pendingTabBarScrollTab; pendingTabBarScrollTab = null; _rerenderTabBar(shell, { scrollTabId, scrollBehavior: scrollTabId ? "smooth" : "auto" }); } function _updateSettingsControl() { if (!host.feedHeaderEl) return; const oldSettings = host.feedHeaderEl.querySelector(".sfp-settings-wrap"); if (!oldSettings) return; if (!!oldSettings.querySelector(".sfp-incoming-hint-row") === _isLatestActivityView()) { _syncSettingsPanelState(oldSettings); return; } const isOpen = oldSettings.classList.contains("open"); const nextSettings = _buildSettingsControl(); if (isOpen) nextSettings.classList.add("open"); oldSettings.replaceWith(nextSettings); } function _formatIncomingCount(count) { return count > 999 ? "999" : String(count); } function _refreshIconHtml() { return ``; } function _backTopIconHtml() { return ``; } function _playBackTopEnterAnimation() { if (!feedRefreshBtn) return; if (feedRefreshBtn.classList.contains("sfp-back-top-enter")) return; feedRefreshBtn.classList.add("sfp-back-top-enter"); setTimeout(() => { feedRefreshBtn?.classList.remove("sfp-back-top-enter"); }, 220); } function start() { if (globalHelpTooltip) return; globalHelpTooltip = document.createElement("div"); globalHelpTooltip.className = "sfp-help-tooltip"; globalHelpTooltip.setAttribute("role", "tooltip"); globalHelpTooltip.style.display = "none"; document.body.append(globalHelpTooltip); } function reset() { _closeFloatingPanels(); feedRefreshBtn = null; pendingTabBarScrollTab = null; } function dispose() { reset(); globalHelpTooltip?.remove(); globalHelpTooltip = null; } function renderHeadAction(isAway, isBusy, incomingCount) { if (!feedRefreshBtn) return; const showsCount = incomingCount > 0; const nextAction = showsCount ? "incoming" : isAway && !isBusy ? "back-top" : "refresh"; const currentAction = feedRefreshBtn.dataset.action || ""; const nextIncomingCount = showsCount ? String(incomingCount) : ""; const shouldUpdateActionHtml = currentAction !== nextAction || showsCount && feedRefreshBtn.dataset.incomingCount !== nextIncomingCount; const nextTitle = showsCount ? t("applyIncoming", { count: incomingCount }) : nextAction === "back-top" ? t("backToTop") : t("refresh"); const nextHtml = showsCount ? `${escapeHtml(_formatIncomingCount(incomingCount))}` : nextAction === "back-top" ? _backTopIconHtml() : _refreshIconHtml(); feedRefreshBtn.classList.toggle("sfp-away-from-head", isAway); feedRefreshBtn.classList.toggle("sfp-has-incoming-count", showsCount); if (currentAction !== nextAction) feedRefreshBtn.dataset.action = nextAction; if (nextIncomingCount) feedRefreshBtn.dataset.incomingCount = nextIncomingCount; else delete feedRefreshBtn.dataset.incomingCount; if (feedRefreshBtn.title !== nextTitle) { feedRefreshBtn.title = nextTitle; feedRefreshBtn.setAttribute("aria-label", nextTitle); } if (shouldUpdateActionHtml) { feedRefreshBtn.innerHTML = nextHtml; if (nextAction === "back-top") _playBackTopEnterAnimation(); } if (nextAction !== "back-top") feedRefreshBtn.classList.remove("sfp-back-top-enter"); } function setBusy(isBusy) { if (!feedRefreshBtn) return false; const wasBusy = feedRefreshBtn.classList.contains("spinning"); feedRefreshBtn.classList.toggle("spinning", isBusy); feedRefreshBtn.setAttribute("aria-busy", String(isBusy)); return wasBusy && !isBusy; } return { start, reset, dispose, renderHeadAction, setBusy, get feedRefreshBtn() { return feedRefreshBtn; }, _getOrderOptions, _getPeriodOptions, _getFilterOptions, _closeFloatingPanels, _buildHeaderControls, _buildTabBar, _rerenderTabBar, _buildFilterBar, _refreshCategoryTabs, _updateSettingsControl }; } var SIDEBAR_HOST_SELECTORS = ["#d-sidebar", ".sidebar-container"]; var TOGGLE_ANCHOR_SELECTORS = [ ".home-logo-wrapper-outlet .title", ".home-logo-wrapper-outlet", ".d-header .contents", ".d-header .panel" ]; function firstMatch(candidates, lookup) { for (const selector of candidates) { const found = lookup(selector); if (found) return found; } return null; } var WIDTH_VARIABLE = "--d-sidebar-width"; function captureInlineValues(saved, targets, property) { const kept = saved.filter((entry) => entry.el.isConnected && targets.includes(entry.el)); for (const el of targets) { if (!el.isConnected) continue; if (kept.some((entry) => entry.el === el)) continue; kept.push({ el, value: el.style.getPropertyValue(property), priority: el.style.getPropertyPriority(property) }); } return kept; } function restoreInlineValues(saved, property) { for (const entry of saved) { entry.el.style.removeProperty(property); if (entry.value) entry.el.style.setProperty(property, entry.value, entry.priority); } } function createFeedHost({ t, getEnabled, getWidth, onToggle, onWidth, getScope }) { let toggleBtn = null; let resizerEl = null; let isResizing = false; let originalSidebarWidthBeforeFeed = null; let widthAnimationTimer = null; let feedContainer = null; let feedHeaderEl = null; let feedScrollEl = null; let feedListEl = null; const requestAnimationFrame = (fn) => getScope().frame(fn); const setTimeout = (fn, ms) => getScope().timeout(fn, ms); function createToggle() { if (toggleBtn?.isConnected) return toggleBtn; const anchor = firstMatch(TOGGLE_ANCHOR_SELECTORS, (selector) => document.querySelector(selector)); if (!anchor) return null; toggleBtn?.remove(); toggleBtn = document.createElement("button"); toggleBtn.className = "sfp-toggle-btn" + (getEnabled() ? " active" : ""); toggleBtn.title = t("toggleTitle"); toggleBtn.innerHTML = ``; toggleBtn.addEventListener("click", (e) => { e.stopPropagation(); e.preventDefault(); onToggle(); toggleBtn?.classList.toggle("active", getEnabled()); }); anchor.appendChild(toggleBtn); return toggleBtn; } function getMinSidebarWidth() { return 272; } function getSidebarElement() { return firstMatch(SIDEBAR_HOST_SELECTORS, (selector) => document.querySelector(selector)); } function getOutletElement() { return document.querySelector("#main-outlet-wrapper"); } let savedWidthValues = []; let savedSidebarWidths = []; let widthAnimationEpoch = 0; function widthVariableTargets() { const targets = [document.documentElement, document.body]; const outlet = getOutletElement(); if (outlet) targets.push(outlet); return targets; } function setSidebarWidthVariable(width) { savedWidthValues = captureInlineValues(savedWidthValues, widthVariableTargets(), WIDTH_VARIABLE); const value = width + "px"; for (const target of widthVariableTargets()) target.style.setProperty(WIDTH_VARIABLE, value); } function clearSidebarWidthVariable() { restoreInlineValues(savedWidthValues, WIDTH_VARIABLE); savedWidthValues = []; } function setSidebarWidth(sidebar, width) { savedSidebarWidths = captureInlineValues(savedSidebarWidths, [sidebar], "width"); sidebar.style.setProperty("width", width + "px", "important"); } function clearSidebarWidths() { restoreInlineValues(savedSidebarWidths, "width"); savedSidebarWidths = []; } function applySidebarWidth(width) { const clampedWidth = Math.min(500, Math.max(getMinSidebarWidth(), width)); const sidebar = getSidebarElement(); if (sidebar) setSidebarWidth(sidebar, clampedWidth); setSidebarWidthVariable(clampedWidth); } function getSidebarWidthTransitionElements(sidebar) { return [sidebar, sidebar?.classList?.contains("sidebar-wrapper") ? sidebar : sidebar?.closest(".sidebar-wrapper")].filter((el) => !!el); } function setSidebarWidthForAnimation(sidebar, width, { enforceMin = true } = {}) { const minWidth = enforceMin ? getMinSidebarWidth() : 0; const clampedWidth = Math.min(500, Math.max(minWidth, width)); setSidebarWidth(sidebar, clampedWidth); setSidebarWidthVariable(clampedWidth); } function animateSidebarWidth(targetWidth, { cleanupAfter = false, enforceMin = true } = {}) { const sidebar = getSidebarElement(); if (!sidebar) { if (cleanupAfter) restoreSidebarWidth(); return; } const epoch = ++widthAnimationEpoch; if (widthAnimationTimer) { getScope().clearTimeout(widthAnimationTimer); widthAnimationTimer = null; } const startWidth = sidebar.getBoundingClientRect().width || 272; const minWidth = enforceMin ? getMinSidebarWidth() : 0; const clampedTarget = Math.min(500, Math.max(minWidth, targetWidth)); const transitionEls = getSidebarWidthTransitionElements(sidebar); setSidebarWidthForAnimation(sidebar, startWidth, { enforceMin: false }); transitionEls.forEach((el) => el.classList.add("sfp-width-animating")); requestAnimationFrame(() => { if (epoch !== widthAnimationEpoch) return; setSidebarWidthForAnimation(sidebar, clampedTarget, { enforceMin }); widthAnimationTimer = setTimeout(() => { widthAnimationTimer = null; transitionEls.forEach((el) => el.classList.remove("sfp-width-animating")); if (cleanupAfter) restoreSidebarWidth(); }, 260); }); } function restoreSidebarWidth() { clearSidebarWidths(); const sidebar = getSidebarElement(); if (sidebar) getSidebarWidthTransitionElements(sidebar).forEach((el) => el.classList.remove("sfp-width-animating")); clearSidebarWidthVariable(); } function setupResizer() { const sidebar = getSidebarElement(); if (!sidebar) return; if (resizerEl && !sidebar.contains(resizerEl)) { resizerEl.remove(); resizerEl = null; } if (resizerEl) return; resizerEl = sidebar.querySelector(":scope > .sfp-resizer") || document.createElement("div"); resizerEl.className = "sfp-resizer"; if (!resizerEl.parentElement) sidebar.appendChild(resizerEl); if (!resizerEl.querySelector(":scope > .sfp-resizer-grip")) { const grip = document.createElement("div"); grip.className = "sfp-resizer-grip"; resizerEl.appendChild(grip); } resizerEl.addEventListener("mousedown", beginResize); } function beginResize(e) { const sidebar = getSidebarElement(); if (!sidebar || isResizing) return; e.preventDefault(); e.stopPropagation(); isResizing = true; const startX = e.clientX; const startWidth = sidebar.offsetWidth; let finalWidth = getWidth(); resizerEl?.classList.add("sfp-resizing"); getSidebarWidthTransitionElements(sidebar).forEach((el) => el.classList.remove("sfp-width-animating")); document.body.style.cursor = "ew-resize"; document.body.style.userSelect = "none"; const onMouseMove = (e) => { if (!isResizing) return; const delta = e.clientX - startX; const newWidth = Math.min(500, Math.max(getMinSidebarWidth(), startWidth + delta)); applySidebarWidth(newWidth); finalWidth = newWidth; }; const onMouseUp = () => { onWidth(finalWidth); cleanup(); forget(); }; const removeMove = getScope().listen(document, "mousemove", onMouseMove); const removeUp = getScope().listen(document, "mouseup", onMouseUp); const cleanup = () => { isResizing = false; resizerEl?.classList.remove("sfp-resizing"); document.body.style.cursor = ""; document.body.style.userSelect = ""; removeMove(); removeUp(); }; const forget = getScope().defer(cleanup); } function removeResizer() { if (resizerEl) { resizerEl.remove(); resizerEl = null; } getSidebarElement()?.querySelectorAll(":scope > .sfp-resizer").forEach((el) => el.remove()); } function ensure() { const sidebar = getSidebarElement(); if (!sidebar) return; if (!sidebar.classList.contains("sfp-feed-mode") || originalSidebarWidthBeforeFeed === null) originalSidebarWidthBeforeFeed = sidebar.getBoundingClientRect().width || 272; sidebar.classList.add("sfp-feed-mode"); animateSidebarWidth(getWidth()); setupResizer(); } function mount(buildHeader, buildTabs, buildFilter) { const sidebar = getSidebarElement(); if (!sidebar) return false; feedContainer = document.createElement("div"); feedContainer.className = "sfp-feed-container"; feedHeaderEl = document.createElement("div"); feedHeaderEl.className = "sfp-feed-header"; buildHeader(feedHeaderEl); feedContainer.append(feedHeaderEl, buildTabs(), buildFilter()); feedScrollEl = document.createElement("div"); feedScrollEl.className = "sfp-feed-scroll"; const wrapper = document.createElement("div"); wrapper.className = "sfp-content-wrapper"; feedListEl = document.createElement("div"); feedListEl.className = "sfp-topic-list"; wrapper.append(feedListEl); feedScrollEl.append(wrapper); feedContainer.append(feedScrollEl); sidebar.append(feedContainer); ensure(); return true; } function unmount() { feedContainer?.remove(); feedContainer = feedHeaderEl = feedScrollEl = feedListEl = null; getSidebarElement()?.classList.remove("sfp-feed-mode"); removeResizer(); animateSidebarWidth(originalSidebarWidthBeforeFeed || 272, { cleanupAfter: true, enforceMin: false }); originalSidebarWidthBeforeFeed = null; } function dispose() { toggleBtn?.remove(); toggleBtn = null; unmount(); restoreSidebarWidth(); } return { createToggle, getSidebarElement, removeResizer, restoreSidebarWidth, ensure, mount, unmount, dispose, get feedContainer() { return feedContainer; }, get feedHeaderEl() { return feedHeaderEl; }, get feedScrollEl() { return feedScrollEl; }, get feedListEl() { return feedListEl; }, get resizerEl() { return resizerEl; }, get toggleBtn() { return toggleBtn; } }; } function createReadLaterView({ t, discourse, renderer, items, savedHeight, saveHeight, updateItems, reloadItems, onExternalItems, removeItem, moveItem, onAdd, markRead, fetchOnebox, fetchPageHead, getFeedScroll, onLayoutChange, getScope }) { const { isLoggedIn, toAbsoluteSiteUrl } = discourse; let section = null; let divider = null; let list = null; let overlay = null; let feedScrollEl = null; let availability = "unavailable"; const failedUrls = new Set(); const requestedUrls = new Set(); const previewCache = new Map(); const topicCache = new Map(); let previewQueue = []; let previewBusy = false; let dragActive = false; let dragHref = ""; let dropActive = false; let internalUrl = null; let internalHandled = false; let leftSection = false; let insertIndex = null; let insertEl = null; let dividerLocked = false; let dragging = false; function parsePreview(html) { return extractPreview(new DOMParser().parseFromString(html, "text/html"), location.origin); } function wait(ms) { return new Promise((resolve) => { getScope().timeout(resolve, ms); }); } function pumpPreviewQueue() { if (previewBusy || previewQueue.length === 0) return; if (availability !== "available") { previewQueue = []; return; } previewBusy = true; const url = previewQueue.shift(); (async () => { let html = ""; let status = 0; try { let result = await fetchOnebox(url, getScope().signal); if (result.status === 429) { await wait(READ_LATER_ONBOX_RETRY_MS); result = await fetchOnebox(url, getScope().signal); } status = result.status; html = result.html; } catch { status = 0; } if (status === 403) { availability = "unavailable"; previewQueue = []; } else if (status >= 200 && status < 300 && html) { const preview = parsePreview(html); if (preview.title || preview.name || preview.excerpt || preview.imageUrl) { previewCache.set(url, preview); updateItems(items().map((item) => item.url === url ? { ...item, preview } : item)); } else await fallbackToOpenGraph(url); } else await fallbackToOpenGraph(url); previewBusy = false; render(); if (previewQueue.length && availability === "available") getScope().timeout(() => pumpPreviewQueue(), 150); })(); } async function fallbackToOpenGraph(url) { let head = ""; let status = 0; try { const result = await fetchPageHead(url, getScope().signal); status = result.status; head = result.head; } catch { status = 0; } if (status >= 200 && status < 300 && head) { const preview = extractOpenGraph(new DOMParser().parseFromString(head, "text/html"), location.origin); if (preview.title || preview.excerpt || preview.imageUrl) { previewCache.set(url, preview); updateItems(items().map((item) => item.url === url ? { ...item, preview } : item)); return; } } failedUrls.add(url); } function schedulePreview(item) { if (availability !== "available") return; if (item.preview || previewCache.has(item.url) || requestedUrls.has(item.url) || failedUrls.has(item.url)) return; requestedUrls.add(item.url); previewQueue.push(item.url); pumpPreviewQueue(); } function cardElement(item) { const preview = item.preview || previewCache.get(item.url) || {}; const title = preview.title || item.title || readLaterFallbackTitle(item.url) || item.url; const href = toAbsoluteSiteUrl(item.url); if (preview.imageUrl || preview.excerpt || preview.name) { const card = document.createElement("a"); card.className = "sfp-rl-card"; card.href = href; card.innerHTML = `${preview.imageUrl ? `` : `${_svgIcon("link")}`}${preview.name ? `${escapeHtml(preview.name)}` : ""}${escapeHtml(title)}${preview.excerpt ? `${escapeHtml(preview.excerpt)}` : ""}`; return card; } const fallback = document.createElement("a"); fallback.className = "sfp-rl-fallback"; fallback.href = href; fallback.innerHTML = `${_svgIcon("link")}${escapeHtml(title)}${escapeHtml(t("linkPreview"))}`; return fallback; } function knownTopic(item) { if (item.topic) { const snapshot = { topic: item.topic, user: item.user, title: item.title }; topicCache.set(item.url, snapshot); return snapshot; } return topicCache.get(item.url) || null; } function renderItem(item) { const wrapper = document.createElement("div"); wrapper.className = "sfp-rl-item"; wrapper.dataset.url = item.url; const snapshot = item.kind === "topic" && !item.preview ? knownTopic(item) : null; if (snapshot) { const element = renderer.createTopicItem(snapshot.topic, false, { href: item.url, user: snapshot.user, onActivate: () => { markRead(item.url); renderer.markRead(element); } }); wrapper.append(element); } else { wrapper.append(cardElement(item)); if (item.kind === "link") schedulePreview(item); } return wrapper; } function itemHeight(root, edge) { if (!root) return null; const nodes = root.querySelectorAll(".sfp-rl-item, .sfp-topic-item"); const height = (edge === "top" ? nodes[0] : nodes[nodes.length - 1])?.offsetHeight || 0; return height > 0 ? height : null; } function measureRoom() { const scroll = feedScrollEl; const container = section?.parentElement; if (!scroll || !container) return null; const available = container.clientHeight - scroll.offsetTop - (divider?.offsetHeight || 0); if (available <= 0) return null; return { available, mainMin: itemHeight(scroll, "top") ?? 64, laterMin: itemHeight(list, "bottom") ?? 56 }; } function applyHeight() { if (!section || section.hidden || !divider) return; const room = measureRoom(); if (!room) return; const { available, mainMin, laterMin } = room; if (available < mainMin + laterMin) { dividerLocked = true; divider.setAttribute("aria-disabled", "true"); setSectionHeight(Math.floor(available / 2)); divider.removeAttribute("aria-valuenow"); return; } dividerLocked = false; divider.removeAttribute("aria-disabled"); const saved = savedHeight(); const autoHeight = itemHeight(list, "bottom") ?? 64; const height = Math.min(Math.max(saved ?? autoHeight, laterMin), available - mainMin); divider.setAttribute("aria-valuemin", String(Math.round(laterMin))); divider.setAttribute("aria-valuemax", String(Math.round(available - mainMin))); setSectionHeight(height); } function createDropOverlay() { const box = document.createElement("div"); box.className = "sfp-rl-drop"; box.hidden = true; box.innerHTML = `${escapeHtml(t("readLaterDropHint"))}`; return box; } function render() { if (!section || !list) return; const current = items(); const live = new Set(current.map((item) => item.url)); for (const url of requestedUrls) if (!live.has(url)) requestedUrls.delete(url); for (const url of failedUrls) if (!live.has(url)) failedUrls.delete(url); previewQueue = previewQueue.filter((url) => live.has(url)); list.replaceChildren(); if (current.length === 0) { section.hidden = true; section.style.height = ""; if (divider) divider.hidden = true; setOverlay(false); onLayoutChange(); return; } section.hidden = false; if (divider) divider.hidden = false; section.setAttribute("aria-label", t("readLaterTitle")); for (const item of current) list.append(renderItem(item)); applyHeight(); onLayoutChange(); } function setOverlay(visible) { if (!overlay) return; overlay.hidden = !visible; overlay.classList.toggle("sfp-rl-drop-active", dropActive); overlayText(); } function overlayText() { const label = overlay?.firstElementChild; if (!label) return; label.textContent = t(dropActive ? "readLaterDropRelease" : "readLaterDropHint"); } function setDropActive(active) { if (dropActive === active) return; dropActive = active; overlay?.classList.toggle("sfp-rl-drop-active", active); overlayText(); } function setInsertMark(index) { if (insertIndex === index && insertEl) return; insertEl?.classList.remove("sfp-rl-insert-before", "sfp-rl-insert-after"); insertEl = null; insertIndex = index; const nodes = [...list?.querySelectorAll(".sfp-rl-item") ?? []]; if (index === null || nodes.length === 0) return; const target = index < nodes.length ? nodes[index] : nodes[nodes.length - 1]; insertEl = target; target.classList.add(index < nodes.length ? "sfp-rl-insert-before" : "sfp-rl-insert-after"); } function insertionIndexAt(clientY) { const nodes = [...list?.querySelectorAll(".sfp-rl-item") ?? []]; for (let index = 0; index < nodes.length; index++) { const rect = nodes[index].getBoundingClientRect(); if (clientY < rect.top + rect.height / 2) return index; } return nodes.length; } function beginDrag() { if (dragActive) return; dragActive = true; if (!section) return; section.hidden = false; if (divider) divider.hidden = false; setOverlay(true); applyHeight(); onLayoutChange(); } function beginInternalDrag(url) { internalUrl = url; internalHandled = false; leftSection = false; list?.querySelector(`.sfp-rl-item[data-url="${CSS.escape(url)}"]`)?.classList.add("sfp-rl-dragging"); } function endDrag() { if (!dragActive && internalUrl === null) return; const removedUrl = internalUrl && !internalHandled && leftSection ? internalUrl : null; dragActive = false; dragHref = ""; dropActive = false; internalUrl = null; internalHandled = false; leftSection = false; setOverlay(false); setInsertMark(null); if (removedUrl) removeItem(removedUrl); render(); } function readDraggedHref(event) { const data = event.dataTransfer; if (!data) return dragHref; const candidate = data.getData("text/uri-list").split(/\r?\n/).map((line) => line.trim()).find((line) => line && !line.startsWith("#")); if (candidate) return candidate; return data.getData("text/plain").trim() || dragHref; } function clampHeight(next) { const room = measureRoom(); if (!room) return null; const max = Math.max(room.laterMin, room.available - room.mainMin); return Math.round(Math.min(Math.max(next, room.laterMin), max)); } function setSectionHeight(height) { if (!section) return; section.style.height = `${height}px`; divider?.setAttribute("aria-valuenow", String(height)); } function setupHeightDrag() { if (!divider || !section) return; const scope = getScope(); const start = (event) => { if (dividerLocked) return; if (event.button !== 0) return; event.preventDefault(); dragging = true; divider?.classList.add("sfp-rl-dragging"); const startY = event.clientY; const startHeight = section?.offsetHeight || 0; const removeMove = scope.listen(document, "pointermove", (move) => { if (!dragging) return; const height = clampHeight(startHeight - (move.clientY - startY)); if (height !== null) setSectionHeight(height); }); const removeUp = scope.listen(document, "pointerup", () => { if (!dragging) return; dragging = false; divider?.classList.remove("sfp-rl-dragging"); const height = section?.offsetHeight || 0; if (height > 0) saveHeight(height); removeMove(); removeUp(); onLayoutChange(); }); }; scope.listen(divider, "pointerdown", start); scope.listen(divider, "keydown", (event) => { if (dividerLocked || !section) return; const room = measureRoom(); if (!room) return; const current = section.offsetHeight; let next; if (event.key === "ArrowUp") next = current + 16; else if (event.key === "ArrowDown") next = current - 16; else if (event.key === "Home") next = room.laterMin; else if (event.key === "End") next = room.available - room.mainMin; else return; event.preventDefault(); const height = clampHeight(next); if (height === null) return; setSectionHeight(height); saveHeight(height); onLayoutChange(); }); } function setupDragAndDrop(container) { const scope = getScope(); scope.listen(document, "dragstart", (event) => { const origin = event.target instanceof Element ? event.target : null; if (!origin) return; const own = origin.closest(".sfp-rl-item"); if (own && section?.contains(own)) { const url = own.dataset.url || ""; if (url) { beginInternalDrag(url); event.dataTransfer?.setData("application/x-sfp-read-later", url); if (event.dataTransfer) event.dataTransfer.effectAllowed = "move"; } return; } dragHref = origin.closest("a[href]")?.getAttribute("href") || origin.closest(".sfp-topic-item")?.querySelector(".sfp-topic-title-line")?.getAttribute("href") || ""; if (dragHref) beginDrag(); }, true); scope.listen(container, "dragenter", (event) => { if (internalUrl) return; const types = Array.from(event.dataTransfer?.types || []); if (!dragActive && !types.includes("text/uri-list")) return; beginDrag(); }); scope.listen(container, "dragover", (event) => { if (internalUrl) { if (!section?.contains(event.target)) return; event.preventDefault(); if (event.dataTransfer) event.dataTransfer.dropEffect = "move"; setInsertMark(insertionIndexAt(event.clientY)); return; } const types = Array.from(event.dataTransfer?.types || []); if (!dragActive && !types.includes("text/uri-list")) return; event.preventDefault(); if (event.dataTransfer) event.dataTransfer.dropEffect = "copy"; }); if (section) { const zone = section; scope.listen(zone, "dragenter", (event) => { if (internalUrl) return; event.preventDefault(); setDropActive(true); }); scope.listen(zone, "dragover", (event) => { if (internalUrl) return; event.preventDefault(); setDropActive(true); }); scope.listen(zone, "dragleave", (event) => { const next = event.relatedTarget; if (next instanceof Node && zone.contains(next)) return; setDropActive(false); if (internalUrl) { leftSection = true; setInsertMark(null); } }); } scope.listen(container, "dragleave", (event) => { const next = event.relatedTarget; if (!next || !(next instanceof Node) || !container.contains(next)) { setDropActive(false); if (internalUrl) { leftSection = true; setInsertMark(null); } } }); scope.listen(section, "drop", (event) => { event.preventDefault(); event.stopPropagation(); if (internalUrl) { const url = internalUrl; const from = [...list?.querySelectorAll(".sfp-rl-item") ?? []].findIndex((node) => node.dataset.url === url); const to = insertionIndexAt(event.clientY); internalHandled = true; setInsertMark(null); const target = to > from ? to - 1 : to; if (from >= 0 && target !== from) moveItem(url, target); endDrag(); return; } const raw = readDraggedHref(event); setDropActive(false); endDrag(); if (!raw) return; const parsed = parseReadLaterLink(raw, location.origin); if (!parsed) return; const plain = event.dataTransfer?.getData("text/plain") || ""; onAdd(parsed, plain && !/^https?:\/\//i.test(plain) ? plain : ""); }); scope.listen(document, "dragend", () => endDrag()); scope.listen(window, "dragleave", (event) => { if (!event.relatedTarget) endDrag(); }); scope.listen(document, "drop", () => { setDropActive(false); endDrag(); }); } function mount(container) { if (section && container.contains(section)) { ensure(container); return; } section?.remove(); divider?.remove(); feedScrollEl = getFeedScroll(); divider = document.createElement("div"); divider.className = "sfp-rl-divider"; divider.setAttribute("role", "separator"); divider.setAttribute("aria-orientation", "horizontal"); divider.setAttribute("aria-label", t("readLaterTitle")); divider.tabIndex = 0; divider.hidden = true; for (const spot of ["center", "end"]) { const grip = document.createElement("div"); grip.className = `sfp-rl-grip sfp-rl-grip-${spot}`; divider.append(grip); } section = document.createElement("section"); section.className = "sfp-read-later"; section.hidden = true; const scroll = document.createElement("div"); scroll.className = "sfp-rl-scroll"; list = document.createElement("div"); list.className = "sfp-rl-list"; scroll.append(list); overlay = createDropOverlay(); section.append(scroll, overlay); container.append(divider, section); availability = isLoggedIn() ? "available" : "unavailable"; setupHeightDrag(); setupDragAndDrop(container); getScope().listen(window, "resize", () => applyHeight()); getScope().listen(window, "storage", (event) => { if (event.key === null || event.key.endsWith("sfp_read_later_v1")) { reloadItems(); render(); onExternalItems(); return; } if (event.key.endsWith("sfp_read_later_height_v1")) { reloadItems(); render(); } }); render(); } function ensure(container) { if (!section || !divider) { mount(container); return; } if (!container.contains(section)) container.append(divider, section); feedScrollEl = getFeedScroll(); render(); } return { mount, ensure, render, destroy() { section?.remove(); divider?.remove(); section = null; divider = null; list = null; overlay = null; feedScrollEl = null; insertEl = null; insertIndex = null; internalUrl = null; previewQueue = []; }, refresh: () => render(), cachedTopic: (url) => topicCache.get(url) }; } function createFeedScroll(callbacks) { let release; function dispose() { release?.(); release = void 0; } function bind(element) { dispose(); if (!element) return; let timer; const onLoadScroll = () => { clearTimeout(timer); timer = setTimeout(() => { timer = void 0; if (!callbacks.canLoadMore()) return; const { scrollTop, scrollHeight, clientHeight } = element; if (scrollHeight - scrollTop - clientHeight < 200) callbacks.onLoadMore(); }, 300); }; element.addEventListener("scroll", callbacks.onReadingState, { passive: true }); element.addEventListener("scroll", onLoadScroll, { passive: true }); release = () => { clearTimeout(timer); element.removeEventListener("scroll", callbacks.onReadingState); element.removeEventListener("scroll", onLoadScroll); }; } return { bind, dispose }; } function _topicBaseUrl(topic) { return `/t/${topic.slug || "topic"}/${topic.id}`; } function _isPinnedTopic(topic) { return !!(topic && (topic.pinned || topic.pinned_globally)); } function _hasLastReadPostNumber(topic) { return topic.last_read_post_number !== null && topic.last_read_post_number !== void 0 && topic.last_read_post_number !== ""; } function _topicListUrl(topic) { const baseUrl = _topicBaseUrl(topic); if (!_hasLastReadPostNumber(topic)) return baseUrl; const lastRead = Number(topic.last_read_post_number); const highest = Number(topic.highest_post_number); if (!Number.isFinite(lastRead)) return baseUrl; let postNumber = lastRead + 1; if (Number.isFinite(highest) && postNumber > highest) postNumber = highest; if (postNumber < 1) postNumber = 1; return `${baseUrl}/${postNumber}`; } function _isTopicRead(topic) { if (!topic || !topic.id) return false; const baseUrl = _topicBaseUrl(topic); const url = _topicListUrl(topic); return url !== baseUrl && url.startsWith(`${baseUrl}/`); } function _hasUnreadMarker(topic) { return !_isTopicRead(topic); } var TRACKING_LEVEL = 2; function _unreadReplyCount(topic) { if (!(Number(topic.notification_level) >= TRACKING_LEVEL)) return 0; const count = Number(topic.unread_posts) || Number(topic.new_posts) || 0; return count > 0 ? count : 0; } function _applyReadMarker(topic) { topic.unseen = false; topic.is_seen = true; if (topic.highest_post_number) topic.last_read_post_number = topic.highest_post_number; } function createTopicRenderer({ t, formatRelativeTime, site, tags, discourse, getUser, markTopicAsRead, getScope }) { const { _getCategoryMeta } = site; const { _tagDisplayName, _getTagStyle } = tags; const { getAvatarUrl, getUserProfileUrl, handlePointerNavigation } = discourse; const topicHighlightTimers = new WeakMap(); const setTimeout = (fn, delay) => getScope().timeout(fn, delay); function _triggerTopicHighlight(item) { if (!item) return; const oldTimer = topicHighlightTimers.get(item); if (oldTimer) getScope().clearTimeout(oldTimer); item.classList.remove("sfp-new-highlight"); item.offsetWidth; item.classList.add("sfp-new-highlight"); const timer = setTimeout(() => { item.classList.remove("sfp-new-highlight"); topicHighlightTimers.delete(item); }, 1e4); topicHighlightTimers.set(item, timer); } function _topicStatsHtml(topic) { const replies = Math.max(0, (topic.posts_count || 1) - 1); const views = (topic.views || 0) >= 1e3 ? ((topic.views || 0) / 1e3).toFixed(1) + "k" : topic.views || 0; const likes = topic.like_count || 0; return ` ${_svgIcon("comment")} ${replies} ${_svgIcon("far-eye")} ${views} ${_svgIcon("heart")} ${likes} `; } function _topicStatusBadgesHtml(topic) { const statusBadges = []; if (topic.is_hot) statusBadges.push(`

${escapeHtml(t("hot"))}

`); if (topic.pinned || topic.pinned_globally) statusBadges.push(`

${escapeHtml(t("pinned"))}

`); if (_isTopicUnavailable(topic)) statusBadges.push(`${_svgIcon("far-eye-slash")}

${escapeHtml(t("topicUnavailable"))}

`); return statusBadges.length ? `${statusBadges.join("")}` : ""; } function _topicTimeHtml(topic) { const timeStr = formatRelativeTime(topic.bumped_at || topic.last_posted_at || topic.created_at); const replies = _unreadReplyCount(topic); if (replies) return `${timeStr}${replies > 99 ? "99+" : replies}`; return `${timeStr}${``}`; } function _isTopicUnavailable(topic) { return !!topic?.sfpUnavailable; } function _buildCategoryBadge(categoryId) { const meta = _getCategoryMeta(categoryId); if (!meta?.name) return ""; const styleParts = [`--category-badge-color: #${_normalizeHexColor(meta.color, "888")}`, `--category-badge-text-color: #${_normalizeHexColor(meta.text_color, "FFFFFF")}`]; if (meta.parent_category_id && meta.parent_color) { styleParts.push(`--parent-category-badge-color: #${_normalizeHexColor(meta.parent_color, "888")}`); styleParts.push(`--parent-category-badge-text-color: #${_normalizeHexColor(meta.parent_text_color, "FFFFFF")}`); } const styleType = _safeCategoryStyleType(meta.style_type, !!meta.icon); const categoryClasses = ["badge-category"]; if (meta.read_restricted) categoryClasses.push("restricted"); if (meta.parent_category_id) categoryClasses.push("--has-parent"); categoryClasses.push(`--style-${styleType}`); const dataParent = meta.parent_category_id ? ` data-parent-category-id="${Number(meta.parent_category_id)}"` : ""; const title = meta.description_text || meta.description_excerpt || ""; const titleAttr = title ? ` title="${escapeAttr(title)}"` : ""; const iconHtml = styleType === "icon" && meta.icon ? _svgIcon(meta.icon) : ""; const lockHtml = meta.read_restricted ? _svgIcon("lock") : ""; return `${iconHtml}${lockHtml}${escapeHtml(meta.name)}`; } function _buildTagBadge(tag) { const tagName = _tagDisplayName(tag); if (!tagName) return ""; const tagStyle = _getTagStyle(tag); const classes = [ "discourse-tag", "box", "sfp-tag" ]; if (tagStyle?.hasIcon) classes.push("discourse-tag--tag-icons-style"); const styleAttr = tagStyle?.cssText ? ` style="${tagStyle.cssText}"` : ""; const iconHtml = tagStyle?.hasIcon ? `${_svgIcon(tagStyle.icon)}` : ""; return `${iconHtml}${escapeHtml(tagName)}`; } function createTopicItem(topic, isNew = false, { href, user, onActivate } = {}) { const item = document.createElement("div"); const targetUrl = href || _topicListUrl(topic); item.className = "sfp-topic-item"; item.dataset.topicId = String(topic.id); if (topic.pinned || topic.pinned_globally) item.classList.add("sfp-pinned"); if (_isTopicRead(topic)) item.classList.add("sfp-read"); if (_isTopicUnavailable(topic)) item.classList.add("sfp-topic-unavailable"); if (isNew) _triggerTopicHighlight(item); let avatarUrl = ""; let name = ""; let username = ""; let userProfileUrl = ""; const posterUserId = topic.posters?.length ? topic.posters[0].user_id : null; const resolvedUser = user || (posterUserId === null ? void 0 : getUser(posterUserId)); if (resolvedUser) { name = resolvedUser.name || ""; username = resolvedUser.username || ""; userProfileUrl = getUserProfileUrl(username); if (resolvedUser.avatar_template) avatarUrl = getAvatarUrl(resolvedUser.avatar_template, 45); } const avatarHtml = userProfileUrl && avatarUrl ? `${escapeHtml(username)}` : avatarUrl ? `${escapeHtml(username)}` : ""; const displayName = name && name !== username ? userProfileUrl ? `${escapeHtml(name)}` : `${escapeHtml(name)}` : ""; const usernameHtml = userProfileUrl ? `${escapeHtml(username)}` : `${escapeHtml(username)}`; const statusBadgesHtml = _topicStatusBadgesHtml(topic); const closedHtml = topic.closed ? `` : ""; const categoryHtml = _buildCategoryBadge(topic.category_id); let tagsHtml = ""; if (topic.tags && topic.tags.length > 0) tagsHtml = `${topic.tags.slice(0, 3).map((tag) => { return _buildTagBadge(tag); }).join("")}`; item.innerHTML = `
${avatarHtml}
${statusBadgesHtml} ${_topicTimeHtml(topic)}
${closedHtml}${escapeHtml(decodeHtmlEntities(selectTopicTitle(topic)))}
${categoryHtml} ${tagsHtml}
${_topicStatsHtml(topic)}
`; const profileHref = (e) => closestTarget(e, "a.sfp-topic-user-link")?.getAttribute("href") || ""; const activate = onActivate || (() => markTopicAsRead(topic, item)); item.addEventListener("click", (e) => { handlePointerNavigation(e, profileHref(e) || targetUrl, { onPrimaryActivate: profileHref(e) ? void 0 : activate }); }); item.addEventListener("auxclick", (e) => { handlePointerNavigation(e, profileHref(e) || targetUrl, { onMiddleActivate: profileHref(e) ? void 0 : activate }); }); return item; } return { createTopicItem, markRead(item) { item.classList.add("sfp-read"); item.querySelector(".sfp-unread-dot")?.classList.add("sfp-unread-hidden"); } }; } function createTopicList({ getScroll, getList, getScope, syncHead, atHead, t, hasMore, loadMore, renderItem }) { const requestAnimationFrame = (fn) => getScope().frame(fn); function _captureFeedScrollAnchor() { const feedScrollEl = getScroll(); const feedListEl = getList(); if (!feedScrollEl || !feedListEl) return null; if (feedScrollEl.scrollTop <= 1) return null; const scrollRect = feedScrollEl.getBoundingClientRect(); const items = feedListEl.querySelectorAll(".sfp-topic-item[data-topic-id]"); for (const item of items) { const itemRect = item.getBoundingClientRect(); if (itemRect.bottom > scrollRect.top + 1) return { topicId: item.dataset.topicId || null, offsetTop: itemRect.top - scrollRect.top, scrollTop: feedScrollEl.scrollTop, scrollHeight: feedScrollEl.scrollHeight }; } return { topicId: null, offsetTop: 0, scrollTop: feedScrollEl.scrollTop, scrollHeight: feedScrollEl.scrollHeight }; } function _restoreFeedScrollAnchor(anchor) { const feedScrollEl = getScroll(); const feedListEl = getList(); if (!anchor || !feedScrollEl || !feedListEl) return; const restore = () => { if (!feedScrollEl || !feedListEl) return; if (anchor.topicId) { const item = feedListEl.querySelector(`.sfp-topic-item[data-topic-id="${anchor.topicId}"]`); if (item) { const scrollRect = feedScrollEl.getBoundingClientRect(); const itemRect = item.getBoundingClientRect(); feedScrollEl.scrollTop += itemRect.top - scrollRect.top - anchor.offsetTop; syncHead(); return; } } feedScrollEl.scrollTop = anchor.scrollTop + (feedScrollEl.scrollHeight - anchor.scrollHeight); syncHead(); }; restore(); requestAnimationFrame(restore); } function _waitForFeedScrollHead(timeoutMs = 1200) { const feedScrollEl = getScroll(); const viewScope = getScope(); if (!feedScrollEl) return Promise.resolve("interrupted"); if (atHead()) return Promise.resolve("reached"); const scrollElement = feedScrollEl; return new Promise((resolve) => { let forget = () => {}; const startedAt = Date.now(); let settledFrames = 0; let done = false; const cleanup = () => { if (done) return; done = true; scrollElement.removeEventListener("scroll", onScroll); scrollElement.removeEventListener("wheel", onInterrupt); scrollElement.removeEventListener("pointerdown", onInterrupt); scrollElement.removeEventListener("touchstart", onInterrupt); window.removeEventListener("keydown", onInterrupt, true); }; const finish = (result) => { if (done) return; forget(); cleanup(); resolve(result); }; const onInterrupt = () => { if (feedScrollEl) feedScrollEl.scrollTo({ top: feedScrollEl.scrollTop, behavior: "auto" }); finish(atHead() ? "reached" : "interrupted"); }; const onScroll = () => { if (!feedScrollEl) { finish("interrupted"); return; } if (atHead()) { settledFrames++; if (settledFrames >= 2) finish("reached"); } else settledFrames = 0; }; const tick = () => { if (done) return; onScroll(); if (done) return; if (Date.now() - startedAt >= timeoutMs) { finish(atHead() ? "reached" : "timeout"); return; } requestAnimationFrame(tick); }; forget = viewScope.defer(() => finish("interrupted")); scrollElement.addEventListener("scroll", onScroll, { passive: true }); scrollElement.addEventListener("wheel", onInterrupt, { passive: true }); scrollElement.addEventListener("pointerdown", onInterrupt, { passive: true }); scrollElement.addEventListener("touchstart", onInterrupt, { passive: true }); window.addEventListener("keydown", onInterrupt, true); requestAnimationFrame(tick); }); } function _renderPaginationFooter({ note = "" } = {}) { const list = getList(); if (!list) return; _removePaginationFooter(); if (note) { const noteEl = document.createElement("div"); noteEl.className = "sfp-load-more-note"; noteEl.textContent = note; list.appendChild(noteEl); } if (hasMore()) { const loadMoreEl = document.createElement("div"); loadMoreEl.className = "sfp-load-more"; loadMoreEl.textContent = t("loadMore"); loadMoreEl.addEventListener("click", () => { loadMoreEl.remove(); loadMore(); }); list.appendChild(loadMoreEl); return; } _appendNoMore(); } function _showLoadMoreSpinner() { const list = getList(); _removePaginationFooter(); const el = document.createElement("div"); el.className = "sfp-load-more"; el.innerHTML = `${escapeHtml(t("loading"))}`; if (list) list.appendChild(el); } function _removePaginationFooter() { getList()?.querySelectorAll(".sfp-load-more, .sfp-no-more, .sfp-load-more-note").forEach((el) => el.remove()); } function _showNoMore() { _removePaginationFooter(); _appendNoMore(); } function _appendNoMore() { const list = getList(); const el = document.createElement("div"); el.className = "sfp-no-more"; el.textContent = t("noMore"); if (list) list.appendChild(el); } function _showLoadMoreError(error) { const list = getList(); _removePaginationFooter(); const el = document.createElement("div"); el.className = "sfp-load-more sfp-load-more-error"; el.innerHTML = ` ${escapeHtml(t("requestFailed"))} `; el.querySelector(".sfp-load-more-retry")?.addEventListener("click", () => { loadMore(); }); if (list) list.appendChild(el); console.warn("[SFP] load more failed:", error); } function showMessage(message) { const list = getList(); if (list) list.innerHTML = `
${escapeHtml(message)}
`; } function showLoading() { const list = getList(); if (list) list.innerHTML = `
${escapeHtml(t("loading"))}
`; } function showError(error, retry) { const list = getList(); if (!list) return; list.innerHTML = `
!
${escapeHtml(t("loadFailed"))}
${escapeHtml(errorMessage(error))}
`; list.querySelector(".sfp-retry-btn")?.addEventListener("click", retry); } function append(topics, highlights = []) { const list = getList(); if (list) for (const topic of topics) list.append(renderItem(topic, highlights.includes(topic.id))); } function render(topics, emptyMessage, highlights) { const list = getList(); if (!list) return; list.replaceChildren(); if (!topics.length) showMessage(emptyMessage); else append(topics, highlights); _renderPaginationFooter(); } return { showMessage, showLoading, showError, append, render, _renderPaginationFooter, _showLoadMoreSpinner, _showNoMore, _showLoadMoreError, captureAnchor: _captureFeedScrollAnchor, restoreAnchor: _restoreFeedScrollAnchor, waitForHead: _waitForFeedScrollHead }; } function topicResponse(value) { const data = objectPayload(value) || {}; const list = objectPayload(data.topic_list); return { users: Array.isArray(data.users) ? data.users.filter((user) => !!user && Number.isFinite(user.id)) : [], topic_list: list ? { topics: Array.isArray(list.topics) ? list.topics.filter((topic) => !!topic && Number.isFinite(topic.id)) : void 0, more_topics_url: typeof list.more_topics_url === "string" ? list.more_topics_url : null } : void 0 }; } function createFeedApi({ buildUrl, getCsrfToken, getSignal }) { async function request(url, signal = getSignal()) { const response = await fetch(url, { headers: { "X-CSRF-Token": getCsrfToken() }, signal }); if (!response.ok) throw new Error(`API error: ${response.status}`); return topicResponse(await response.json()); } return { page: (query, page) => request(buildUrl(query, page)), byIds(ids, signal = getSignal()) { const unique = [...new Set(ids.map(Number).filter(Number.isFinite))]; return unique.length ? request(`/latest.json?topic_ids=${unique.join(",")}`, signal) : Promise.resolve(null); }, async topicView(id, signal = getSignal()) { const response = await fetch(`/t/${id}.json`, { headers: { "X-CSRF-Token": getCsrfToken() }, signal }); if (!response.ok) return { status: response.status, json: null }; const json = await response.json().catch(() => null); return { status: response.status, json }; } }; } var AutoLoadGate = class { now; key = ""; timestamps = []; emptyCount = 0; stopped = false; constructor(now = Date.now) { this.now = now; } reset(key) { this.key = key; this.timestamps = []; this.emptyCount = 0; this.stopped = false; } ensure(key) { if (this.key !== key) this.reset(key); } canRun(key) { this.ensure(key); if (this.stopped) return false; this.timestamps = this.timestamps.filter((time) => this.now() - time < AUTO_LOAD_RATE_WINDOW_MS); return this.timestamps.length < 3; } recordRequest(key) { this.ensure(key); this.timestamps.push(this.now()); } recordResult(key, count) { this.ensure(key); this.emptyCount = count > 0 ? 0 : this.emptyCount + 1; if (this.emptyCount >= 3) this.stopped = true; } }; var IncomingTopics = class { candidates = []; candidateIds = new Set(); cache = new Map(); filtered = []; get ids() { return this.candidates; } get matchingIds() { return this.filtered; } get(id) { return this.cache.get(id); } clear() { this.candidates = []; this.candidateIds.clear(); this.cache.clear(); this.filtered = []; } touch(id, payload) { const numeric = Number(id); if (!Number.isFinite(numeric)) return false; if (this.candidateIds.has(numeric)) { const index = this.candidates.indexOf(numeric); if (index !== -1) this.candidates.splice(index, 1); } else this.candidateIds.add(numeric); this.candidates.push(numeric); if (payload) this.cache.set(numeric, { ...this.cache.get(numeric), ...payload, id: numeric }); return true; } recompute(enabled, matches) { this.filtered = enabled ? this.candidates.filter((id) => { const topic = this.cache.get(id); return !topic || matches(topic); }) : []; return this.filtered; } loadIds(pageSize) { return this.filtered.slice(-Math.max(1, pageSize)); } remove(ids) { const removed = new Set(ids.map(Number).filter(Number.isFinite)); this.candidates = this.candidates.filter((id) => !removed.has(id)); this.filtered = this.filtered.filter((id) => !removed.has(id)); removed.forEach((id) => { this.candidateIds.delete(id); this.cache.delete(id); }); } }; var ReadingState = class { away = null; atHead(scrollTop) { return scrollTop <= 1; } isAway(scrollTop, viewport) { return !this.atHead(scrollTop) && (this.away === true || scrollTop > viewport); } reset() { this.away = null; } }; var RefreshCountdown = class { timer = null; remaining = 0; get running() { return this.timer !== null; } reset(seconds) { this.remaining = seconds; } start(seconds, tick) { this.stop(); this.reset(seconds); this.timer = window.setInterval(() => { if (--this.remaining <= 0) { this.reset(seconds); tick(); } }, 1e3); } stop() { if (this.timer !== null) window.clearInterval(this.timer); this.timer = null; } }; var PageActivity = class { now; lastActivity; constructor(now = Date.now) { this.now = now; this.lastActivity = now(); } record = () => { this.lastActivity = this.now(); }; idle(hidden) { return hidden || this.now() - this.lastActivity > 6e5; } start(scope) { for (const event of [ "pointerdown", "keydown", "wheel", "touchstart", "scroll" ]) scope.listen(window, event, this.record, { passive: true }); scope.listen(document, "visibilitychange", () => { if (document.visibilityState === "visible") this.record(); }); } }; function markUnavailable(topic) { if (!topic || topic.sfpUnavailable) return false; topic.sfpUnavailable = true; topic.sfpUnavailablePushed = 0; return true; } function sortKey(topic, order) { if (!topic) return null; switch (order) { case "created": { const created = Date.parse(topic.created_at || ""); return Number.isFinite(created) ? created : null; } case "views": return Number.isFinite(topic.views) ? topic.views : null; case "posts": return Number.isFinite(topic.posts_count) ? topic.posts_count : null; case "likes": return Number.isFinite(topic.like_count) ? topic.like_count : null; case "op_likes": return Number.isFinite(topic.op_like_count) ? topic.op_like_count : null; default: { const bumped = Date.parse(topic.bumped_at || ""); return Number.isFinite(bumped) ? bumped : null; } } } function interleaveUnavailableTopics(allTopics, query, freshLength) { if (allTopics.length <= freshLength) return; const anomalies = []; for (let index = allTopics.length - 1; index >= freshLength; index--) if (allTopics[index]?.sfpUnavailable) anomalies.unshift(allTopics.splice(index, 1)[0]); if (anomalies.length === 0) return; const pinnedFloats = !query?.order || ["activity", "default"].includes(query.order); for (const anomaly of anomalies) { const keyValue = sortKey(anomaly, query.order); let insertIndex = 0; if (keyValue !== null) while (insertIndex < allTopics.length) { const resident = allTopics[insertIndex]; if (pinnedFloats && (resident.pinned || resident.pinned_globally)) { insertIndex++; continue; } const residentKey = sortKey(resident, query.order); if (residentKey !== null && residentKey < keyValue) break; insertIndex++; } allTopics.splice(insertIndex, 0, anomaly); } } function detectVanishedTopics(allTopics, rawTopics, query) { if (!Array.isArray(rawTopics) || rawTopics.length === 0) return; if (allTopics.length === 0) return; if (_needsPeriodForUrl(query.order) && query.period !== "all") return; const pinnedFloats = !query.order || ["activity", "default"].includes(query.order); const newTopicIds = new Set(rawTopics.map((topic) => Number(topic?.id)).filter(Number.isFinite)); let windowMinKeyValue = null; for (const topic of rawTopics) { if (pinnedFloats && (topic?.pinned || topic?.pinned_globally)) continue; const keyValue = sortKey(topic, query.order); if (keyValue === null) continue; if (windowMinKeyValue === null || keyValue < windowMinKeyValue) windowMinKeyValue = keyValue; } if (windowMinKeyValue === null) return; const candidateWindow = Math.min(rawTopics.length, allTopics.length); for (let index = 0; index < candidateWindow; index++) { const topic = allTopics[index]; if (!topic || newTopicIds.has(Number(topic.id))) continue; if (pinnedFloats && (topic.pinned || topic.pinned_globally)) continue; if (topic.sfpUnavailable) continue; const keyValue = sortKey(topic, query.order); if (keyValue === null || keyValue <= windowMinKeyValue) continue; markUnavailable(topic); } } var ResidentTopics = class { retained = []; displayUsers = {}; loaded = new Set(); depth = 0; size = 30; more = true; get topics() { return this.retained; } get users() { return this.displayUsers; } get page() { return this.depth; } get pageSize() { return this.size; } get hasMore() { return this.more; } get limit() { return Math.max(1, this.size) * (Math.max(0, this.depth) + 1); } has(id) { return this.loaded.has(id); } reset({ clearUsers = true } = {}) { this.retained = []; if (clearUsers) this.displayUsers = {}; this.loaded.clear(); this.depth = 0; this.more = true; } addUsers(users = []) { for (const user of users) this.displayUsers[user.id] = user; } loadHead(topics, rawPageSize, moreTopicsUrl) { this.retained = topics; this.loaded = new Set(topics.map((topic) => topic.id)); this.size = rawPageSize; this.depth = 0; this.more = !!moreTopicsUrl; } endPages() { this.more = false; } append(topics, moreTopicsUrl) { this.depth++; this.more = !!moreTopicsUrl; const added = topics.filter((topic) => { if (this.loaded.has(topic.id)) return false; this.loaded.add(topic.id); return true; }); this.retained = this.retained.concat(added); return added; } markRead(topic) { if (_isTopicRead(topic)) return false; _applyReadMarker(topic); const resident = this.retained.find((candidate) => candidate.id === topic.id); if (resident && resident !== topic) _applyReadMarker(resident); return true; } lifecycle(message) { if (!message?.topic_id) return false; const topicId = Number(message.topic_id); if (!Number.isFinite(topicId)) return false; const topic = this.retained.find((candidate) => candidate.id === topicId); if (!topic) return false; if (message.message_type === "recover") { if (!topic.sfpUnavailable) return false; topic.sfpUnavailable = false; delete topic.sfpUnavailablePushed; return true; } return ["delete", "destroy"].includes(message.message_type || "") && markUnavailable(topic); } detectVanished(rawTopics, query) { detectVanishedTopics(this.retained, rawTopics, query); } rebuildUsers() { const next = {}; for (const topic of this.retained) { const id = topic.posters?.[0]?.user_id; if (id !== void 0 && this.displayUsers[id]) next[id] = this.displayUsers[id]; } this.displayUsers = next; } expireUnavailable(newEntrants) { if (!newEntrants) return; const expired = new Set(); for (const topic of this.retained) { if (!topic.sfpUnavailable) continue; topic.sfpUnavailablePushed = (topic.sfpUnavailablePushed || 0) + newEntrants; if (topic.sfpUnavailablePushed >= this.limit) expired.add(topic.id); } if (!expired.size) return; this.retained = this.retained.filter((topic) => !expired.has(topic.id)); expired.forEach((id) => this.loaded.delete(id)); this.rebuildUsers(); } trim() { let kept = 0; const trimmed = []; this.retained = this.retained.filter((topic) => { if (topic.sfpUnavailable) return true; if (kept++ < this.limit) return true; trimmed.push(topic.id); return false; }); if (!trimmed.length) return; trimmed.forEach((id) => this.loaded.delete(id)); this.rebuildUsers(); } merge(topics, options = {}) { if (!topics.length) return null; const { mode = "prepend", resetFeedDepth = true, moreTopicsUrl, sortQuery } = options; const fetched = new Map(topics.map((topic) => [topic.id, topic])); for (const resident of this.retained) { const replacement = fetched.get(resident.id); if (replacement && _isTopicRead(resident) && !_isTopicRead(replacement)) _applyReadMarker(replacement); } if (resetFeedDepth) this.depth = 0; const highlights = topics.filter((topic) => mode === "prepend" || !this.loaded.has(topic.id)).map((topic) => topic.id); this.retained = [...topics, ...this.retained.filter((topic) => !fetched.has(topic.id))]; if (mode === "replace-head") { if (sortQuery) interleaveUnavailableTopics(this.retained, sortQuery, topics.length); this.more = !!moreTopicsUrl; } else if (resetFeedDepth) this.more = this.retained.length >= Math.max(1, this.size); const entrants = topics.filter((topic) => !this.loaded.has(topic.id)).length; topics.forEach((topic) => this.loaded.add(topic.id)); this.expireUnavailable(entrants); this.trim(); return highlights; } }; function createFeedController({ storage, discourse, prefs, site, tags }) { const { CATEGORY_DATA_CACHE_KEY, TAG_STYLE_CACHE_KEY } = storage_keys_exports; const { delete: _deleteSiteValue } = storage; const { getCsrfToken, getDiscourse, getMessageBus, toAbsoluteSiteUrl, waitForStableHeaderMount } = discourse; const { t, getUiLocale, formatRelativeTime } = createI18n(getDiscourse); const { _getCategoryMeta, _findTabCategoryByTabId, loadCategoryMetadata } = site; const { loadTagStyleIndex } = tags; const scroll = createFeedScroll({ canLoadMore: () => resident.hasMore && !isLoadingMore, onLoadMore: () => loadMoreTopics({ source: "auto" }), onReadingState: () => _scheduleHeadActionStateSync() }); const FeedQuery = { snapshot: () => ({ tab: prefs.currentTab, categoryId: currentCategoryId, order: prefs.currentOrder, period: prefs.currentPeriod, filter: prefs.currentFilter }), key: feedQueryKey, isCurrent: (query) => feedQueryKey(query) === feedQueryKey(FeedQuery.snapshot()), buildUrl: (query, page) => buildFeedUrl(query, page, _getCategoryMeta) }; const api = createFeedApi({ buildUrl: FeedQuery.buildUrl, getCsrfToken, getSignal: () => requests.signal }); const fetchFeedTopics = api.page; const fetchFeedTopicsByIds = api.byIds; const fetchTopicView = api.topicView; const RouteWatcher = createRouteWatcher({ onRouteChange: () => { if (prefs.feedModeEnabled) _syncIncomingHeadAction(); }, onMutation: () => { if (!host.toggleBtn?.isConnected) createToggle(); if (!prefs.feedModeEnabled) return; const sidebar = getSidebarElement(); if (!sidebar) { if (host.feedContainer) deactivateFeed(); return; } const resizerMissing = !host.resizerEl || !sidebar?.contains(host.resizerEl); if (sidebar && (!host.feedContainer || !sidebar.contains(host.feedContainer) || !sidebar.classList.contains("sfp-feed-mode") || resizerMissing)) activateFeed(); } }); let currentCategoryId = null; const resident = new ResidentTopics(); const autoLoad = new AutoLoadGate(); let isLoading = false; let loadCycle = null; let isLoadingMore = false; let isRefreshing = false; const silentRefresh = new RefreshCountdown(); const automaticRefresh = new RefreshCountdown(); const activity = new PageActivity(); const appScope = new Lifetime(); let viewScope = new Lifetime(); let headActionScheduled = false; let started = false; let disposed = false; let active = false; let viewEpoch = 0; let requests = new AbortController(); const setTimeout = (callback, delay) => viewScope.timeout(callback, delay); const clearTimeout = (id) => viewScope.clearTimeout(id); const requestAnimationFrame = (callback) => viewScope.frame(callback); function invalidateRequests() { viewEpoch++; sidebarIncomingState.applyQueued = false; refreshBusyCount = 0; requests.abort(); requests = new AbortController(); activeLoadToken++; activeLoadMoreToken++; activeRefreshToken++; sidebarIncomingState.filterRefreshToken++; } const incoming = new IncomingTopics(); const reading = new ReadingState(); const sidebarIncomingState = { filterRefreshTimer: null, filterRefreshToken: 0, viewSettling: false, filterStable: false, applyQueued: false }; let sidebarMessageBus = null; let sidebarLatestMessageBusCallback = null; let sidebarNewMessageBusCallback = null; let sidebarLifecycleMessageBus = null; let sidebarLifecycleMessageBusCallback = null; let activeLoadToken = 0; let activeLoadMoreToken = 0; let activeRefreshToken = 0; let refreshBusyCount = 0; const topicRenderer = createTopicRenderer({ t, formatRelativeTime, site, tags, discourse, getUser: (id) => resident.users[id], markTopicAsRead, getScope: () => viewScope }); const host = createFeedHost({ t, getEnabled: () => prefs.feedModeEnabled, getWidth: () => prefs.sfpSidebarWidth, onToggle: () => { prefs.feedModeEnabled = !prefs.feedModeEnabled; if (prefs.feedModeEnabled) activateFeed(); else deactivateFeed(); }, onWidth: (width) => { prefs.sfpSidebarWidth = width; }, getScope: () => viewScope }); const { createToggle, getSidebarElement, removeResizer, restoreSidebarWidth } = host; const { createTopicItem } = topicRenderer; const readLaterStore = createReadLaterStore({ storage: resolveReadLaterStorage(), origin: location.origin }); const readLaterView = createReadLaterView({ t, discourse, renderer: topicRenderer, items: () => readLaterStore.items(), savedHeight: () => readLaterStore.height(), saveHeight: (value) => readLaterStore.setHeight(value), updateItems: (next) => readLaterStore.update(next), reloadItems: () => readLaterStore.reload(), onExternalItems: () => void syncReadLater(), removeItem: (url) => removeReadLaterItem(url), moveItem: (url, toIndex) => { const next = readLaterStore.move(url, toIndex); readLaterView.refresh(); return next; }, onAdd: (link, title) => addReadLaterLink(link, title), markRead: (url) => markReadLaterItem(url), fetchOnebox: (url, signal) => fetchOneboxPreview(url, signal), fetchPageHead: (url, signal) => fetchPageHead(url, signal), getFeedScroll: () => host.feedScrollEl, onLayoutChange: () => _scheduleHeadActionStateSync(), getScope: () => viewScope }); function fetchOneboxPreview(url, signal, opts) { const target = toAbsoluteSiteUrl(url); const query = new URLSearchParams({ url: target, refresh: "true" }); if (Number.isFinite(opts?.categoryId)) query.set("category_id", String(opts?.categoryId)); if (Number.isFinite(opts?.topicId)) query.set("topic_id", String(opts?.topicId)); return fetch(`/onebox?${query}`, { credentials: "include", headers: { "X-CSRF-Token": getCsrfToken(), "X-Requested-With": "XMLHttpRequest", Accept: "text/html, */*; q=0.01" }, signal }).then(async (response) => ({ status: response.status, html: response.ok ? await response.text() : "" })); } async function fetchTopicPreview(id, categoryId, signal) { try { const result = await fetchOneboxPreview(`/t/${id}`, signal, { categoryId, topicId: id }); if (result.status < 200 || result.status >= 300 || !result.html) return null; const preview = extractPreview(new DOMParser().parseFromString(result.html, "text/html"), location.origin); return preview.title || preview.name || preview.excerpt || preview.imageUrl ? preview : null; } catch { return null; } } function fetchPageHead(url, signal) { return fetch(toAbsoluteSiteUrl(url), { credentials: "include", headers: { Accept: "text/html, */*; q=0.01" }, signal }).then(async (response) => { if (!response.ok) return { status: response.status, head: "" }; const html = await response.text(); const end = html.indexOf(""); return { status: response.status, head: end > 0 ? html.slice(0, end + 7) : html }; }); } function addReadLaterLink(link, title) { const known = link.topicId ? resident.topics.find((topic) => topic.id === link.topicId) : void 0; const posterId = known?.posters?.[0]?.user_id; const cached = known ? void 0 : readLaterView.cachedTopic(link.url); readLaterStore.add({ url: link.url, kind: link.kind, topicId: link.topicId, title: title || (known ? selectTopicTitle(known) : cached?.title) || readLaterFallbackTitle(link.url), topic: known || cached?.topic, user: known ? posterId === void 0 ? void 0 : resident.users[posterId] : cached?.user }); readLaterView.refresh(); if (!known && !cached) syncReadLater(); } function removeReadLaterItem(url) { const next = readLaterStore.remove(url); if (next.length === 0) readLaterStore.setHeight(null); return next; } function markReadLaterItem(url) { readLaterStore.update(readLaterStore.items().map((item) => { if (item.url !== url || !item.topic) return item; const topic = { ...item.topic }; _applyReadMarker(topic); return { ...item, topic }; })); } async function syncReadLater() { if (disposed) return; const residentById = new Map(); for (const item of readLaterStore.items()) { if (!item.topicId || residentById.has(item.topicId)) continue; const known = resident.topics.find((topic) => topic.id === item.topicId); if (known) residentById.set(item.topicId, known); } const missing = [...new Set(readLaterStore.items().map((item) => item.topicId).filter((id) => Number.isFinite(id)).filter((id) => !residentById.has(id)))]; const topics = new Map(residentById); const users = new Map(); const previews = new Map(); const gone = new Set(); const itemById = new Map(); for (const item of readLaterStore.items()) if (item.topicId && !itemById.has(item.topicId)) itemById.set(item.topicId, item); for (let index = 0; index < missing.length; index += 50) { const chunk = missing.slice(index, index + 50); try { const data = await fetchFeedTopicsByIds(chunk, requests.signal); for (const topic of data?.topic_list?.topics || []) topics.set(topic.id, topic); for (const user of data?.users || []) users.set(user.id, user); } catch (e) { if (requests.signal.aborted) return; } } for (const id of missing) { if (topics.has(id) || disposed) continue; const item = itemById.get(id); let definitive = false; try { const result = await fetchTopicView(id, requests.signal); const parsed = result.json ? topicFromView(result.json) : null; if (parsed) { topics.set(id, parsed.topic); if (parsed.user) users.set(parsed.user.id, parsed.user); continue; } definitive = result.status === 403 || result.status === 404; } catch (e) { if (requests.signal.aborted) return; } if (definitive && item?.topic?.sfpUnavailable) { gone.add(id); continue; } const preview = await fetchTopicPreview(id, item?.topic?.category_id, requests.signal); if (preview) previews.set(id, preview); else if (definitive) gone.add(id); } if (disposed) return; let changed = false; const next = readLaterStore.items().map((item) => { if (!item.topicId) return item; const fresh = topics.get(item.topicId); if (!fresh) { const preview = previews.get(item.topicId); if (preview) { changed = true; return { ...item, topic: void 0, preview }; } if (!gone.has(item.topicId)) return item; const topic = { ...item.topic || { id: item.topicId } }; if (!markUnavailable(topic)) return item; changed = true; return { ...item, topic }; } const posterId = fresh.posters?.[0]?.user_id; const user = posterId === void 0 ? void 0 : users.get(posterId); const title = selectTopicTitle(fresh) || item.title; changed = true; return { ...item, topic: fresh, user: user || item.user, title, preview: void 0 }; }); if (changed) readLaterStore.update(next); readLaterView.refresh(); } function applyReadLaterSnapshot(topics, users = []) { const next = mergeTopicSnapshot(readLaterStore.items(), topics, users, selectTopicTitle); if (!next) return; readLaterStore.update(next); readLaterView.refresh(); } const controls = createControls({ renderTopics, _finishSidebarIncomingViewSettling, t, prefs, site, host, getScope: () => viewScope, updatePreference: (key, value) => { const changed = prefs[key] !== value; Object.assign(prefs, { [key]: value }); if (key === "currentFilter" && changed) { if (isLoading) loadTopics(); else { invalidateRequests(); isLoadingMore = isRefreshing = false; } } }, saveTabOrder: (ids) => prefs.saveTabOrder(ids), getCategoryId: () => currentCategoryId, setCategoryId: (id) => { currentCategoryId = id; }, _resetAutoLoadState, loadTopics, _beginSidebarIncomingViewSettling, _syncDefaultViewControls, _syncRefreshButtonBusy, _handleHeadActionClick, _syncSidebarIncomingTracking, _syncHeadActionState, _isAutoSilentRefreshActive, _queueSidebarIncomingApply, _startAutoSilentRefresh, _startAutoRefresh, _isLatestActivityView }); const { _getOrderOptions, _getPeriodOptions, _getFilterOptions, _closeFloatingPanels, _buildHeaderControls, _buildTabBar, _rerenderTabBar, _buildFilterBar, _refreshCategoryTabs, _updateSettingsControl } = controls; const topicList = createTopicList({ t, hasMore: () => resident.hasMore, loadMore: () => loadMoreTopics(), renderItem: createTopicItem, getScroll: () => host.feedScrollEl, getList: () => host.feedListEl, getScope: () => viewScope, syncHead: _syncHeadActionState, atHead: _isAtFeedHead }); const { _renderPaginationFooter, _showLoadMoreSpinner, _showNoMore, _showLoadMoreError } = topicList; let siteControlsSignature = ""; function _normalizeCurrentSiteState() { let changed = false; const orderOptions = _getOrderOptions(); const periodOptions = _getPeriodOptions(); const filterOptions = _getFilterOptions(); if (!site.capabilities.orderValues.has(prefs.currentOrder)) { prefs.currentOrder = orderOptions[0]?.value || "activity"; changed = true; } if (!site.capabilities.periodValues.has(prefs.currentPeriod)) { prefs.currentPeriod = site.capabilities.periodValues.has("all") ? "all" : periodOptions[0]?.value || "all"; changed = true; } if (!site.capabilities.filterValues.has(prefs.currentFilter)) { prefs.currentFilter = filterOptions[0]?.value || "all"; changed = true; } if (prefs.currentTab === "all") currentCategoryId = null; else { const tab = _findTabCategoryByTabId(prefs.currentTab); if (tab) { currentCategoryId = tab.id; if (prefs.currentTab !== tab.tabId) { prefs.currentTab = tab.tabId; changed = true; } } else { prefs.currentTab = "all"; currentCategoryId = null; changed = true; } } return changed; } function _siteControlsStateSignature() { return JSON.stringify({ locale: getUiLocale(), orders: _getOrderOptions().map((option) => option.value), periods: _getPeriodOptions().map((option) => option.value), filters: _getFilterOptions().map((option) => option.value), tabs: site.tabCategories.map((cat) => `${cat.id}:${cat.name}:${cat.icon}:${cat.color}`), currentTab: prefs.currentTab, currentOrder: prefs.currentOrder, currentPeriod: prefs.currentPeriod, currentFilter: prefs.currentFilter }); } function _syncSiteDerivedControls({ force = false } = {}) { const nextSignature = _siteControlsStateSignature(); if (!force && nextSignature === siteControlsSignature) return; siteControlsSignature = nextSignature; if (host.feedHeaderEl) { host.feedHeaderEl.replaceChildren(); _buildHeaderControls(host.feedHeaderEl); } const tabShell = host.feedContainer?.querySelector(".sfp-tab-shell"); if (tabShell) _rerenderTabBar(tabShell); const filterBar = host.feedContainer?.querySelector(".sfp-filter-bar"); if (filterBar) filterBar.replaceWith(_buildFilterBar()); } function clearDataCaches({ reload = false } = {}) { _deleteSiteValue(CATEGORY_DATA_CACHE_KEY); _deleteSiteValue(TAG_STYLE_CACHE_KEY); site.reset(); tags.reset(); console.info("[SFP] category data and tag style caches cleared"); if (reload && prefs.feedModeEnabled) { _resetAutoLoadState(); loadTopics(); } } function reloadForPageLocale() { if (disposed) return; site.reset(); tags.reset(); readLaterView.refresh(); if (!prefs.feedModeEnabled) return; _syncSiteDerivedControls({ force: true }); _resetAutoLoadState(); loadTopics(); } function activateFeed() { if (disposed || !prefs.feedModeEnabled) return; const sidebar = getSidebarElement(); if (!sidebar) return; active = true; if (host.feedContainer && sidebar.contains(host.feedContainer)) { host.ensure(); readLaterView.ensure(host.feedContainer); _startSidebarTopicLifecycleTracking(); _syncDefaultViewControls(); _syncIncomingHeadAction(); _syncHeadActionState(); return; } if (host.feedContainer) deactivateFeed(); active = true; if (!host.mount(_buildHeaderControls, _buildTabBar, _buildFilterBar)) return; if (host.feedContainer) readLaterView.mount(host.feedContainer); _restoreTabState(); _syncDefaultViewControls(); loadTopics(); scroll.bind(host.feedScrollEl); _startSidebarTopicLifecycleTracking(); } function deactivateFeed() { active = false; invalidateRequests(); viewScope.dispose(); viewScope = new Lifetime(); headActionScheduled = false; isLoading = false; loadCycle = null; isLoadingMore = false; isRefreshing = false; sidebarIncomingState.applyQueued = false; scroll.dispose(); _stopAutoRefresh(); _stopAutoSilentRefresh(); _stopSidebarIncomingTracking(); _stopSidebarTopicLifecycleTracking(); _resetRefreshButtonBusy(); controls.feedRefreshBtn?.classList.remove("sfp-back-top-enter"); readLaterView.destroy(); host.unmount(); controls.reset(); resident.reset(); _resetAutoLoadState(); } function _beginRefreshButtonBusy() { refreshBusyCount++; _syncRefreshButtonBusy(); let ended = false; const busyEpoch = viewEpoch; return () => { if (ended || busyEpoch !== viewEpoch) return; ended = true; refreshBusyCount = Math.max(0, refreshBusyCount - 1); _syncRefreshButtonBusy(); }; } function _syncRefreshButtonBusy() { if (controls.setBusy(refreshBusyCount > 0)) _syncHeadActionState(); } function _resetRefreshButtonBusy() { refreshBusyCount = 0; _syncRefreshButtonBusy(); } function _restoreTabState() { if (prefs.currentTab === "all") { currentCategoryId = null; return; } const cat = _findTabCategoryByTabId(prefs.currentTab); if (cat) { currentCategoryId = cat.id; prefs.currentTab = cat.tabId; } else if (site.loaded) { prefs.currentTab = "all"; currentCategoryId = null; } else currentCategoryId = null; } function _isAtFeedHead() { return reading.atHead(host.feedScrollEl?.scrollTop || 0); } function _getIncomingCountDisplayValue(query = FeedQuery.snapshot()) { if (!prefs.showIncomingHint || !_canShowSidebarIncomingHint(query)) return 0; if (!reading.isAway(host.feedScrollEl?.scrollTop || 0, host.feedScrollEl?.clientHeight || 0) && _isAutoSilentRefreshActive(query)) return 0; return sidebarIncomingState.filterStable ? incoming.matchingIds.length : 0; } function _restartAutomaticRefreshTimers() { if (isLoading || isLoadingMore || isRefreshing) return; _startAutoSilentRefresh(); _startAutoRefresh(); if (_isAtFeedHead() && _isAutoSilentRefreshActive() && prefs.autoSilentRefreshInterval === 0) _queueSidebarIncomingApply(); } function _isAutoSilentRefreshTimerExpected() { return _isAutoSilentRefreshActive() && prefs.autoSilentRefreshInterval > 0; } function _isAutoRefreshTimerExpected() { return prefs.autoRefreshEnabled && !_isLatestActivityView() && _isAtFeedHead(); } function _restoreMissingAutomaticRefreshTimers() { if (isLoading || isLoadingMore || isRefreshing) return; if (_isAutoSilentRefreshTimerExpected() && !silentRefresh.running) _startAutoSilentRefresh(); if (_isAutoRefreshTimerExpected() && !automaticRefresh.running) _startAutoRefresh(); } function _syncHeadActionState({ restartAuto = true } = {}) { if (!controls.feedRefreshBtn) return; const isAtHead = _isAtFeedHead(); const isAway = reading.isAway(host.feedScrollEl?.scrollTop || 0, host.feedScrollEl?.clientHeight || 0); const isBusy = refreshBusyCount > 0; const incomingCount = _getIncomingCountDisplayValue(); controls.renderHeadAction(isAway, isBusy, incomingCount); _syncRefreshButtonBusy(); if (reading.away !== isAway) { reading.away = isAway; if (isAway) { _stopAutoRefresh(); _stopAutoSilentRefresh(); } else if (restartAuto && isAtHead) _restartAutomaticRefreshTimers(); } else if (!isAway && restartAuto && isAtHead) _restoreMissingAutomaticRefreshTimers(); } function _scheduleHeadActionStateSync() { if (headActionScheduled) return; headActionScheduled = true; requestAnimationFrame(() => { headActionScheduled = false; _syncHeadActionState(); }); } async function _returnToHead({ animated = true, restartAuto = true } = {}) { if (!host.feedScrollEl) return false; host.feedScrollEl.scrollTo({ top: 0, behavior: animated ? "smooth" : "auto" }); if (!animated) { _syncHeadActionState({ restartAuto }); return _isAtFeedHead(); } const scrollResult = await topicList.waitForHead(); if (scrollResult === "timeout" && host.feedScrollEl && !_isAtFeedHead()) host.feedScrollEl.scrollTop = 0; _syncHeadActionState({ restartAuto }); return scrollResult !== "interrupted" && _isAtFeedHead(); } async function _handleHeadActionClick(event) { if (!controls.feedRefreshBtn || controls.feedRefreshBtn.getAttribute("aria-busy") === "true") return; const action = controls.feedRefreshBtn.dataset.action || "refresh"; if (event?.detail >= 2 && action === "back-top") { if (!_isAtFeedHead()) await _returnToHead({ animated: false }); else _syncHeadActionState(); return; } if (action === "incoming") { if (!_isAtFeedHead()) { if (!await _returnToHead({ animated: true, restartAuto: false })) { _syncHeadActionState(); return; } } await _applySidebarIncomingTopics({ requireDefaultView: true, logPrefix: "head action incoming", queueIfBusy: false, resetFeedDepth: true }); _restartAutomaticRefreshTimers(); _syncHeadActionState(); return; } if (action === "back-top") { await _returnToHead({ animated: true }); return; } await refreshCurrentView(); _syncHeadActionState(); } function _beginSidebarIncomingViewSettling() { sidebarIncomingState.viewSettling = true; sidebarIncomingState.filterStable = false; incoming.recompute(false, () => false); sidebarIncomingState.filterRefreshToken++; if (sidebarIncomingState.filterRefreshTimer) { clearTimeout(sidebarIncomingState.filterRefreshTimer); sidebarIncomingState.filterRefreshTimer = null; } _syncHeadActionState(); } function _finishSidebarIncomingViewSettling() { sidebarIncomingState.viewSettling = false; sidebarIncomingState.filterStable = false; _recomputeSidebarIncomingFilteredTopicIds(); _scheduleSidebarIncomingFilterRefresh(); _syncIncomingHeadAction(); if (_isAutoSilentRefreshActive() && prefs.autoSilentRefreshInterval === 0) _queueSidebarIncomingApply(); } function _syncIncomingHeadAction({ skipIncomingFilterRefresh = false } = {}) { if (!skipIncomingFilterRefresh) _scheduleSidebarIncomingFilterRefresh(); _syncHeadActionState(); } function _isLatestActivityView(query = FeedQuery.snapshot()) { return query.order === "activity"; } function _canUseSidebarIncomingRefresh(query = FeedQuery.snapshot()) { return _isLatestActivityView(query); } function _shouldUseSidebarIncomingQueue(query = FeedQuery.snapshot()) { return _canUseSidebarIncomingRefresh(query) && (prefs.showIncomingHint || prefs.autoSilentRefreshEnabled); } function _canShowSidebarIncomingHint(query = FeedQuery.snapshot()) { return prefs.showIncomingHint && _canUseSidebarIncomingRefresh(query) && query.filter === "all"; } function _isAutoSilentRefreshActive(query = FeedQuery.snapshot()) { return active && !disposed && prefs.autoSilentRefreshEnabled && _isAtFeedHead() && _canUseSidebarIncomingRefresh(query); } function _topicMatchesCategoryScope(topic, query = FeedQuery.snapshot()) { if (query.tab === "all" || !query.categoryId) return true; let categoryId = Number(topic?.category_id); const targetCategoryId = Number(query.categoryId); if (!Number.isFinite(categoryId) || !Number.isFinite(targetCategoryId)) return false; while (Number.isFinite(categoryId)) { if (categoryId === targetCategoryId) return true; const parentId = Number(_getCategoryMeta(categoryId)?.parent_category_id); if (!Number.isFinite(parentId) || parentId === categoryId) break; categoryId = parentId; } return false; } function _topicMatchesLocalFilter(topic, query = FeedQuery.snapshot()) { if (query.filter === "unseen") return _hasUnreadMarker(topic); if (query.filter === "read") return !_hasUnreadMarker(topic); return true; } function _topicMatchesIncomingView(topic, query = FeedQuery.snapshot()) { return _topicMatchesIncomingCandidate(topic, query) && _topicMatchesLocalFilter(topic, query); } function _topicMatchesIncomingCandidate(topic, query = FeedQuery.snapshot()) { return _canUseSidebarIncomingRefresh(query) && _topicMatchesCategoryScope(topic, query); } function _recomputeSidebarIncomingFilteredTopicIds(query = FeedQuery.snapshot()) { const enabled = _canUseSidebarIncomingRefresh(query); if (!enabled) sidebarIncomingState.filterStable = false; return incoming.recompute(enabled, (topic) => _topicMatchesIncomingCandidate(topic, query)); } function _scheduleSidebarIncomingFilterRefresh() { if (sidebarIncomingState.viewSettling || isLoading || isRefreshing) return; if (!_canUseSidebarIncomingRefresh() || incoming.ids.length === 0) return; if (sidebarIncomingState.filterRefreshTimer) return; sidebarIncomingState.filterRefreshTimer = setTimeout(() => { sidebarIncomingState.filterRefreshTimer = null; _refreshSidebarIncomingFilter().catch((e) => { console.warn("[SFP] incoming filter refresh error:", e); }); }, 150); } async function _refreshSidebarIncomingFilter() { const requestQuery = FeedQuery.snapshot(); if (sidebarIncomingState.viewSettling || isLoading || isRefreshing) return incoming.matchingIds; if (!_canUseSidebarIncomingRefresh(requestQuery)) { _recomputeSidebarIncomingFilteredTopicIds(requestQuery); _syncIncomingHeadAction({ skipIncomingFilterRefresh: true }); return []; } const incomingTopicIds = incoming.ids.slice(); const token = ++sidebarIncomingState.filterRefreshToken; if (incomingTopicIds.length === 0) { incoming.recompute(false, () => false); sidebarIncomingState.filterStable = true; _syncIncomingHeadAction({ skipIncomingFilterRefresh: true }); return []; } await loadCategoryMetadata(); if (token !== sidebarIncomingState.filterRefreshToken || !FeedQuery.isCurrent(requestQuery)) return incoming.matchingIds; _recomputeSidebarIncomingFilteredTopicIds(requestQuery); sidebarIncomingState.filterStable = true; _syncIncomingHeadAction({ skipIncomingFilterRefresh: true }); return incoming.matchingIds; } function _syncDefaultViewControls() { _syncSidebarIncomingTracking(); _recomputeSidebarIncomingFilteredTopicIds(); _updateSettingsControl(); _syncIncomingHeadAction(); _syncHeadActionState(); _scheduleSidebarIncomingFilterRefresh(); if (_isAutoSilentRefreshActive() && prefs.autoSilentRefreshInterval === 0) _queueSidebarIncomingApply(); } function _startSidebarIncomingTracking() { if (!_shouldUseSidebarIncomingQueue()) return; if (sidebarLatestMessageBusCallback || sidebarNewMessageBusCallback) return; const messageBus = getMessageBus(); if (!messageBus?.subscribe) { console.warn("[SFP] message-bus service unavailable; incoming topics will not auto-update"); return; } sidebarMessageBus = messageBus; sidebarLatestMessageBusCallback = (data) => _handleSidebarIncomingMessage(data); sidebarNewMessageBusCallback = (data) => _handleSidebarIncomingMessage(data); messageBus.subscribe("/latest", sidebarLatestMessageBusCallback, _getMessageBusLastId(messageBus, "/latest")); messageBus.subscribe("/new", sidebarNewMessageBusCallback, _getMessageBusLastId(messageBus, "/new")); } function _syncSidebarIncomingTracking() { if (prefs.feedModeEnabled && _shouldUseSidebarIncomingQueue()) _startSidebarIncomingTracking(); else _stopSidebarIncomingTracking(); } function _getMessageBusLastId(messageBus, channel) { const candidates = [ messageBus?.lastId?.(channel), messageBus?.lastIdForChannel?.(channel), messageBus?.lastIds?.[channel], messageBus?.last_ids?.[channel], messageBus?.channels?.[channel]?.lastId ]; for (const candidate of candidates) { const lastId = Number(candidate); if (Number.isFinite(lastId)) return lastId; } return -1; } function _stopSidebarIncomingTracking() { if (sidebarMessageBus?.unsubscribe) { if (sidebarLatestMessageBusCallback) sidebarMessageBus.unsubscribe("/latest", sidebarLatestMessageBusCallback); if (sidebarNewMessageBusCallback) sidebarMessageBus.unsubscribe("/new", sidebarNewMessageBusCallback); } sidebarMessageBus = null; sidebarLatestMessageBusCallback = null; sidebarNewMessageBusCallback = null; _clearSidebarIncomingCandidates(); } function _startSidebarTopicLifecycleTracking() { if (!prefs.feedModeEnabled) return; if (sidebarLifecycleMessageBusCallback) return; const messageBus = getMessageBus(); if (!messageBus?.subscribe) return; sidebarLifecycleMessageBus = messageBus; sidebarLifecycleMessageBusCallback = (data) => _handleSidebarTopicLifecycleMessage(data); messageBus.subscribe("/delete", sidebarLifecycleMessageBusCallback, _getMessageBusLastId(messageBus, "/delete")); messageBus.subscribe("/recover", sidebarLifecycleMessageBusCallback, _getMessageBusLastId(messageBus, "/recover")); messageBus.subscribe("/destroy", sidebarLifecycleMessageBusCallback, _getMessageBusLastId(messageBus, "/destroy")); } function _stopSidebarTopicLifecycleTracking() { if (sidebarLifecycleMessageBus?.unsubscribe && sidebarLifecycleMessageBusCallback) { sidebarLifecycleMessageBus.unsubscribe("/delete", sidebarLifecycleMessageBusCallback); sidebarLifecycleMessageBus.unsubscribe("/recover", sidebarLifecycleMessageBusCallback); sidebarLifecycleMessageBus.unsubscribe("/destroy", sidebarLifecycleMessageBusCallback); } sidebarLifecycleMessageBus = null; sidebarLifecycleMessageBusCallback = null; } function _handleSidebarTopicLifecycleMessage(data) { if (resident.lifecycle(data) && host.feedListEl) renderTopics(); } function _handleSidebarIncomingMessage(data) { if (!data || !["latest", "new_topic"].includes(data.message_type || "")) return; if (!data.topic_id) return; if (!_shouldUseSidebarIncomingQueue()) { _clearSidebarIncomingCandidates(); return; } if (data.payload?.archetype && data.payload.archetype !== "regular") return; incoming.touch(data.topic_id, data.payload); sidebarIncomingState.filterStable = false; if (_isAutoSilentRefreshActive() && prefs.autoSilentRefreshInterval === 0) _queueSidebarIncomingApply(); else if (_canUseSidebarIncomingRefresh()) { _recomputeSidebarIncomingFilteredTopicIds(); sidebarIncomingState.filterStable = true; _syncIncomingHeadAction({ skipIncomingFilterRefresh: true }); } } function _clearSidebarIncomingCandidates() { incoming.clear(); sidebarIncomingState.filterStable = false; sidebarIncomingState.applyQueued = false; if (sidebarIncomingState.filterRefreshTimer) { clearTimeout(sidebarIncomingState.filterRefreshTimer); sidebarIncomingState.filterRefreshTimer = null; } sidebarIncomingState.filterRefreshToken++; _syncHeadActionState(); } function _resetSidebarIncomingCandidatesForQueryChange() { incoming.clear(); sidebarIncomingState.filterStable = false; sidebarIncomingState.applyQueued = false; if (sidebarIncomingState.filterRefreshTimer) { clearTimeout(sidebarIncomingState.filterRefreshTimer); sidebarIncomingState.filterRefreshTimer = null; } sidebarIncomingState.filterRefreshToken++; _syncHeadActionState(); } function _getSidebarIncomingLoadTopicIds() { return incoming.loadIds(resident.pageSize); } function _removeSidebarIncomingTopicIds(topicIds) { incoming.remove(topicIds); _recomputeSidebarIncomingFilteredTopicIds(); } function _removeSidebarIncomingTopicsForQuery(query = FeedQuery.snapshot()) { if (!incoming.ids.length) return; _removeSidebarIncomingTopicIds(incoming.ids.filter((id) => { const topic = incoming.get(Number(id)); return topic && _topicMatchesIncomingCandidate(topic, query); })); } function _queueSidebarIncomingApply() { if (sidebarIncomingState.viewSettling) return; if (!_canUseSidebarIncomingRefresh()) return; if (!_isAutoSilentRefreshActive() || prefs.autoSilentRefreshInterval > 0) return; if (_shouldSkipAutomaticRefresh()) return; if (incoming.ids.length === 0) return; if (sidebarIncomingState.applyQueued) return; sidebarIncomingState.applyQueued = true; const queuedEpoch = viewEpoch; Promise.resolve().then(async () => { if (disposed || queuedEpoch !== viewEpoch) return; sidebarIncomingState.applyQueued = false; if (!_isAutoSilentRefreshActive() || prefs.autoSilentRefreshInterval > 0) return; await _applySidebarIncomingTopics({ requireDefaultView: true, logPrefix: "auto silent refresh" }); }); } function _flushQueuedSidebarIncomingApply() { if (!sidebarIncomingState.applyQueued) return; if (!_isAutoSilentRefreshActive() || prefs.autoSilentRefreshInterval > 0) { sidebarIncomingState.applyQueued = false; return; } sidebarIncomingState.applyQueued = false; _queueSidebarIncomingApply(); } function _getAutoLoadSessionKey() { return FeedQuery.key(FeedQuery.snapshot()); } function _resetAutoLoadState() { autoLoad.reset(_getAutoLoadSessionKey()); } function _canRunAutoLoad() { return autoLoad.canRun(_getAutoLoadSessionKey()); } function _recordAutoLoadRequest() { autoLoad.recordRequest(_getAutoLoadSessionKey()); } function _recordAutoLoadFilterResult(count) { autoLoad.recordResult(_getAutoLoadSessionKey(), count); } function _shouldExcludeTopReadPinnedTopics(query, page) { return prefs.hidePinned && page === 0 && _isLatestActivityView(query); } function _excludeTopReadPinnedTopics(topics, query, page) { if (!_shouldExcludeTopReadPinnedTopics(query, page) || !Array.isArray(topics)) return topics; let firstNonPinnedIndex = 0; while (firstNonPinnedIndex < topics.length && _isPinnedTopic(topics[firstNonPinnedIndex])) firstNonPinnedIndex++; if (firstNonPinnedIndex === 0) return topics; const topPinnedTopics = topics.slice(0, firstNonPinnedIndex); const visibleTopPinnedTopics = topPinnedTopics.filter((topic) => _hasUnreadMarker(topic)); if (visibleTopPinnedTopics.length === topPinnedTopics.length) return topics; return visibleTopPinnedTopics.concat(topics.slice(firstNonPinnedIndex)); } function _startTagStyleIndexLoad() { if (tags.loaded || tags.loading) return; loadTagStyleIndex().then(() => { if (prefs.feedModeEnabled && host.feedListEl && resident.topics.length > 0) renderTopics(); }); } async function loadTopics() { if (disposed || !prefs.feedModeEnabled || !host.feedListEl) return; if (loadCycle) { loadCycle.pending = true; invalidateRequests(); return; } const cycle = { pending: false }; loadCycle = cycle; invalidateRequests(); let requestQuery = null; const requestToken = ++activeLoadToken; activeLoadMoreToken++; activeRefreshToken++; _beginSidebarIncomingViewSettling(); _resetSidebarIncomingCandidatesForQueryChange(); isLoading = true; isLoadingMore = false; isRefreshing = false; resident.reset({ clearUsers: false }); if (host.feedScrollEl) host.feedScrollEl.scrollTop = 0; reading.away = null; _syncIncomingHeadAction(); _resetAutoLoadState(); reading.away = null; topicList.showLoading(); _syncHeadActionState(); try { await loadCategoryMetadata(); _startTagStyleIndexLoad(); if (requestToken !== activeLoadToken) return; requestQuery = FeedQuery.snapshot(); _resetAutoLoadState(); _syncSiteDerivedControls(); if (requestToken !== activeLoadToken || !FeedQuery.isCurrent(requestQuery)) return; _refreshCategoryTabs(); const data = await fetchFeedTopics(requestQuery, 0); if (requestToken !== activeLoadToken || !FeedQuery.isCurrent(requestQuery)) return; _processUsers(data); if (data?.topic_list?.topics) { const rawTopics = data.topic_list.topics; const topics = _excludeTopReadPinnedTopics(rawTopics, requestQuery, 0); resident.loadHead(topics, rawTopics.length, data.topic_list.more_topics_url); renderTopics(); syncReadLater(); _removeSidebarIncomingTopicIds(topics.map((topic) => topic.id)); _syncIncomingHeadAction(); } else { topicList.showMessage(t("emptyTopics")); resident.endPages(); } } catch (e) { if (requestToken !== activeLoadToken || requestQuery && !FeedQuery.isCurrent(requestQuery)) return; console.error("[SFP] loadTopics error:", e); topicList.showError(e, () => { loadTopics(); }); } finally { if (loadCycle === cycle) { loadCycle = null; isLoading = false; if (cycle.pending) loadTopics(); else { _flushQueuedSidebarIncomingApply(); _finishSidebarIncomingViewSettling(); _restartAutomaticRefreshTimers(); } } } } async function loadMoreTopics({ source = "manual" } = {}) { if (disposed || !prefs.feedModeEnabled || isLoading || isLoadingMore || !resident.hasMore) return; const isAutoLoad = source === "auto"; if (isAutoLoad && !_canRunAutoLoad()) return; const requestQuery = FeedQuery.snapshot(); const requestToken = ++activeLoadMoreToken; isLoadingMore = true; if (isAutoLoad) _recordAutoLoadRequest(); _showLoadMoreSpinner(); try { await loadCategoryMetadata(); if (requestToken !== activeLoadMoreToken || !FeedQuery.isCurrent(requestQuery)) return; const data = await fetchFeedTopics(requestQuery, resident.page + 1); if (requestToken !== activeLoadMoreToken || !FeedQuery.isCurrent(requestQuery)) return; _processUsers(data); if (data?.topic_list?.topics) { const topics = data.topic_list.topics; const newTopics = resident.append(topics, data.topic_list.more_topics_url); if (newTopics.length === 0) { if (resident.hasMore) _renderPaginationFooter({ note: !isAutoLoad ? t("nextPageNoMatch") : "" }); else _showNoMore(); } else { const filteredNew = _applyFilter(newTopics); topicList.append(filteredNew); if (isAutoLoad) _recordAutoLoadFilterResult(filteredNew.length); _renderPaginationFooter({ note: !isAutoLoad && filteredNew.length === 0 ? t("nextPageNoMatch") : "" }); } } else { resident.endPages(); _showNoMore(); } } catch (e) { if (requestToken !== activeLoadMoreToken || !FeedQuery.isCurrent(requestQuery)) return; console.error("[SFP] loadMoreTopics error:", e); _showLoadMoreError(e); } finally { if (requestToken === activeLoadMoreToken) { isLoadingMore = false; _flushQueuedSidebarIncomingApply(); } } } function _processUsers(data) { resident.addUsers(data?.users); } async function refreshCurrentView() { return _refreshCurrentView({ logPrefix: "manual refresh" }); } function _mergeAndRenderTopics(fetchedTopics, { mode = "prepend", moreTopicsUrl = "", incomingCandidateIds = [], filterTopic = null, clearIncomingForQuery = null, resetFeedDepth = true, sortQuery = null } = {}) { const topics = mode !== "replace-head" && typeof filterTopic === "function" ? fetchedTopics.filter((topic) => filterTopic?.(topic)) : fetchedTopics; if (topics.length === 0) { if (incomingCandidateIds.length) _removeSidebarIncomingTopicIds(incomingCandidateIds); _syncIncomingHeadAction(); return false; } const highlightTopicIds = resident.merge(topics, { mode, moreTopicsUrl, resetFeedDepth, sortQuery }) || []; const scrollAnchor = topicList.captureAnchor(); renderTopics(highlightTopicIds); if (clearIncomingForQuery) _removeSidebarIncomingTopicsForQuery(clearIncomingForQuery); else if (incomingCandidateIds.length) _removeSidebarIncomingTopicIds(incomingCandidateIds); _syncIncomingHeadAction(); topicList.restoreAnchor(scrollAnchor); _syncHeadActionState(); return true; } async function _refreshCurrentView({ requireDefaultView = false, logPrefix = "refresh" } = {}) { if (disposed || !prefs.feedModeEnabled || isLoading || isLoadingMore || isRefreshing) return false; if (requireDefaultView && !_canUseSidebarIncomingRefresh()) return false; if (!host.feedListEl) return false; const requestQuery = FeedQuery.snapshot(); const requestToken = ++activeRefreshToken; const endRefreshBusy = _beginRefreshButtonBusy(); isRefreshing = true; try { await loadCategoryMetadata(); if (requestToken !== activeRefreshToken || !FeedQuery.isCurrent(requestQuery)) return false; const data = await fetchFeedTopics(requestQuery, 0); if (requestToken !== activeRefreshToken || !FeedQuery.isCurrent(requestQuery)) return false; _processUsers(data); if (!data?.topic_list?.topics) return false; resident.detectVanished(data.topic_list.topics, requestQuery); return _mergeAndRenderTopics(_excludeTopReadPinnedTopics(data.topic_list.topics, requestQuery, 0), { mode: "replace-head", moreTopicsUrl: data.topic_list.more_topics_url, clearIncomingForQuery: requestQuery, resetFeedDepth: true, sortQuery: requestQuery }); } catch (e) { console.warn(`[SFP] ${logPrefix} error:`, e); return false; } finally { if (requestToken === activeRefreshToken) { isRefreshing = false; _resetAutoRefreshCountdown(); _flushQueuedSidebarIncomingApply(); _syncHeadActionState(); syncReadLater(); } endRefreshBusy(); } } async function _applySidebarIncomingTopics({ requireDefaultView = false, logPrefix = "incoming", queueIfBusy = true, resetFeedDepth = true } = {}) { if (sidebarIncomingState.viewSettling || isLoading || isLoadingMore || isRefreshing) { if (queueIfBusy) sidebarIncomingState.applyQueued = true; return; } if (requireDefaultView && !_canUseSidebarIncomingRefresh()) return; if (!host.feedListEl) return; if (disposed || !prefs.feedModeEnabled) return; const applyEpoch = viewEpoch; const endRefreshBusy = _beginRefreshButtonBusy(); let requestToken = null; try { const incomingCandidateIds = await _refreshSidebarIncomingFilter(); if (disposed || applyEpoch !== viewEpoch) return; const incomingTopicIds = _getSidebarIncomingLoadTopicIds(); if (incomingTopicIds.length === 0) return; if (isLoading || isLoadingMore || isRefreshing) { if (queueIfBusy) sidebarIncomingState.applyQueued = true; return; } const requestQuery = FeedQuery.snapshot(); requestToken = ++activeRefreshToken; isRefreshing = true; await loadCategoryMetadata(); if (requestToken !== activeRefreshToken || !FeedQuery.isCurrent(requestQuery)) return; const data = await fetchFeedTopicsByIds(incomingTopicIds); if (requestToken !== activeRefreshToken || !FeedQuery.isCurrent(requestQuery)) return; if (!data?.topic_list?.topics) return; _processUsers(data); applyReadLaterSnapshot(data.topic_list.topics, data.users); _mergeAndRenderTopics(data.topic_list.topics, { mode: "prepend", incomingCandidateIds, filterTopic: (topic) => _topicMatchesIncomingView(topic, requestQuery), resetFeedDepth }); } catch (e) { console.warn(`[SFP] ${logPrefix} error:`, e); } finally { if (requestToken === activeRefreshToken) { isRefreshing = false; _flushQueuedSidebarIncomingApply(); _syncHeadActionState(); } endRefreshBusy(); } } function _startAutoSilentRefresh() { _stopAutoSilentRefresh(); if (!_isAutoSilentRefreshActive()) return; if (prefs.autoSilentRefreshInterval <= 0) return; _resetAutoSilentRefreshCountdown(); silentRefresh.start(prefs.autoSilentRefreshInterval, () => { if (prefs.feedModeEnabled && !isLoading && !isLoadingMore && !isRefreshing && !_shouldSkipAutomaticRefresh()) { if (_canUseSidebarIncomingRefresh()) _applySidebarIncomingTopics({ requireDefaultView: true, logPrefix: "auto silent refresh interval", queueIfBusy: false }); else _refreshCurrentView({ logPrefix: "auto silent refresh interval" }); } }); } function _resetAutoSilentRefreshCountdown() { if (_isAutoSilentRefreshActive() && prefs.autoSilentRefreshInterval > 0) silentRefresh.reset(prefs.autoSilentRefreshInterval); } function _stopAutoSilentRefresh() { silentRefresh.stop(); } function _isPageIdleForAutoRefresh() { return activity.idle(document.visibilityState === "hidden"); } function _shouldSkipAutomaticRefresh() { return _isPageIdleForAutoRefresh() || !_isAtFeedHead(); } function _setupPageActivityTracking() { activity.start(appScope); } function _startAutoRefresh() { _stopAutoRefresh(); if (!active || disposed) return; if (_isLatestActivityView()) return; if (!prefs.autoRefreshEnabled) return; if (!_isAtFeedHead()) return; _resetAutoRefreshCountdown(); automaticRefresh.start(prefs.autoRefreshInterval, () => { if (prefs.feedModeEnabled && !isLoading && !isLoadingMore && !_shouldSkipAutomaticRefresh()) _refreshCurrentView({ logPrefix: "auto refresh" }); }); } function _resetAutoRefreshCountdown() { if (prefs.autoRefreshEnabled) automaticRefresh.reset(prefs.autoRefreshInterval); } function _stopAutoRefresh() { automaticRefresh.stop(); } function renderTopics(newTopicIds = []) { const filtered = _applyFilter(resident.topics); let emptyMessage = t("emptyTopics"); if (resident.topics.length) { emptyMessage = prefs.currentFilter === "unseen" ? t(prefs.currentTab !== "all" ? "noUnreadInCategory" : "noUnread") : t(prefs.currentFilter === "read" ? "noRead" : "noMatchingTopics"); if (resident.hasMore && !isLoadingMore) emptyMessage = t("currentPagePrefix") + emptyMessage; } topicList.render(filtered, emptyMessage, newTopicIds); _syncHeadActionState(); } function markTopicAsRead(topic, itemElement) { if (resident.markRead(topic)) topicRenderer.markRead(itemElement); } function _applyFilter(topics) { if (prefs.currentFilter === "all") return topics; let result = topics; if (prefs.currentFilter === "unseen") result = result.filter((t) => _hasUnreadMarker(t)); if (prefs.currentFilter === "read") result = result.filter((t) => !_hasUnreadMarker(t)); return result; } function init() { if (started || disposed) return; started = true; appScope.listen(document, "click", () => _closeFloatingPanels()); _setupPageActivityTracking(); controls.start(); waitForStableHeaderMount(() => { if (disposed) return; createToggle(); RouteWatcher.start(); if (prefs.feedModeEnabled) appScope.timeout(() => activateFeed(), 300); else { removeResizer(); restoreSidebarWidth(); } }); } function dispose() { if (disposed) return; disposed = true; RouteWatcher.stop(); deactivateFeed(); viewScope.dispose(); appScope.dispose(); host.dispose(); controls.dispose(); restoreSidebarWidth(); } return { start: init, activate: () => { prefs.feedModeEnabled = true; activateFeed(); }, deactivate: () => { prefs.feedModeEnabled = false; deactivateFeed(); }, dispose, clearCaches: clearDataCaches, reloadForPageLocale, normalizeSiteState: _normalizeCurrentSiteState }; } function createDiscourseBridge(pageWindow) { let scope = new Lifetime(); let cachedCsrfToken = null; function getCsrfToken() { if (cachedCsrfToken === null) cachedCsrfToken = document.querySelector("meta[name=\"csrf-token\"]")?.getAttribute("content") || ""; return cachedCsrfToken; } function getPageLocale() { const root = document.documentElement; return (root?.getAttribute("lang") || root?.getAttribute("xml:lang") || "").trim().toLowerCase().replace(/_/g, "-"); } function observePageLocale(onChange) { const root = document.documentElement; if (!root) return; const initial = getPageLocale(); let reported = false; const observer = new MutationObserver(() => { if (reported || getPageLocale() === initial) return; reported = true; onChange(); }); observer.observe(root, { attributes: true, attributeFilter: ["lang", "xml:lang"] }); scope.defer(() => observer.disconnect()); } function toAbsoluteSiteUrl(path) { if (!path) return ""; return new URL(path, location.origin).href; } function navigateTo(path) { const script = document.createElement("script"); script.textContent = `window.require("discourse/lib/url").default.routeTo(${JSON.stringify(path)});`; document.documentElement.appendChild(script); script.remove(); } function getDiscourse() { try { return pageWindow?.Discourse || typeof window !== "undefined" && window.Discourse || typeof Discourse !== "undefined" && Discourse || null; } catch (e) { return null; } } function getMessageBus() { try { return getDiscourse()?.__container__?.lookup("service:message-bus") || null; } catch (e) { return null; } } function isLoggedIn() { try { const user = getDiscourse()?.__container__?.lookup("service:current-user"); if (user && Number.isFinite(user.id)) return true; if (user === null) return false; } catch (e) {} try { const preloaded = document.getElementById("data-preloaded")?.textContent; if (preloaded) return preloaded.includes("\"currentUser\":"); } catch (e) {} return false; } function getAvatarUrl(template, size) { if (!template) return ""; let url = template.replace("{size}", String(size)); if (!url.startsWith("http")) url = toAbsoluteSiteUrl(url); return url; } function getUserProfileUrl(username) { if (!username) return ""; return `/u/${encodeURIComponent(username)}`; } function openPathInNewTab(path) { window.open(toAbsoluteSiteUrl(path), "_blank", "noopener,noreferrer"); } function handlePointerNavigation(e, path, { onPrimaryActivate, onMiddleActivate } = {}) { if (!path) return false; const isPrimaryClick = e.type === "click" && e.button === 0; const isMiddleClick = e.type === "auxclick" && e.button === 1; if (!isPrimaryClick && !isMiddleClick) return false; e.preventDefault(); e.stopPropagation(); if (isPrimaryClick && (e.metaKey || e.ctrlKey || e.shiftKey || e.altKey)) { onPrimaryActivate?.(); openPathInNewTab(path); return true; } if (isPrimaryClick) { onPrimaryActivate?.(); navigateTo(path); return true; } onMiddleActivate?.(); openPathInNewTab(path); return true; } function waitForEmber(callback, maxWait = 15e3) { const start = Date.now(); function check() { if (scope.disposed) return; try { if (getDiscourse()?.__container__) { callback(); return; } } catch (e) {} if (Date.now() - start < maxWait) scope.timeout(check, 500); else console.warn("[SFP] Timed out waiting for Ember"); } check(); } function waitForStableHeaderMount(callback) { waitForEmber(() => { const run = () => { scope.frame(() => { scope.frame(() => { callback(); }); }); }; if (document.readyState === "complete") { run(); return; } scope.listen(window, "load", run, { once: true }); }); } return { dispose: () => scope.dispose(), getCsrfToken, getPageLocale, observePageLocale, toAbsoluteSiteUrl, navigateTo, getDiscourse, getMessageBus, isLoggedIn, getAvatarUrl, getUserProfileUrl, openPathInNewTab, handlePointerNavigation, waitForEmber, waitForStableHeaderMount }; } var _GM_addStyle = (() => typeof GM_addStyle != "undefined" ? GM_addStyle : void 0)(); var _GM_deleteValue = (() => typeof GM_deleteValue != "undefined" ? GM_deleteValue : void 0)(); var _GM_getValue = (() => typeof GM_getValue != "undefined" ? GM_getValue : void 0)(); var _GM_registerMenuCommand = (() => typeof GM_registerMenuCommand != "undefined" ? GM_registerMenuCommand : void 0)(); var _GM_setValue = (() => typeof GM_setValue != "undefined" ? GM_setValue : void 0)(); var _unsafeWindow = (() => typeof unsafeWindow != "undefined" ? unsafeWindow : void 0)(); var gm = { get: _GM_getValue, set: _GM_setValue, delete: (key) => { if (typeof _GM_deleteValue === "function") _GM_deleteValue(key); }, addStyle: _GM_addStyle, registerMenu: (label, callback) => { if (typeof _GM_registerMenuCommand === "function") _GM_registerMenuCommand(label, callback); }, pageWindow: () => typeof _unsafeWindow !== "undefined" ? _unsafeWindow : window }; function createSiteStorage(origin, values) { const keyFor = (key) => `${SITE_STORAGE_PREFIX}:${encodeURIComponent(origin)}:${key}`; return { get: (key, fallback) => values.get(keyFor(key), fallback), set: (key, value) => values.set(keyFor(key), value), delete: (key) => values.delete(keyFor(key)), migrateLegacy() { if (origin !== "https://linux.do") return; for (const key of SITE_SCOPED_STORAGE_KEYS) { const legacy = values.get(key, STORAGE_MISSING); if (legacy === "__SFP_STORAGE_MISSING__") continue; if (values.get(keyFor(key), "__SFP_STORAGE_MISSING__") === "__SFP_STORAGE_MISSING__") values.set(keyFor(key), legacy); values.delete(key); } } }; } var Preferences = class { storage; _feedModeEnabled; _currentOrder; _currentPeriod; _sfpSidebarWidth; _currentTab; _currentFilter; _hidePinned; _showIncomingHint; _autoSilentRefreshEnabled; _autoSilentRefreshInterval; _autoRefreshEnabled; _autoRefreshInterval; constructor(storage) { this.storage = storage; this._feedModeEnabled = storage.get(STATE_KEY, false); this._currentOrder = storage.get(ORDER_KEY, "activity"); if (this._currentOrder === "default") { this._currentOrder = "activity"; storage.set(ORDER_KEY, this._currentOrder); } this._currentPeriod = storage.get(PERIOD_KEY, "all"); this._sfpSidebarWidth = storage.get(WIDTH_KEY, 272); this._currentTab = storage.get(TAB_KEY, "all"); this._currentFilter = storage.get(FILTER_KEY, "all"); this._hidePinned = storage.get(HIDE_PINNED_KEY, false); this._showIncomingHint = storage.get(SHOW_INCOMING_HINT_KEY, true); this._autoSilentRefreshEnabled = storage.get(AUTO_SILENT_REFRESH_KEY, false); this._autoSilentRefreshInterval = Math.max(0, Number(storage.get("sfp_auto_silent_refresh_interval", 0)) || 0); this._autoRefreshEnabled = storage.get(AUTO_REFRESH_ENABLED_KEY, false); this._autoRefreshInterval = Math.max(1, Number(storage.get("sfp_auto_refresh_interval", 10)) || 10); } get feedModeEnabled() { return this._feedModeEnabled; } set feedModeEnabled(value) { this._feedModeEnabled = value; this.storage.set(STATE_KEY, value); } get currentOrder() { return this._currentOrder; } set currentOrder(value) { this._currentOrder = value; this.storage.set(ORDER_KEY, value); } get currentPeriod() { return this._currentPeriod; } set currentPeriod(value) { this._currentPeriod = value; this.storage.set(PERIOD_KEY, value); } get sfpSidebarWidth() { return this._sfpSidebarWidth; } set sfpSidebarWidth(value) { this._sfpSidebarWidth = value; this.storage.set(WIDTH_KEY, value); } get currentTab() { return this._currentTab; } set currentTab(value) { this._currentTab = value; this.storage.set(TAB_KEY, value); } get currentFilter() { return this._currentFilter; } set currentFilter(value) { this._currentFilter = value; this.storage.set(FILTER_KEY, value); } get hidePinned() { return this._hidePinned; } set hidePinned(value) { this._hidePinned = value; this.storage.set(HIDE_PINNED_KEY, value); } get showIncomingHint() { return this._showIncomingHint; } set showIncomingHint(value) { this._showIncomingHint = value; this.storage.set(SHOW_INCOMING_HINT_KEY, value); } get autoSilentRefreshEnabled() { return this._autoSilentRefreshEnabled; } set autoSilentRefreshEnabled(value) { this._autoSilentRefreshEnabled = value; this.storage.set(AUTO_SILENT_REFRESH_KEY, value); } get autoSilentRefreshInterval() { return this._autoSilentRefreshInterval; } set autoSilentRefreshInterval(value) { this._autoSilentRefreshInterval = value; this.storage.set(AUTO_SILENT_REFRESH_INTERVAL_KEY, value); } get autoRefreshEnabled() { return this._autoRefreshEnabled; } set autoRefreshEnabled(value) { this._autoRefreshEnabled = value; this.storage.set(AUTO_REFRESH_ENABLED_KEY, value); } get autoRefreshInterval() { return this._autoRefreshInterval; } set autoRefreshInterval(value) { this._autoRefreshInterval = value; this.storage.set(AUTO_REFRESH_INTERVAL_KEY, value); } saveTabOrder(ids) { this.storage.set(TAB_ORDER_KEY, ids); } }; function createTagStyles({ storage, site, getPageLocale }) { let scope = new Lifetime(); const _getSiteValue = storage.get; const _setSiteValue = storage.set; const { loadSiteData, _extractPreloadedSiteData } = site; const tagStyleByKey = new Map(); let tagStylePromise = null; let tagStyleLoaded = false; function _tagIndexKeys(tag) { const keys = []; const add = (value) => { if (value === null || value === void 0) return; const key = String(value).trim().toLowerCase(); if (key && !keys.includes(key)) keys.push(key); }; if (typeof tag === "string") { add(tag); return keys; } add(tag?.name); add(tag?.slug); add(tag?.text); add(tag?.id); return keys; } function _tagDisplayName(tag) { return typeof tag === "string" ? tag : tag?.name || tag?.text || tag?.slug || ""; } function _normalizeTagRecord(tag) { if (typeof tag === "string") { const name = tag.trim(); return name ? { name, slug: name } : null; } if (tag && typeof tag === "object") { const name = String(tag.name || tag.text || tag.slug || tag.id || "").trim(); if (!name) return null; return { id: tag.id, name, slug: tag.slug || tag.name || name }; } const name = String(tag || "").trim(); return name ? { name, slug: name } : null; } function _getTopTagsFromSiteData(site) { if (site?.can_tag_topics === false) return []; return (Array.isArray(site?.top_tags) ? site.top_tags : []).map(_normalizeTagRecord).filter((tag) => !!tag); } function _cacheTagStyleAliases(tags) { tags.forEach((tag) => { const style = _getTagStyle(tag); if (style) _cacheTagStyle(_tagIndexKeys(tag), style); }); } function _getTagStyle(tag) { for (const key of _tagIndexKeys(tag)) { const style = tagStyleByKey.get(key); if (style) return style; } return null; } function _sanitizeTagStyle(styleText) { const pairs = []; String(styleText || "").split(";").forEach((part) => { const [rawName, rawValue] = part.split(":"); const name = rawName?.trim(); const value = rawValue?.trim(); if ((name === "--color1" || name === "--color2") && /^#[A-Fa-f0-9]{3,8}$/.test(value || "")) pairs.push(`${name}: ${value}`); }); return pairs.join("; "); } function _cacheTagStyle(keys, style) { keys.forEach((key) => { const normalized = String(key || "").trim().toLowerCase(); if (normalized) tagStyleByKey.set(normalized, style); }); } function _loadTagStyleCache() { if (tagStyleByKey.size > 0) return true; try { const cache = _getSiteValue(TAG_STYLE_CACHE_KEY, null); const entry = cache?.locales?.[getPageLocale()]; if (!cache || cache.version !== 1 || !Array.isArray(entry?.entries)) return false; entry.entries.forEach(([key, style]) => { if (!key || !style || typeof style !== "object") return; const icon = _safeIconName(style.icon || ""); const cssText = _sanitizeTagStyle(style.cssText || ""); if (!icon && !cssText) return; tagStyleByKey.set(String(key), { icon, cssText, hasIcon: !!icon }); }); return tagStyleByKey.size > 0; } catch (e) { console.warn("[SFP] load tag style cache failed:", e); return false; } } function _saveTagStyleCache() { if (tagStyleByKey.size === 0) return; try { const cached = _getSiteValue(TAG_STYLE_CACHE_KEY, null); _setSiteValue(TAG_STYLE_CACHE_KEY, { version: 1, locales: withLocaleEntry(cached?.version === 1 ? cached.locales : void 0, getPageLocale(), { savedAt: Date.now(), entries: Array.from(tagStyleByKey.entries()) }) }); } catch (e) { console.warn("[SFP] save tag style cache failed:", e); } } function _extractTagStylesFromDocument(doc) { Array.from(doc.querySelectorAll("a.discourse-tag[data-tag-name]")).forEach((anchor) => { const use = anchor.querySelector("svg use"); const icon = _safeIconName((use?.getAttribute("href") || use?.getAttribute("xlink:href") || "").replace(/^#/, "")); const style = { icon, cssText: _sanitizeTagStyle(anchor.getAttribute("style") || ""), hasIcon: !!icon }; if (!style.hasIcon && !style.cssText) return; const hrefParts = (anchor.getAttribute("href") || "").split("/").filter(Boolean); _cacheTagStyle([ anchor.dataset.tagName, anchor.textContent, hrefParts[1], hrefParts[2] ], style); }); } function _waitForIframeTags(iframe, loadScope, timeoutMs = 12e3) { return new Promise((resolve) => { const forget = loadScope.defer(() => resolve(null)); const finish = (value) => { forget(); resolve(value); }; const start = Date.now(); const tick = () => { let doc = null; try { doc = iframe.contentDocument; if (doc && doc.querySelector("a.discourse-tag[data-tag-name]")) { finish(doc); return; } } catch (e) { finish(null); return; } if (Date.now() - start >= timeoutMs) { finish(doc); return; } loadScope.timeout(tick, 250); }; tick(); }); } async function loadTagStyleIndex() { if (scope.disposed) return; if (tagStyleLoaded) return; if (tagStylePromise) return tagStylePromise; const loadScope = scope; tagStylePromise = (async () => { let iframe = null; try { let siteTags = []; try { siteTags = _getTopTagsFromSiteData(await loadSiteData()); } catch (e) { siteTags = _getTopTagsFromSiteData(_extractPreloadedSiteData()); } if (loadScope.disposed) return; if (_loadTagStyleCache()) { _cacheTagStyleAliases(siteTags); tagStyleLoaded = true; return; } _extractTagStylesFromDocument(document); iframe = document.createElement("iframe"); iframe.src = "/tags"; iframe.setAttribute("aria-hidden", "true"); iframe.style.cssText = "position:absolute;width:1px;height:1px;left:-10000px;top:-10000px;border:0;visibility:hidden;pointer-events:none;"; document.body.appendChild(iframe); const forgetIframe = loadScope.defer(() => iframe?.remove()); const doc = await _waitForIframeTags(iframe, loadScope); forgetIframe(); if (loadScope.disposed) return; if (doc) _extractTagStylesFromDocument(doc); _cacheTagStyleAliases(siteTags); _saveTagStyleCache(); tagStyleLoaded = true; } catch (e) { console.warn("[SFP] load tag style index failed:", e); } finally { if (iframe) iframe.remove(); if (loadScope === scope) tagStylePromise = null; } })(); return tagStylePromise; } function _resetRuntimeTagStyleData() { scope.dispose(); scope = new Lifetime(); tagStylePromise = null; tagStyleLoaded = false; tagStyleByKey.clear(); } return { get loaded() { return tagStyleLoaded; }, get loading() { return !!tagStylePromise; }, loadTagStyleIndex, _tagDisplayName, _getTagStyle, reset: _resetRuntimeTagStyleData, dispose: () => scope.dispose() }; } var feed_default = "/* ===== 切换按钮 ===== */\n.sfp-toggle-btn {\n display: inline-flex;\n align-items: center;\n justify-content: center;\n width: 28px;\n height: 28px;\n border: none;\n background: var(--secondary);\n color: var(--primary-medium);\n cursor: pointer;\n border-radius: 6px;\n padding: 0;\n margin-left: 6px;\n vertical-align: middle;\n transition:\n color 0.2s,\n background 0.2s;\n flex-shrink: 0;\n}\n.sfp-toggle-btn:hover {\n color: var(--primary);\n background: var(--primary-low);\n}\n.sfp-toggle-btn.active {\n color: var(--secondary);\n background: var(--tertiary);\n}\n.sfp-toggle-btn svg {\n width: 18px;\n height: 18px;\n fill: currentColor;\n}\n.home-logo-wrapper-outlet .title {\n display: flex;\n align-items: center;\n gap: 2px;\n}\n\n/* ===== 侧边栏 Feed 模式 ===== */\n/*\n * feed 模式下**不裁剪**横向溢出。\n *\n * Discourse 自己给 `.sidebar-container` 设了 `overflow-x: hidden`\n * (`common/base/sidebar.scss`),`overflow-y` 因此计算成 `auto`;我们过去又用\n * `!important` 把两者都钉成隐藏。但 feed 模式里没有任何东西需要这层裁剪:\n * 纵向 `scrollHeight === clientHeight`(`.sfp-feed-container` 是 `height: 100%`\n * 且 `overflow: hidden`),自己的 UI 也被 `.sfp-feed-container` 和\n * `.sfp-settings-shell` 各自裁住。唯一想越过边界的是右侧宽度把手和它的\n * 命中条——而 `overflow-x: visible` 和 `overflow-y: auto` 不能共存\n * (`visible` 会被计算成 `auto`),所以两轴都得放开。\n */\n.sidebar-wrapper:has(> .sidebar-container.sfp-feed-mode) {\n overflow: visible !important;\n}\n.sidebar-container.sfp-feed-mode {\n overflow: visible !important;\n}\n.sidebar-container.sfp-feed-mode .sfp-feed-container,\n.sidebar-container.sfp-feed-mode .sfp-feed-container * {\n box-sizing: border-box;\n}\n/* 隐藏所有非 feed 的直接子元素 */\n.sidebar-container.sfp-feed-mode > :not(.sfp-feed-container):not(.sfp-resizer) {\n display: none !important;\n}\n/* 显式隐藏常见 sidebar 组件(嵌套情况兜底) */\n.sidebar-container.sfp-feed-mode .sidebar-sections,\n.sidebar-container.sfp-feed-mode .sidebar-footer-container,\n.sidebar-container.sfp-feed-mode .sidebar-footer-wrapper,\n.sidebar-container.sfp-feed-mode .sidebar-footer,\n.sidebar-container.sfp-feed-mode .sidebar-custom-sections,\n.sidebar-container.sfp-feed-mode .sidebar-section-wrapper,\n.sidebar-container.sfp-feed-mode .sidebar-section-header,\n.sidebar-container.sfp-feed-mode .sidebar-section-link-wrapper {\n display: none !important;\n}\n.sidebar-container.sfp-feed-mode .sfp-feed-container {\n display: flex;\n flex-direction: column;\n position: relative;\n width: 100%;\n min-width: 0;\n height: 100%;\n overflow: hidden;\n max-width: 100%;\n}\n.sidebar-wrapper.sfp-width-animating,\n.sidebar-container.sfp-width-animating,\n#d-sidebar.sfp-width-animating {\n transition:\n width 220ms ease,\n max-width 220ms ease;\n}\n\n/* ===== 拖拽调整宽度 ===== */\n.sfp-resizer {\n position: absolute;\n top: 0;\n right: -2px;\n width: 5px;\n height: 100%;\n cursor: ew-resize;\n z-index: 10001;\n transition: background 0.2s;\n}\n.sfp-resizer:hover,\n.sfp-resizer.sfp-resizing {\n background: var(--tertiary);\n}\n/*\n * 5 px 的宽度把手同样太细。加一条不可见的宽条:宽 15 px(3 倍把手宽)、\n * 高 25%(侧边栏高度的 1/4),以把手为圆心,于是左右各伸出 5 px、横跨侧边栏\n * 边界。它只做命中判定,不画任何东西。能伸出边界是因为上面把侧边栏的裁剪\n * 放开了;否则越界的那半截会被裁掉,看得见摸不着。\n */\n.sfp-resizer-grip {\n position: absolute;\n top: 50%;\n left: 50%;\n width: 15px;\n height: 25%;\n transform: translate(-50%, -50%);\n cursor: ew-resize;\n}\n\n/* ===== Feed Header ===== */\n.sfp-feed-header {\n position: relative;\n flex-shrink: 0;\n padding: 8px 12px;\n border-bottom: 1px solid var(--primary-low);\n display: flex;\n flex-wrap: wrap;\n gap: 6px;\n align-items: center;\n overflow: visible;\n}\n.sfp-feed-header .sfp-header-spacer {\n flex: 1 1 auto;\n min-width: 8px;\n}\n.sfp-feed-header .sfp-refresh-btn,\n.sfp-feed-header .sfp-settings-btn {\n display: inline-flex;\n align-items: center;\n justify-content: center;\n width: 28px;\n height: 28px;\n border: none;\n background: var(--primary-very-low);\n color: var(--primary-medium);\n cursor: pointer;\n border-radius: 6px;\n padding: 0;\n flex-shrink: 0;\n transition:\n color 0.2s,\n background 0.2s;\n}\n.sfp-feed-header .sfp-refresh-btn:hover,\n.sfp-feed-header .sfp-settings-btn:hover,\n.sfp-settings-wrap.open .sfp-settings-btn {\n color: var(--tertiary);\n background: var(--primary-low);\n}\n.sfp-feed-header .sfp-refresh-btn.spinning svg {\n animation: sfp-spin 0.6s linear infinite;\n}\n.sfp-feed-header .sfp-refresh-btn.spinning {\n color: var(--tertiary);\n background: var(--primary-low);\n}\n.sfp-feed-header\n .sfp-refresh-btn.sfp-back-top-enter:not(.sfp-has-incoming-count)\n svg {\n animation: sfp-back-top-enter 0.18s ease;\n}\n.sfp-feed-header .sfp-refresh-btn .sfp-refresh-count {\n display: inline-block;\n min-width: 3ch;\n color: var(--tertiary);\n font-size: 13px;\n font-weight: 700;\n line-height: 1;\n text-align: center;\n white-space: nowrap;\n}\n@keyframes sfp-spin {\n from {\n transform: rotate(0deg);\n }\n to {\n transform: rotate(360deg);\n }\n}\n@keyframes sfp-back-top-enter {\n from {\n opacity: 0;\n transform: translateY(8px);\n }\n to {\n opacity: 1;\n transform: translateY(0);\n }\n}\n.sfp-feed-header .sfp-refresh-btn svg,\n.sfp-feed-header .sfp-settings-btn svg {\n width: 16px;\n height: 16px;\n fill: currentColor;\n}\n.sfp-settings-btn {\n position: absolute;\n top: 0;\n right: 0;\n z-index: 2;\n gap: 3px;\n flex-direction: column;\n}\n.sfp-settings-line {\n width: 14px;\n height: 2px;\n border-radius: 2px;\n background: currentColor;\n transition:\n transform 0.28s ease,\n opacity 0.2s ease;\n transform-origin: center;\n}\n.sfp-settings-wrap.open .sfp-settings-line-1 {\n transform: translateY(5px) rotate(45deg);\n}\n.sfp-settings-wrap.open .sfp-settings-line-2 {\n opacity: 0;\n transform: scaleX(0);\n}\n.sfp-settings-wrap.open .sfp-settings-line-3 {\n transform: translateY(-5px) rotate(-45deg);\n}\n.sfp-settings-wrap {\n position: relative;\n width: 28px;\n height: 28px;\n flex-shrink: 0;\n overflow: visible;\n}\n.sfp-settings-shell {\n position: absolute;\n top: 0;\n right: 0;\n width: 28px;\n height: 28px;\n background: transparent;\n border: none;\n border-radius: 6px;\n box-shadow: none;\n z-index: 10003;\n overflow: hidden;\n transition:\n width 0.36s cubic-bezier(0.25, 1, 0.5, 1),\n height 0.36s cubic-bezier(0.25, 1, 0.5, 1),\n border-radius 0.24s ease,\n background 0.2s ease;\n}\n.sfp-settings-wrap.open .sfp-settings-shell {\n width: 204px;\n height: var(--sfp-settings-shell-height, 128px);\n overflow: visible;\n background: var(--secondary);\n border: 1px solid var(--primary-low);\n border-radius: 8px;\n box-shadow: 0 8px 24px color-mix(in srgb, var(--primary) 14%, transparent);\n}\n.sfp-settings-panel {\n box-sizing: border-box;\n width: 204px;\n padding: 36px 10px 8px 10px;\n opacity: 0;\n visibility: hidden;\n transform: translateY(-8px);\n pointer-events: none;\n transition:\n opacity 0.18s ease,\n transform 0.18s ease,\n visibility 0.18s;\n}\n.sfp-settings-wrap.open .sfp-settings-panel {\n opacity: 1;\n visibility: visible;\n transform: translateY(0);\n pointer-events: auto;\n transition:\n opacity 0.26s ease 0.12s,\n transform 0.26s ease 0.12s,\n visibility 0.26s 0.12s;\n}\n.sfp-setting-row {\n display: grid;\n grid-template-columns: minmax(0, 1fr) auto;\n align-items: center;\n column-gap: 8px;\n font-size: 12px;\n color: var(--primary);\n line-height: 1.3;\n padding: 4px 0;\n}\n.sfp-setting-label {\n display: inline-flex;\n align-items: center;\n gap: 4px;\n min-width: 0;\n white-space: nowrap;\n}\n.sfp-setting-help-wrap {\n display: inline-flex;\n align-items: center;\n justify-content: center;\n width: 14px;\n height: 14px;\n flex: 0 0 14px;\n pointer-events: none;\n}\n.sfp-setting-help {\n display: inline-flex;\n align-items: center;\n justify-content: center;\n width: 14px;\n height: 14px;\n box-sizing: border-box;\n appearance: none;\n border: none;\n background: transparent;\n color: var(--primary-medium);\n cursor: help;\n padding: 0;\n pointer-events: auto;\n}\n.sfp-setting-help svg {\n width: 13px;\n height: 13px;\n display: block;\n fill: currentColor;\n pointer-events: none;\n}\n.sfp-setting-help:hover {\n color: var(--tertiary);\n outline: none;\n}\n.sfp-help-tooltip {\n position: fixed;\n z-index: 10004;\n width: 178px;\n max-width: calc(100vw - 32px);\n padding: 8px 9px;\n border: 1px solid var(--primary-low);\n border-radius: 6px;\n background: var(--secondary);\n box-shadow: 0 8px 22px color-mix(in srgb, var(--primary) 16%, transparent);\n color: var(--primary);\n font-size: 12px;\n font-weight: 400;\n line-height: 1.45;\n text-align: left;\n white-space: normal;\n opacity: 0;\n pointer-events: none;\n transform: translateY(-4px);\n transition:\n opacity 0.16s ease,\n transform 0.16s ease;\n}\n.sfp-help-tooltip.visible {\n opacity: 1;\n transform: translateY(0);\n}\n.sfp-setting-row input[type=\"checkbox\"] {\n flex-shrink: 0;\n margin: 0;\n}\n.sfp-setting-interval {\n display: none;\n grid-template-columns: minmax(0, 1fr) 58px auto;\n align-items: center;\n column-gap: 6px;\n margin-top: 6px;\n font-size: 12px;\n color: var(--primary-medium);\n}\n.sfp-setting-interval.visible {\n display: grid;\n}\n.sfp-setting-row.hidden,\n.sfp-setting-interval.hidden {\n display: none;\n}\n.sfp-setting-interval input {\n width: 58px;\n height: 26px;\n padding: 2px 6px;\n border: 1px solid var(--primary-low);\n border-radius: 4px;\n background: var(--secondary);\n color: var(--primary);\n font-size: 12px;\n}\n\n/* ===== 自定义下拉 ===== */\n.sfp-custom-select {\n position: relative;\n flex-shrink: 0;\n}\n.sfp-custom-select-btn {\n display: inline-flex;\n align-items: center;\n gap: 4px;\n padding: 4px 10px;\n font-size: 12px;\n height: 28px;\n border: none;\n background: var(--primary-very-low);\n color: var(--primary);\n border-radius: 6px;\n cursor: pointer;\n white-space: nowrap;\n user-select: none;\n transition:\n background 0.2s,\n color 0.2s;\n}\n.sfp-custom-select-btn:hover {\n background: var(--primary-low);\n}\n.sfp-custom-select-btn::after {\n content: \"\";\n width: 0;\n height: 0;\n border-left: 4px solid transparent;\n border-right: 4px solid transparent;\n border-top: 5px solid currentColor;\n}\n.sfp-custom-select-dropdown {\n position: absolute;\n top: calc(100% + 4px);\n left: 0;\n min-width: 100%;\n background: var(--secondary);\n border: 1px solid var(--primary-low);\n border-radius: 6px;\n box-shadow: 0 4px 12px color-mix(in srgb, var(--primary) 10%, transparent);\n z-index: 10002;\n display: none;\n overflow: hidden;\n}\n.sfp-custom-select.open .sfp-custom-select-dropdown {\n display: block;\n}\n.sfp-custom-select-option {\n display: block;\n width: 100%;\n padding: 6px 14px;\n font-size: 12px;\n border: none;\n background: none;\n color: var(--primary);\n cursor: pointer;\n text-align: left;\n white-space: nowrap;\n transition: background 0.15s;\n}\n.sfp-custom-select-option:hover {\n background: var(--primary-very-low);\n}\n.sfp-custom-select-option.selected {\n color: var(--tertiary);\n font-weight: 600;\n}\n\n/* ===== 分类标签栏 ===== */\n.sfp-tab-shell {\n position: relative;\n display: grid;\n grid-template-columns: minmax(0, 1fr) 36px;\n align-items: stretch;\n width: 100%;\n min-width: 0;\n max-width: 100%;\n border-bottom: 1px solid var(--primary-low);\n flex-shrink: 0;\n background: var(--d-content-background, var(--secondary));\n}\n.sfp-tab-bar {\n display: flex;\n gap: 8px;\n overflow-x: auto;\n overflow-y: hidden;\n -webkit-overflow-scrolling: touch;\n scrollbar-width: none;\n width: 100%;\n min-width: 0;\n max-width: 100%;\n padding: 8px 12px;\n margin: 0;\n flex-shrink: 0;\n background: transparent;\n}\n.sfp-tab-bar::-webkit-scrollbar {\n display: none;\n}\n.sfp-tab-item {\n display: inline-flex;\n align-items: center;\n gap: 3px;\n padding: 4px 12px;\n font-size: 13px;\n color: var(--primary-medium);\n cursor: pointer;\n white-space: nowrap;\n border-radius: 16px;\n background: var(--primary-very-low);\n transition: all 0.2s;\n border: 1px solid transparent;\n flex-shrink: 0;\n user-select: none;\n}\n.sfp-tab-item:hover {\n color: var(--primary);\n background: var(--primary-low);\n}\n.sfp-tab-item.active {\n color: var(--secondary);\n background: var(--tertiary);\n border-color: var(--tertiary);\n}\n.sfp-tab-item svg {\n width: 12px;\n height: 12px;\n fill: currentColor;\n flex-shrink: 0;\n}\n.sfp-category-color-marker {\n width: 0.72em;\n height: 0.72em;\n border-radius: 50%;\n background: var(--sfp-category-marker-color, currentColor);\n box-shadow: inset 0 0 0 1px\n color-mix(in srgb, var(--primary) 12%, transparent);\n flex: 0 0 auto;\n}\n.sfp-tab-more-btn {\n display: inline-flex;\n align-items: center;\n justify-content: center;\n width: 36px;\n min-width: 36px;\n padding: 0;\n border: none;\n border-left: 1px solid var(--primary-low);\n background: var(--d-content-background, var(--secondary));\n color: var(--primary-medium);\n cursor: pointer;\n transition:\n background 0.2s,\n color 0.2s;\n}\n.sfp-tab-more-btn:hover,\n.sfp-tab-shell.open .sfp-tab-more-btn {\n background: var(--primary-very-low);\n color: var(--primary);\n}\n.sfp-tab-more-btn svg {\n width: 16px;\n height: 16px;\n fill: currentColor;\n}\n.sfp-tab-panel {\n position: absolute;\n top: 100%;\n right: 8px;\n left: 8px;\n display: none;\n padding: 10px;\n max-height: min(58vh, 420px);\n overflow-y: auto;\n background: var(--secondary);\n border: 1px solid var(--primary-low);\n border-radius: 8px;\n box-shadow: 0 10px 28px color-mix(in srgb, var(--primary) 16%, transparent);\n z-index: 10002;\n}\n.sfp-tab-shell.open .sfp-tab-panel {\n display: block;\n}\n.sfp-tab-panel-header {\n display: flex;\n align-items: center;\n justify-content: space-between;\n gap: 8px;\n margin-bottom: 8px;\n font-size: 12px;\n color: var(--primary-medium);\n line-height: 1.3;\n}\n.sfp-tab-panel-title {\n display: inline-flex;\n align-items: center;\n gap: 6px;\n min-width: 0;\n}\n.sfp-tab-panel-title svg {\n width: 13px;\n height: 13px;\n fill: currentColor;\n flex-shrink: 0;\n}\n.sfp-tab-panel-close {\n display: inline-flex;\n align-items: center;\n justify-content: center;\n width: 24px;\n height: 24px;\n padding: 0;\n border: none;\n border-radius: 4px;\n background: transparent;\n color: var(--primary-medium);\n cursor: pointer;\n}\n.sfp-tab-panel-close:hover {\n background: var(--primary-very-low);\n color: var(--primary);\n}\n.sfp-tab-grid {\n display: grid;\n grid-template-columns: repeat(auto-fill, minmax(92px, 1fr));\n gap: 6px;\n}\n.sfp-tab-grid-item {\n display: inline-flex;\n align-items: center;\n justify-content: center;\n gap: 5px;\n min-width: 0;\n min-height: 32px;\n padding: 6px 8px;\n border: 1px solid transparent;\n border-radius: 6px;\n background: var(--primary-very-low);\n color: var(--primary-medium);\n cursor: pointer;\n font-size: 12px;\n line-height: 1.2;\n text-align: center;\n user-select: none;\n transition:\n background 0.15s,\n border-color 0.15s,\n color 0.15s,\n opacity 0.15s;\n}\n.sfp-tab-grid-item svg {\n width: 12px;\n height: 12px;\n fill: currentColor;\n flex: 0 0 auto;\n}\n.sfp-tab-grid-item .sfp-category-color-marker {\n width: 0.75em;\n height: 0.75em;\n}\n.sfp-tab-grid-item span {\n min-width: 0;\n overflow: hidden;\n text-overflow: ellipsis;\n white-space: nowrap;\n}\n.sfp-tab-grid-item:hover {\n background: var(--primary-low);\n color: var(--primary);\n}\n.sfp-tab-grid-item.active {\n background: var(--tertiary);\n border-color: var(--tertiary);\n color: var(--secondary);\n}\n.sfp-tab-grid-item.dragging {\n opacity: 0.45;\n}\n.sfp-tab-grid-item.drop-target {\n border-color: var(--tertiary);\n box-shadow: inset 0 0 0 1px var(--tertiary);\n}\n\n/* ===== 筛选栏 ===== */\n.sfp-filter-bar {\n position: relative;\n z-index: 3;\n display: flex;\n align-items: center;\n gap: 12px;\n width: 100%;\n min-width: 0;\n max-width: 100%;\n padding: 8px 16px;\n margin: 0;\n background: var(--primary-very-low);\n border-bottom: 1px solid var(--primary-low);\n font-size: 12px;\n color: var(--primary-medium);\n flex-shrink: 0;\n}\n.sfp-filter-item {\n cursor: pointer;\n padding: 2px 6px;\n border-radius: 4px;\n transition: all 0.2s;\n user-select: none;\n}\n.sfp-filter-item:hover {\n color: var(--tertiary);\n background: var(--primary-low);\n}\n.sfp-filter-item.active {\n color: var(--secondary);\n background: var(--tertiary);\n}\n\n/* ===== Feed 滚动区 ===== */\n.sfp-feed-scroll {\n position: relative;\n flex: 1;\n min-width: 0;\n max-width: 100%;\n overflow-y: auto;\n overflow-x: hidden;\n -webkit-overflow-scrolling: touch;\n overscroll-behavior-y: contain;\n touch-action: pan-y pinch-zoom;\n --scrollbarBg: transparent;\n --scrollbarThumbBg: var(--d-selected, var(--token-color-surface-hovered));\n --scrollbarWidth: var(--space-2, 0.5em);\n scrollbar-gutter: stable;\n scrollbar-color: var(--scrollbarThumbBg) var(--scrollbarBg);\n}\n.sfp-feed-scroll::-webkit-scrollbar {\n width: var(--scrollbarWidth);\n}\n.sfp-feed-scroll::-webkit-scrollbar-thumb {\n background-color: var(--scrollbarThumbBg);\n border-radius: calc(var(--scrollbarWidth) / 2);\n}\n.sfp-feed-scroll::-webkit-scrollbar-track {\n background-color: transparent;\n}\n.sfp-content-wrapper {\n position: relative;\n min-width: 0;\n max-width: 100%;\n min-height: 100%;\n}\n\n/* ===== 帖子列表项 ===== */\n.sfp-topic-item {\n display: block;\n padding: 12px 20px;\n border-bottom: 1px solid var(--primary-very-low);\n color: inherit;\n cursor: pointer;\n text-decoration: none;\n transition: background 0.2s;\n position: relative;\n min-width: 0;\n max-width: 100%;\n overflow-wrap: break-word;\n word-break: break-word;\n}\n/* 卡片是 `
`,内部标题与发布者才是真链接;这里抹平原生链接样式。 */\n.sfp-topic-item .sfp-topic-title-line,\n.sfp-topic-item .sfp-topic-user-link {\n color: inherit;\n text-decoration: none;\n}\n.sfp-topic-item .sfp-topic-title-line:focus-visible {\n outline: 2px solid var(--tertiary);\n outline-offset: 2px;\n border-radius: 4px;\n}\n.sfp-topic-item:hover {\n background: var(--primary-very-low);\n}\n.sfp-topic-item.sfp-filter-mismatch {\n opacity: 0.48;\n filter: grayscale(0.85);\n}\n.sfp-topic-item.sfp-topic-unavailable {\n opacity: 0.62;\n}\n.sfp-topic-item.sfp-topic-unavailable .sfp-topic-title-line {\n text-decoration: line-through;\n}\n.sfp-topic-item.sfp-new-highlight {\n --sfp-new-highlight-color: var(--tertiary-med-or-tertiary, var(--tertiary));\n animation: sfp-new-pulse 10s ease-out forwards;\n position: relative;\n}\n@keyframes sfp-new-pulse {\n 0% {\n box-shadow: inset 0 0 0 2px var(--sfp-new-highlight-color);\n background: color-mix(\n in srgb,\n var(--sfp-new-highlight-color) 15%,\n transparent\n );\n }\n 100% {\n box-shadow: inset 0 0 0 0px transparent;\n background: transparent;\n }\n}\n\n/* 未读圆点 — 紧跟在时间后 */\n.sfp-topic-item .sfp-topic-time {\n font-size: 12px;\n color: var(--primary-medium);\n white-space: nowrap;\n margin-left: auto;\n flex-shrink: 0;\n display: inline-flex;\n align-items: center;\n gap: 4px;\n line-height: 1;\n}\n.sfp-topic-item .sfp-unread-dot {\n width: 8px;\n height: 8px;\n flex: 0 0 8px;\n display: inline-block;\n border-radius: 50%;\n color: var(--tertiary-med-or-tertiary, var(--tertiary));\n background: currentColor;\n opacity: 0.75;\n vertical-align: middle;\n}\n/* 跟踪/关注的话题:同一个位置换成新回复数。行高与时间文字一致,不撑高头部。 */\n.sfp-topic-item .sfp-unread-count {\n flex: 0 0 auto;\n min-width: 14px;\n padding: 0 3px;\n border-radius: 7px;\n background: var(--tertiary-med-or-tertiary, var(--tertiary));\n color: var(--secondary);\n font-size: 10px;\n font-weight: 700;\n line-height: 12px;\n text-align: center;\n font-variant-numeric: tabular-nums;\n}\n.sfp-topic-item .sfp-unread-hidden {\n visibility: hidden;\n}\n\n/* 头像 + 用户信息行 */\n.sfp-topic-item .sfp-topic-header {\n display: flex;\n align-items: center;\n gap: 8px;\n margin-bottom: 5px;\n}\n.sfp-topic-item .sfp-topic-user-link {\n display: inline-flex;\n align-items: center;\n gap: 5px;\n min-width: 0;\n color: inherit;\n text-decoration: none;\n cursor: pointer;\n}\n/* 防止父级 topic 链接的 hover/focus 样式把用户入口改成普通文本。 */\n.sfp-topic-item .sfp-topic-user-link:hover,\n.sfp-topic-item .sfp-topic-user-link:focus {\n color: inherit;\n text-decoration: none;\n}\n.sfp-topic-item .sfp-topic-user-link:focus-visible {\n outline: 2px solid var(--tertiary);\n outline-offset: 2px;\n border-radius: 4px;\n}\n.sfp-topic-item .sfp-topic-avatar {\n width: 28px;\n height: 28px;\n border-radius: 50%;\n flex-shrink: 0;\n object-fit: cover;\n}\n.sfp-topic-item .sfp-topic-meta-col {\n display: flex;\n flex-direction: column;\n min-width: 0;\n flex: 1;\n}\n.sfp-topic-item .sfp-topic-user-info {\n display: flex;\n align-items: center;\n gap: 5px;\n flex-wrap: wrap;\n overflow: hidden;\n}\n.sfp-topic-item .sfp-topic-username {\n font-size: 13px;\n color: var(--primary);\n font-weight: 500;\n cursor: pointer;\n transition: color 0.2s;\n white-space: nowrap;\n overflow: hidden;\n text-overflow: ellipsis;\n}\n.sfp-topic-item .sfp-topic-username:hover {\n color: var(--tertiary);\n}\n.sfp-topic-item .sfp-topic-name {\n font-size: 12px;\n color: var(--primary-medium);\n white-space: nowrap;\n overflow: hidden;\n text-overflow: ellipsis;\n}\n/* 标题 */\n.sfp-topic-item .sfp-topic-title {\n font-size: 14px;\n font-weight: bold;\n color: var(--primary);\n line-height: 1.4;\n margin: 0;\n word-break: break-word;\n overflow: hidden;\n text-overflow: ellipsis;\n display: -webkit-box;\n -webkit-line-clamp: 2;\n -webkit-box-orient: vertical;\n transition: color 0.2s;\n}\n.sfp-topic-item .sfp-topic-title:hover {\n color: var(--tertiary);\n}\n.sfp-topic-item.sfp-read .sfp-topic-title {\n color: var(--title-color--read, var(--primary-medium));\n}\n.sfp-topic-item .sfp-topic-title-line {\n display: inline;\n}\n.sfp-topic-item .sfp-topic-status-badges {\n display: inline-flex;\n align-items: center;\n gap: 4px;\n margin-left: 2px;\n flex-shrink: 0;\n}\n.sfp-topic-item .topic-status-card {\n --badge-accent: var(--primary-medium);\n --badge-bg: var(--primary-very-low);\n --badge-border: var(--primary-low);\n display: inline-flex;\n align-items: center;\n gap: 3px;\n padding: 1px 6px;\n border: 1px solid var(--badge-border);\n border-radius: var(--d-border-radius, 8px);\n background: var(--badge-bg);\n color: var(--badge-accent);\n font-size: 11px;\n font-weight: 700;\n line-height: 1.45;\n margin-right: 5px;\n vertical-align: middle;\n white-space: nowrap;\n box-shadow: inset 0 0 0 1px\n color-mix(in srgb, var(--badge-accent) 8%, transparent);\n}\n.sfp-topic-item .topic-status-card.--hot {\n --badge-accent: var(--danger);\n --badge-bg: var(--danger-low, var(--d-hover, var(--tertiary-low)));\n --badge-border: color-mix(in srgb, var(--danger) 28%, transparent);\n}\n.sfp-topic-item .topic-status-card.--pinned {\n --badge-accent: var(--primary-medium);\n --badge-bg: var(--primary-very-low);\n --badge-border: var(--primary-low);\n}\n.sfp-topic-item .topic-status-card.--unavailable {\n --badge-accent: var(--danger);\n --badge-bg: var(--danger-low, var(--d-hover, var(--tertiary-low)));\n --badge-border: color-mix(in srgb, var(--danger) 28%, transparent);\n}\n.sfp-topic-item .topic-status-card__name {\n color: var(--badge-accent);\n font-size: inherit;\n font-weight: inherit;\n line-height: inherit;\n margin: 0;\n}\n.sfp-topic-item .topic-status-card .d-icon {\n color: var(--badge-accent);\n width: 0.92em;\n height: 0.92em;\n flex-shrink: 0;\n}\n.sfp-topic-item .topic-statuses {\n float: left;\n}\n.sfp-topic-item .topic-statuses .topic-status {\n display: inline-flex;\n align-items: center;\n color: var(--primary-medium);\n margin: 0 0.18em 0 0;\n --icon-size: 0.86em;\n}\n.sfp-topic-item .topic-statuses .topic-status .d-icon {\n width: var(--icon-size);\n height: var(--icon-size);\n color: currentColor;\n}\n\n/* 分类 + 标签行 */\n.sfp-topic-item .sfp-topic-category-tags {\n display: flex;\n flex-wrap: wrap;\n align-items: center;\n gap: 4px;\n margin-top: 5px;\n}\n.sfp-topic-item .sfp-category-badge {\n --badge-category-bg: light-dark(\n oklch(from var(--category-badge-color) 97% calc(c * 0.3) h),\n oklch(from var(--category-badge-color) 45% calc(c * 0.5) h)\n );\n --badge-category-text: light-dark(\n oklch(from var(--category-badge-color) 35% calc(c * 0.6) h),\n oklch(from var(--category-badge-color) 95% calc(c * 0.2) h)\n );\n display: inline-flex;\n align-items: center;\n gap: 0.33em;\n font-size: 11px;\n padding: 2px 6px;\n border-radius: var(--d-border-radius, 4px);\n background-color: var(--badge-category-bg, var(--primary-very-low));\n color: var(--badge-category-text, var(--primary-medium));\n flex-shrink: 0;\n max-width: 120px;\n overflow: hidden;\n text-overflow: ellipsis;\n white-space: nowrap;\n}\n.sfp-topic-item .sfp-category-badge .badge-category {\n min-width: 0;\n align-items: center;\n}\n.sfp-topic-item .sfp-category-badge .badge-category__name {\n min-width: 0;\n overflow: hidden;\n text-overflow: ellipsis;\n color: var(--badge-category-text, var(--primary-medium));\n}\n.sfp-topic-item .sfp-category-badge .d-icon {\n width: 0.9em;\n height: 0.9em;\n flex-shrink: 0;\n}\n@supports not (color: light-dark(tan, tan)) {\n .sfp-topic-item .sfp-category-badge {\n --badge-category-bg: color-mix(\n in srgb,\n var(--category-badge-color) 16%,\n transparent\n );\n --badge-category-text: var(--primary-high);\n }\n}\n\n/* 标签 */\n.sfp-topic-item .sfp-topic-tags {\n display: flex;\n gap: 3px;\n flex-wrap: wrap;\n}\n.sfp-topic-item .sfp-tag {\n font-size: 11px;\n padding: 2px 6px;\n border-radius: var(--d-border-radius, 4px);\n line-height: 1.4;\n max-width: 96px;\n overflow: hidden;\n text-overflow: ellipsis;\n white-space: nowrap;\n flex-shrink: 1;\n}\n.sfp-topic-item .sfp-tag .tag-icon .d-icon {\n width: 0.9em;\n height: 0.9em;\n}\n\n/* 统计行 */\n.sfp-topic-item .sfp-topic-stats {\n display: flex;\n gap: 12px;\n margin-top: 8px;\n font-size: 12px;\n color: var(--primary-medium);\n}\n.sfp-topic-item .sfp-topic-stat {\n display: flex;\n align-items: center;\n gap: 4px;\n}\n.sfp-topic-item .sfp-topic-stat .d-icon {\n width: 1em;\n height: 1em;\n}\n\n/* ===== 加载状态 ===== */\n.sfp-loading {\n display: flex;\n flex-direction: column;\n align-items: center;\n justify-content: center;\n padding: 40px 20px;\n color: var(--primary-medium);\n font-size: 13px;\n gap: 12px;\n}\n.sfp-spinner {\n width: 28px;\n height: 28px;\n border: 3px solid var(--primary-low);\n border-top-color: var(--tertiary);\n border-radius: 50%;\n animation: sfp-spin 0.8s linear infinite;\n}\n.sfp-empty {\n text-align: center;\n padding: 40px 10px;\n color: var(--primary-medium);\n font-size: 13px;\n}\n.sfp-load-more {\n padding: 14px 10px;\n text-align: center;\n font-size: 12px;\n color: var(--primary-medium);\n cursor: pointer;\n transition: color 0.2s;\n}\n.sfp-load-more-error {\n display: flex;\n justify-content: center;\n align-items: center;\n gap: 8px;\n cursor: default;\n}\n.sfp-load-more-error .sfp-load-more-retry {\n padding: 4px 10px;\n border: none;\n border-radius: 4px;\n background: var(--tertiary);\n color: var(--secondary);\n cursor: pointer;\n font-size: 12px;\n}\n.sfp-load-more:hover {\n color: var(--tertiary);\n}\n.sfp-load-more .sfp-load-more-spinner {\n display: inline-block;\n width: 14px;\n height: 14px;\n border: 2px solid var(--primary-low);\n border-top-color: var(--tertiary);\n border-radius: 50%;\n animation: sfp-spin 0.8s linear infinite;\n vertical-align: middle;\n margin-right: 6px;\n}\n.sfp-no-more {\n padding: 14px 10px;\n text-align: center;\n font-size: 11px;\n color: var(--primary-low-mid);\n}\n.sfp-load-more-note {\n padding: 10px 10px 0;\n text-align: center;\n font-size: 12px;\n color: var(--primary-medium);\n}\n.sfp-error {\n padding: 40px 20px;\n text-align: center;\n color: var(--danger);\n display: flex;\n flex-direction: column;\n align-items: center;\n gap: 10px;\n}\n.sfp-error-icon {\n font-size: 32px;\n}\n.sfp-error-msg {\n font-size: 14px;\n font-weight: 600;\n}\n.sfp-error-detail {\n font-size: 12px;\n color: var(--primary-medium);\n word-break: break-word;\n}\n.sfp-error .sfp-retry-btn {\n margin-top: 6px;\n padding: 6px 16px;\n background: var(--tertiary);\n color: var(--secondary);\n border: none;\n border-radius: 4px;\n cursor: pointer;\n font-size: 12px;\n transition: opacity 0.2s;\n}\n.sfp-error .sfp-retry-btn:hover {\n opacity: 0.85;\n}\n\n/* ===== 稍后再读 ===== */\n/* 分隔线是区域的兄弟节点:它不属于任何一侧,线上方就是主列表。\n 元素自身就是那条线,不留上下留白,也不带背景色,避免遮挡两侧内容。 */\n.sfp-read-later {\n position: relative;\n flex: 0 0 auto;\n display: flex;\n flex-direction: column;\n min-height: 0;\n overflow: hidden;\n background: var(--secondary);\n}\n.sfp-read-later[hidden] {\n display: none;\n}\n.sfp-rl-divider {\n position: relative;\n /* 区域是 `position: relative` 且有背景,DOM 上又在分隔线之后,默认会盖住\n 圆伸进区域的那一半(点击被背景吃掉)。抬到区域之上,圆才两侧都能命中。 */\n z-index: 2;\n flex: 0 0 auto;\n /* 自动外边距把「分隔线 + 区域」一起压到容器底部;主滚动区撑满时为 0。 */\n margin-top: auto;\n height: 3px;\n padding: 0;\n border: none;\n background: var(--primary-low);\n cursor: ns-resize;\n transition: background 0.2s;\n}\n/*\n * 3 px 的线本身太细。命中面积靠两个不可见的小圆撑开:一个在中心、一个在右端。\n * 直径是线高的 10 倍(30 px),所以上下各多出 13.5 px;圆形而非整条,是为了\n * 尽量少挡住两侧条目。圆都**不越过右边界**:侧边栏有 `overflow-x: hidden`,\n * 越界的部分会被裁掉、也就点不到。\n */\n.sfp-rl-grip {\n position: absolute;\n top: 50%;\n width: 30px;\n height: 30px;\n margin: -15px 0 0 -15px;\n border-radius: 50%;\n cursor: ns-resize;\n}\n.sfp-rl-grip-center {\n left: 50%;\n}\n.sfp-rl-grip-end {\n /* 右边与侧边栏的宽度分割线相切:圆心不再压在线尾,而是整个圆贴在里面。 */\n left: auto;\n right: 0;\n margin-left: 0;\n}\n.sfp-rl-divider[hidden] {\n display: none;\n}\n.sfp-rl-divider:hover,\n.sfp-rl-divider:focus-visible,\n.sfp-rl-divider.sfp-rl-dragging {\n background: var(--tertiary);\n}\n.sfp-rl-divider[aria-disabled=\"true\"] {\n cursor: default;\n}\n.sfp-rl-divider[aria-disabled=\"true\"] .sfp-rl-grip {\n cursor: default;\n}\n.sfp-rl-scroll {\n flex: 1 1 auto;\n min-height: 0;\n /*\n * `column-reverse` 是这个区域唯一的贴底手段,也是它「从下往上算」的来源:\n * 主轴起点在底部,整块列表(唯一子元素)钉在底边,滚动原点也在底部——\n * 改高度只是让上方多露出内容,内容本身一步都不动。用 `column` +\n * `margin-top: auto` 会在「放得下」与「放不下」之间换一次对齐方式,\n * 拖分隔线时内容就跳。\n *\n * 注意它只负责「整块贴底」,不反转条目:`.sfp-rl-list` 自身仍是正常\n * `column`,所以 DOM 顺序(新 → 旧)就是从上到下的视觉顺序。\n */\n display: flex;\n flex-direction: column-reverse;\n overflow-y: auto;\n overflow-x: hidden;\n -webkit-overflow-scrolling: touch;\n overscroll-behavior-y: contain;\n touch-action: pan-y pinch-zoom;\n /* 与主滚动区同一套滚动条,两侧看起来才是同一个列表。 */\n --scrollbarBg: transparent;\n --scrollbarThumbBg: var(--d-selected, var(--token-color-surface-hovered));\n --scrollbarWidth: var(--space-2, 0.5em);\n scrollbar-gutter: stable;\n scrollbar-color: var(--scrollbarThumbBg) var(--scrollbarBg);\n}\n.sfp-rl-scroll::-webkit-scrollbar {\n width: var(--scrollbarWidth);\n}\n.sfp-rl-scroll::-webkit-scrollbar-thumb {\n background-color: var(--scrollbarThumbBg);\n border-radius: calc(var(--scrollbarWidth) / 2);\n}\n.sfp-rl-scroll::-webkit-scrollbar-track {\n background-color: transparent;\n}\n/* 列表自身是正常 `column`:DOM 顺序(新 → 旧)就是从上到下的视觉顺序。 */\n.sfp-rl-list {\n display: flex;\n flex-direction: column;\n flex: 0 0 auto;\n}\n.sfp-rl-item {\n position: relative;\n}\n/* 拖动中的条目:表示这是「搬动」,不是新链接。 */\n.sfp-rl-item.sfp-rl-dragging {\n opacity: 0.45;\n}\n/* 排序插入位置:只画边线,不动布局,拖动时列表不会抖。 */\n.sfp-rl-item.sfp-rl-insert-before {\n box-shadow: inset 0 2px 0 var(--tertiary);\n}\n.sfp-rl-item.sfp-rl-insert-after {\n box-shadow: inset 0 -2px 0 var(--tertiary);\n}\n.sfp-rl-card {\n display: flex;\n gap: 8px;\n padding: 8px 28px 8px 10px;\n color: inherit;\n text-decoration: none;\n}\n.sfp-rl-card:hover {\n background: var(--primary-very-low);\n}\n.sfp-rl-card-image {\n flex: 0 0 auto;\n width: 40px;\n height: 40px;\n border-radius: 6px;\n object-fit: cover;\n background: var(--primary-low);\n}\n.sfp-rl-card-body {\n min-width: 0;\n display: flex;\n flex-direction: column;\n gap: 2px;\n}\n.sfp-rl-card-title-row {\n display: flex;\n align-items: baseline;\n gap: 5px;\n min-width: 0;\n}\n/* 用户卡片的全名:与标题同一行、排在前面,样式同话题条目的姓名。 */\n.sfp-rl-card-name {\n flex: 0 0 auto;\n max-width: 55%;\n font-size: 12px;\n line-height: 1.3;\n color: var(--primary-medium);\n white-space: nowrap;\n overflow: hidden;\n text-overflow: ellipsis;\n}\n.sfp-rl-card-title {\n min-width: 0;\n font-size: 13px;\n font-weight: 500;\n line-height: 1.3;\n color: var(--primary);\n display: -webkit-box;\n -webkit-line-clamp: 2;\n -webkit-box-orient: vertical;\n overflow: hidden;\n word-break: break-word;\n}\n.sfp-rl-card-excerpt {\n font-size: 12px;\n line-height: 1.35;\n color: var(--primary-medium);\n display: -webkit-box;\n -webkit-line-clamp: 2;\n -webkit-box-orient: vertical;\n overflow: hidden;\n word-break: break-word;\n}\n.sfp-rl-fallback {\n display: flex;\n gap: 8px;\n align-items: flex-start;\n padding: 8px 28px 8px 10px;\n color: inherit;\n text-decoration: none;\n}\n.sfp-rl-fallback:hover {\n background: var(--primary-very-low);\n}\n.sfp-rl-fallback-icon {\n flex: 0 0 auto;\n width: 40px;\n height: 40px;\n display: flex;\n align-items: center;\n justify-content: center;\n border-radius: 6px;\n background: var(--primary-very-low);\n color: var(--primary-medium);\n}\n.sfp-rl-fallback-body {\n min-width: 0;\n display: flex;\n flex-direction: column;\n gap: 2px;\n}\n.sfp-rl-fallback-title {\n font-size: 13px;\n font-weight: 500;\n line-height: 1.3;\n color: var(--primary);\n display: -webkit-box;\n -webkit-line-clamp: 2;\n -webkit-box-orient: vertical;\n overflow: hidden;\n word-break: break-word;\n}\n.sfp-rl-fallback-note {\n font-size: 11px;\n color: var(--primary-low-mid);\n}\n/* 拖拽指示框:覆盖整个区域、压在条目之上,但不吃指针事件。 */\n.sfp-rl-drop {\n position: absolute;\n inset: 2px;\n z-index: 3;\n display: flex;\n align-items: center;\n justify-content: center;\n border: 2px dashed var(--primary-low-mid);\n border-radius: 6px;\n pointer-events: none;\n font-size: 12px;\n color: var(--primary-medium);\n text-align: center;\n transition: background 0.15s, border-color 0.15s, color 0.15s;\n}\n.sfp-rl-drop[hidden] {\n display: none;\n}\n.sfp-rl-drop > span {\n padding: 2px 8px;\n border-radius: 4px;\n background: var(--secondary);\n opacity: 0.95;\n}\n/* 指针进入区域:遮罩加深,文案从「拖进」变成「松开」。 */\n.sfp-rl-drop.sfp-rl-drop-active {\n border-color: var(--tertiary);\n color: var(--tertiary);\n background: color-mix(in srgb, var(--tertiary) 12%, transparent);\n}\n"; var instance = null; var menuRegistered = false; function startFeedPanel() { if (instance) return instance; const storage = createSiteStorage(location.origin, gm); storage.migrateLegacy(); const discourse = createDiscourseBridge(gm.pageWindow()); const prefs = new Preferences(storage); let controller; const site = createSiteData({ storage, getCsrfToken: discourse.getCsrfToken, getPageLocale: discourse.getPageLocale, onChange: () => { controller?.normalizeSiteState(); } }); const tags = createTagStyles({ storage, site, getPageLocale: discourse.getPageLocale }); controller = createFeedController({ storage, discourse, prefs, site, tags }); discourse.observePageLocale(() => controller?.reloadForPageLocale()); const style = gm.addStyle(feed_default); const application = { clearCaches: controller.clearCaches, dispose() { if (instance !== application) return; instance = null; controller?.dispose(); tags.dispose(); site.dispose(); discourse.dispose(); style?.remove(); } }; instance = application; if (!menuRegistered) { gm.registerMenu("SFP: 清空分类和标签缓存", () => instance?.clearCaches({ reload: true })); menuRegistered = true; } try { const page = gm.pageWindow(); page.SFPFeedPanel = Object.assign(page.SFPFeedPanel || {}, { clearCaches: () => instance?.clearCaches({ reload: true }), dispose: () => instance?.dispose(), start: () => { startFeedPanel(); } }); } catch (error) { console.warn("[SFP] setup cache controls failed:", error); } controller.start(); return application; } if (window.top === window.self) startFeedPanel(); })();