From bb8b914072ce641b84b98b3d3716264deffb763d Mon Sep 17 00:00:00 2001 From: the1812 Date: Thu, 17 Feb 2022 12:56:16 +0800 Subject: [PATCH] Improve cid hook compatibility (fix #3005) --- src/core/observer.ts | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/core/observer.ts b/src/core/observer.ts index 2f50dc821..1e1430741 100644 --- a/src/core/observer.ts +++ b/src/core/observer.ts @@ -272,17 +272,17 @@ export const videoChange = async ( window.dispatchEvent(event) } if (!cidHooked) { - let hookedCid = cid - Object.defineProperty(unsafeWindow, 'cid', { - get() { - return hookedCid - }, - set(newId) { - hookedCid = newId - if (!Array.isArray(newId)) { - fireEvent() - } - }, + let lastCid = cid + allMutations(() => { + const { cid: newCid } = getId() + // b 站代码的神秘行为, 在更换 cid 时会临时改成一个数组, 做监听要忽略这种值 + if (Array.isArray(newCid)) { + return + } + if (lastCid !== newCid) { + fireEvent() + lastCid = newCid + } }) cidHooked = true }