From 7dc310de430bffe830016140e8e26d5e5e3accf1 Mon Sep 17 00:00:00 2001 From: xuan <27200289+wuaishare@users.noreply.github.com> Date: Fri, 19 Dec 2025 06:13:34 +0800 Subject: [PATCH] Update kuaishou.ts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 快手路由小修:将 window.__APOLLO_STATE__= 提取为常量 APOLLO_STATE_PREFIX,并去掉 allItems?.forEach 的可选链(allItems 已有默认空数组)。 --- src/routes/kuaishou.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/routes/kuaishou.ts b/src/routes/kuaishou.ts index d6c2a71..04bdf90 100755 --- a/src/routes/kuaishou.ts +++ b/src/routes/kuaishou.ts @@ -4,6 +4,8 @@ import { get } from "../utils/getData.js"; import { parseChineseNumber } from "../utils/getNum.js"; import UserAgent from "user-agents"; +const APOLLO_STATE_PREFIX = "window.__APOLLO_STATE__="; + export const handleRoute = async (_: undefined, noCache: boolean) => { const listData = await getList(noCache); const routeData: RouterData = { @@ -33,11 +35,11 @@ const getList = async (noCache: boolean) => { const listData: ListItem[] = []; // 获取主要内容 const html = result.data || ""; - const start = html.indexOf("window.__APOLLO_STATE__="); + const start = html.indexOf(APOLLO_STATE_PREFIX); if (start === -1) { throw new Error("快手页面结构变更,未找到 APOLLO_STATE"); } - const scriptSlice = html.slice(start + "window.__APOLLO_STATE__=".length); + const scriptSlice = html.slice(start + APOLLO_STATE_PREFIX.length); const sentinelA = scriptSlice.indexOf(";(function("); const sentinelB = scriptSlice.indexOf(""); const cutIndex = @@ -66,7 +68,7 @@ const getList = async (noCache: boolean) => { ?.items || []; // 获取全部热榜 - allItems?.forEach((item: { id: string }) => { + allItems.forEach((item: { id: string }) => { // 基础数据 const hotItem: RouterType["kuaishou"] = jsonObject[item.id]; if (!hotItem) return;