cortico-world-mosey 1.3.1 → 1.3.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # 更新日志
2
2
 
3
+ ## 1.3.2
4
+
5
+ - **修:点登录,窗口闪一下就没了**。`getPage()` 里隐式调了一次 `launch()`,没带窗口模式,于是它拿「后台模式」的默认值和当前的有头窗口一比,认定「模式不一致」,当场把刚开的登录窗关掉、重开成无头。B站和抖音都中招。现在 `getPage()` 沿用当前模式,不再偷偷换
6
+ - **修:旧窗口的关闭事件会误伤新窗口**。关窗回调没有校验是不是自己那个 context,旧窗口晚到的 close 会把新窗口的记录一起清掉。现在只处理自己的
7
+ - **换号更干净**:登录窗打开后、跳转前先把目标站点的 cookie 清掉,不用再多开一次无头窗口预热
8
+
3
9
  ## 1.3.1
4
10
 
5
11
  - **修:「重新登录」点了没反应、不弹窗**。1.3.0 里加了一条「已登录就直接收」的快捷路径,结果已登录时点按钮只会回一句「已经是登录状态」,窗口根本不打开。现在只要点登录就一定开窗,不再抄近路
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cortico-world-mosey",
3
- "version": "1.3.1",
3
+ "version": "1.3.2",
4
4
  "type": "module",
5
5
  "description": "摸鱼(Mosey):让 Coopanion 的桌宠自己去抖音和 B站逛一圈。关注流、周报、本地书库;视频、动态、专栏都能看能评;按额度点赞、评论、关注、发动态、私信;默认人审,三把工具、少 token。",
6
6
  "license": "MIT",
package/src/douyin.js CHANGED
@@ -202,6 +202,8 @@ export class DouyinBrowser {
202
202
  context = await chromium.launchPersistentContext(this.profileDir, { ...options, channel: 'chrome' });
203
203
  }
204
204
  context.on('close', () => {
205
+ // 旧窗口的 close 可能比新窗口的赋值还晚到,只处理自己的那一个
206
+ if (this.context !== context) return;
205
207
  this.context = null;
206
208
  this.page = null;
207
209
  this.headless = null;
@@ -221,7 +223,8 @@ export class DouyinBrowser {
221
223
  }
222
224
 
223
225
  async getPage() {
224
- const context = await this.launch();
226
+ // 沿用当前的窗口模式:开着的登录窗不能被一次隐式 launch 关掉重开
227
+ const context = await this.launch({ headless: this.headless ?? (this.cfg.background !== false) });
225
228
  if (this.page && !this.page.isClosed()) return this.page;
226
229
  this.page = await context.newPage();
227
230
  this.page.setDefaultTimeout(20000);
@@ -819,15 +822,23 @@ export class DouyinBrowser {
819
822
  * 专门用来登录的窗口:开一个有头窗口停在登录页,期间不自动关页。
820
823
  * 不复用「一起看」,免得扫码扫到一半被 scheduleClose 关掉。
821
824
  */
822
- async loginWindow(url) {
825
+ async loginWindow(url, { clearCookies = null } = {}) {
823
826
  return this.serialize(async () => {
824
- await this.launch({ headless: false, force: true });
825
- this.holdOpen = true;
827
+ // 先挂住,别让排队中的自动关页把刚开的窗口关掉
826
828
  if (this.closeTimer) clearTimeout(this.closeTimer);
827
829
  this.closeTimer = null;
828
- const page = await this.getPage();
829
- if (url) await this.goto(page, url, 2200);
830
- return { ok: true, url: page.url() };
830
+ this.holdOpen = true;
831
+ try {
832
+ await this.launch({ headless: false, force: true });
833
+ // 换号场景:新窗口里先把目标站点的 cookie 清干净,再跳登录页
834
+ if (clearCookies) await this.clearCookiesMatching(clearCookies);
835
+ const page = await this.getPage();
836
+ if (url) await this.goto(page, url, 2200);
837
+ return { ok: true, url: page.url() };
838
+ } catch (error) {
839
+ this.holdOpen = false;
840
+ throw error;
841
+ }
831
842
  });
832
843
  }
833
844
 
package/src/world.js CHANGED
@@ -524,15 +524,13 @@ export class StrollWorld {
524
524
 
525
525
  async login(platform) {
526
526
  if (platform === 'bilibili') {
527
- // 之前点过「退出登录」:先把窗口里残留的 cookie 清干净,保证这次是干净的二维码
528
- if (this.bili.isLoggedOut()) {
529
- if (!this.douyin.context) await this.douyin.launch({ headless: true }).catch(() => {});
530
- await this.douyin.clearCookiesMatching(/bilibili\.com$/i).catch(() => null);
531
- this.bili.markLoggedOut(false);
532
- }
527
+ // 之前点过「退出登录」:开窗时顺手把 B站 cookie 清掉,保证这次看到的是干净二维码
528
+ const wasOut = this.bili.isLoggedOut();
533
529
  let info;
534
530
  try {
535
- info = await this.douyin.loginWindow('https://passport.bilibili.com/login');
531
+ info = await this.douyin.loginWindow('https://passport.bilibili.com/login', {
532
+ clearCookies: wasOut ? /bilibili\.com$/i : null,
533
+ });
536
534
  } catch (error) {
537
535
  return {
538
536
  ok: false,
@@ -540,6 +538,7 @@ export class StrollWorld {
540
538
  error: `登录窗没打开:${error?.message ?? String(error)}。多半是没装 Edge / Chrome,或者上次的窗口没退干净。`,
541
539
  };
542
540
  }
541
+ if (wasOut) this.bili.markLoggedOut(false);
543
542
  // 扫码是异步的:后台轮询,登上了就把 cookie 收回来并关窗
544
543
  let tries = 0;
545
544
  const timer = setInterval(async () => {