koishi-plugin-steaminfo-xiaoheihe 0.1.0 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/lib/index.js +45 -12
  2. package/package.json +1 -1
package/lib/index.js CHANGED
@@ -86,21 +86,54 @@ function apply(ctx, config) {
86
86
  const searchUrl = `https://www.xiaoheihe.cn/app/search?q=${encodeURIComponent(game)}`;
87
87
  log(`准备导航到搜索页面: ${searchUrl}`);
88
88
  await page.goto(searchUrl, { waitUntil: "load", timeout: config.waitTimeout });
89
- const gameLinkSelector = 'a[href*="/app/topic/game/"]';
90
- let gamePageHref;
89
+ let finalUrl;
90
+ let navigationCompleted = false;
91
+ const listGameSelector = 'a[href*="/app/topic/game/"]';
92
+ log(`[Plan A] 尝试寻找列表页的游戏链接: "${listGameSelector}"`);
91
93
  try {
92
- log(`将使用选择器 "${gameLinkSelector}" 寻找游戏链接...`);
93
- await page.waitForSelector(gameLinkSelector, { timeout: 1e4 });
94
- gamePageHref = await page.$eval(gameLinkSelector, (el) => el.getAttribute("href"));
94
+ await page.waitForSelector(listGameSelector, { timeout: 5e3 });
95
+ const gamePageHref = await page.$eval(listGameSelector, (el) => el.getAttribute("href"));
96
+ finalUrl = `https://www.xiaoheihe.cn${gamePageHref}`;
97
+ log(`[Plan A] 成功!获取到链接: ${finalUrl}`);
95
98
  } catch (e) {
96
- if (config.debug) logger.warn("在搜索页未能找到游戏链接,可能是Cookie失效、游戏不存在或页面结构更新。");
97
- const screenshot = await page.screenshot({ fullPage: true });
98
- await session.send(["未能找到游戏专题链接。这是当前页面的截图:", import_koishi.segment.image(screenshot)]);
99
- return;
99
+ log(`[Plan A] 失败。切换到 Plan B...`);
100
+ try {
101
+ const communityLinkSelector = ".search-topic__topic-name";
102
+ log(`[Plan B] 尝试寻找社区链接: "${communityLinkSelector}"`);
103
+ await page.waitForSelector(communityLinkSelector, { timeout: 5e3 });
104
+ await Promise.all([
105
+ page.waitForNavigation({ waitUntil: "load", timeout: config.waitTimeout }),
106
+ page.click(communityLinkSelector)
107
+ ]);
108
+ const gameTabSelector = ".slide-tab__tab-label";
109
+ await page.waitForSelector(gameTabSelector, { timeout: 1e4 });
110
+ await Promise.all([
111
+ page.waitForNavigation({ waitUntil: "load", timeout: config.waitTimeout }),
112
+ page.click(gameTabSelector)
113
+ ]);
114
+ navigationCompleted = true;
115
+ log(`[Plan B] 成功到达最终游戏页面: ${page.url()}`);
116
+ } catch (e2) {
117
+ log(`[Plan B] 失败。切换到 Plan C...`);
118
+ try {
119
+ const singleGameCardSelector = ".search-result__game .game-rank__game-card";
120
+ log(`[Plan C] 尝试寻找并点击独立游戏卡片: "${singleGameCardSelector}"`);
121
+ await page.waitForSelector(singleGameCardSelector, { timeout: 5e3 });
122
+ await page.click(singleGameCardSelector);
123
+ navigationCompleted = true;
124
+ log(`[Plan C] 点击成功!页面将原地跳转。`);
125
+ } catch (e3) {
126
+ if (config.debug) logger.warn("在搜索页未能找到游戏链接,所有方案均失败。");
127
+ const screenshot = await page.screenshot({ fullPage: true });
128
+ await session.send(["未能找到游戏专题链接。这是当前页面的截图:", import_koishi.segment.image(screenshot)]);
129
+ return;
130
+ }
131
+ }
132
+ }
133
+ if (!navigationCompleted) {
134
+ log(`正在导航到: ${finalUrl}`);
135
+ await page.goto(finalUrl, { waitUntil: "load", timeout: config.waitTimeout });
100
136
  }
101
- const finalUrl = `https://www.xiaoheihe.cn${gamePageHref}`;
102
- log(`已获取详情页链接,正在导航到: ${finalUrl}`);
103
- await page.goto(finalUrl, { waitUntil: "load", timeout: config.waitTimeout });
104
137
  const mainContentSelector = ".game-detail-page-detail";
105
138
  log(`等待核心内容 "${mainContentSelector}" 出现...`);
106
139
  await page.waitForSelector(mainContentSelector, { timeout: config.waitTimeout });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koishi-plugin-steaminfo-xiaoheihe",
3
- "version": "0.1.0",
3
+ "version": "0.2.2",
4
4
  "description": "通过小黑盒网站搜索并生成截图获取 steam 游戏信息",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",