From 9ed2c23230bb6dbc03b8a405ea4ff01eaf3b4f3f Mon Sep 17 00:00:00 2001 From: wxs Date: Fri, 17 Jul 2026 16:54:19 +0800 Subject: [PATCH] fix: refresh favorite identity from market bridge --- src/content/market/index.ts | 31 +++++++++++++++++++++++++++++- tests/market-content-entry.test.ts | 14 +++----------- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/src/content/market/index.ts b/src/content/market/index.ts index 4795722..35c210a 100644 --- a/src/content/market/index.ts +++ b/src/content/market/index.ts @@ -138,6 +138,7 @@ export interface CreateMarketControllerOptions { const AUDIENCE_PROFILE_FIELD_SELECTION_STORAGE_KEY = "sces:audience-profile:selectedHeaders"; +const SERIALIZED_MARKET_ROWS_ATTRIBUTE = "data-sces-market-rows"; export function createMarketController(options: CreateMarketControllerOptions) { const marketApiClient = createMarketApiClient(); @@ -226,6 +227,7 @@ export function createMarketController(options: CreateMarketControllerOptions) { let activeSort: MarketSortState | undefined; let isDisposed = false; let lastKnownPageSignature = ""; + let lastKnownFavoriteIdentitySignature = readFavoriteIdentitySignature(options.document); let needsResync = false; let scheduledSyncTimeoutId: number | null = null; let activeSyncPromise: Promise | null = null; @@ -268,6 +270,9 @@ export function createMarketController(options: CreateMarketControllerOptions) { } catch { return; } + const nextFavoriteIdentitySignature = readFavoriteIdentitySignature(options.document); + const favoriteIdentityChanged = + nextFavoriteIdentitySignature !== lastKnownFavoriteIdentitySignature; const toolbarNeedsRemount = !toolbar || !isPluginToolbarMounted(toolbar.root, options.document); @@ -291,7 +296,8 @@ export function createMarketController(options: CreateMarketControllerOptions) { !needsToolbarRemount && !selectionControlsMissing && !favoriteControlsMissing && - !needsUnavailableFavoritesRecovery + !needsUnavailableFavoritesRecovery && + !favoriteIdentityChanged ) { return; } @@ -303,6 +309,10 @@ export function createMarketController(options: CreateMarketControllerOptions) { if (favoriteControlsMissing || needsUnavailableFavoritesRecovery) { needsFavoritesRefresh = true; } + if (favoriteIdentityChanged) { + lastKnownFavoriteIdentitySignature = nextFavoriteIdentitySignature; + needsFavoritesRefresh = true; + } scheduleSync(); }); const observationRoot = options.document.body ?? options.document.documentElement; @@ -1870,10 +1880,25 @@ export function createMarketController(options: CreateMarketControllerOptions) { return; } + const documentElement = options.document.documentElement; + if (observationRoot === documentElement) { + observer.observe(observationRoot, { + attributeFilter: [SERIALIZED_MARKET_ROWS_ATTRIBUTE], + attributes: true, + childList: true, + subtree: true + }); + return; + } + observer.observe(observationRoot, { childList: true, subtree: true }); + observer.observe(documentElement, { + attributeFilter: [SERIALIZED_MARKET_ROWS_ATTRIBUTE], + attributes: true + }); } function runSyncCycle(): Promise { @@ -1929,6 +1954,10 @@ export function createMarketController(options: CreateMarketControllerOptions) { } +function readFavoriteIdentitySignature(document: Document): string { + return document.documentElement.getAttribute(SERIALIZED_MARKET_ROWS_ATTRIBUTE) ?? ""; +} + function readNativeToolbarActionCount(document: Document): number { return Array.from(document.querySelectorAll("button, a, [role='button']")).filter( (element) => { diff --git a/tests/market-content-entry.test.ts b/tests/market-content-entry.test.ts index 1cd4eff..6e02444 100644 --- a/tests/market-content-entry.test.ts +++ b/tests/market-content-entry.test.ts @@ -4658,8 +4658,8 @@ describe("market-content-entry", () => { }); test("preserves a late serialized core user id through favorite batch import", async () => { - document.body.innerHTML = buildRealMarketFixtureWithoutAuthorIds([ - { authorName: "达人 A", price21To60s: "¥11,000" } + document.body.innerHTML = buildRealMarketFixture([ + { authorId: "111", authorName: "达人 A", price21To60s: "¥11,000" } ]); const repository = createTestFavoritesRepository(); const observer = createMutationObserverFactory(); @@ -4687,16 +4687,8 @@ describe("market-content-entry", () => { { authorId: "111", authorName: "达人 A", coreUserId: "core-111" } ]) ); - document.documentElement.setAttribute("data-test-page-index", "2"); observer.trigger(); - await waitForCondition( - () => { - const favoriteButton = document.querySelector( - '[data-sces-favorite-row-action="button"]' - ); - return favoriteButton instanceof HTMLButtonElement && !favoriteButton.disabled; - } - ); + await flushWithTimers(); click('[data-sces-favorite-row-action="button"]'); click('[data-sces-favorite-row-picker="create-folder"]');