File size: 3,009 Bytes
08c2825
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import { chromium } from 'playwright';
import fs from 'fs';
import config from './config.js';
import { fileURLToPath } from 'url';
import path from 'path';
import { loadCookies, saveScreenshot, getHumanReadableTimestamp } from './utils/common-utils.js';
async function login() {
  console.log('启动浏览器...');
  const browser = await chromium.launch(config.browserOptions);
  const context = await browser.newContext();

  if (fs.existsSync(config.cookieFile)) {
    // 读取并设置cookies
    const cookies = loadCookies(config.cookieFile);
    await context.addCookies(cookies);
  }

  const page = await context.newPage();

  try {
    console.log(`导航到登录页面:${config.webideUrl}...`);
    // 首先访问主页面,通常会重定向到登录页面
    await page.goto(config.webideUrl);

    // 等待页面加载
    await page.waitForTimeout(config.waitTimes.pageLoad);

    console.log('当前页面URL:', page.url());
    console.log('页面标题:', await page.title());

    // 检查是否已经登录(如果页面包含编辑器元素,说明已登录)
    const isLoggedIn = await page.locator(config.selectors.editor).count() > 0;

    if (isLoggedIn) {
      console.log('检测到已经登录状态,保存cookie...');
    } else {
      console.log('需要登录,请在浏览器中手动完成登录过程...');
      console.log('登录完成后,请按 Enter 键继续...');

      // 等待用户手动登录
      await waitForUserInput();

      // 等待登录完成,检查是否出现编辑器界面
      console.log('等待登录完成...');
      try {
        await page.waitForSelector(config.selectors.editor, {
          timeout: 60000
        });

      } catch (error) {
        console.log('未检测到编辑器界面,但继续保存cookie...');
      }
    }

    // 保存cookies
    const cookies = await context.cookies();
    fs.writeFileSync(config.cookieFile, JSON.stringify(cookies, null, 2));
    console.log(`Cookies已保存到 ${config.cookieFile}`);
    console.log(`保存了 ${cookies.length} 个cookies`);

    // 显示保存的cookie信息(仅显示名称,不显示值)
    console.log('保存的cookie名称:');
    cookies.forEach(cookie => {
      console.log(`  - ${cookie.name} (域名: ${cookie.domain})`);
    });

  } catch (error) {
    console.error('登录过程中发生错误:', error);
  } finally {
    await browser.close();
  }
}

// 等待用户输入的辅助函数
async function waitForUserInput() {
  const { default: readline } = await import('readline');
  return new Promise((resolve) => {
    const rl = readline.createInterface({
      input: process.stdin,
      output: process.stdout
    });

    rl.question('', () => {
      rl.close();
      resolve();
    });
  });
}

// 运行命令执行脚本
const __filename = fileURLToPath(import.meta.url);
const scriptPath = path.resolve(process.argv[1]);

if (path.resolve(__filename) === scriptPath) {
  login().catch(console.error);
}