diff --git a/douyin/.gitignore b/douyin/.gitignore new file mode 100644 index 0000000..fd94589 --- /dev/null +++ b/douyin/.gitignore @@ -0,0 +1 @@ +profiles \ No newline at end of file diff --git a/douyin/index.ts b/douyin/index.ts new file mode 100644 index 0000000..a11b05e --- /dev/null +++ b/douyin/index.ts @@ -0,0 +1,26 @@ +// node run.js https://example.com/dashboard +import { chromium } from 'playwright'; + +const userDataDir = './profiles/site1'; +const targetUrl = process.argv[2] || 'https://baidu.com/'; + +const context = await chromium.launchPersistentContext(userDataDir, { + headless: true, // 无头 +}); +await context.addInitScript({ path: './userscripts/my-script.js' }); + +const page = context.pages()[0] || await context.newPage(); +await page.goto(targetUrl, { waitUntil: 'networkidle' }); + +// === 你的自动化逻辑(示例)=== +await page.click('text=开始任务', { timeout: 10_000 }).catch(()=>{}); +await page.waitForTimeout(1000); + +// @ts-ignore +console.log('当前用户:', await page.evaluate(() => document.title)); + +// 示例:截图/导出结果 +await page.screenshot({ path: './out.png', fullPage: true }); + +await context.close(); +console.log('Done.'); diff --git a/douyin/login.ts b/douyin/login.ts new file mode 100644 index 0000000..7b63603 --- /dev/null +++ b/douyin/login.ts @@ -0,0 +1,25 @@ +// node login.js https://example.com/login +import { chromium } from 'playwright'; + +const userDataDir = './profiles/site1'; +const startUrl = process.argv[2] || 'https://douyin.com'; + +const context = await chromium.launchPersistentContext(userDataDir, { + headless: false, // 首次建议“有头”,方便手工/2FA + args: ['--disable-blink-features=AutomationControlled'], +}); +await context.addInitScript({ path: './userscripts/my-script.js' }); + +const page = context.pages()[0] || await context.newPage(); +await page.goto(startUrl, { waitUntil: 'domcontentloaded' }); + +// 在这一步完成你的登录(手输或自动化都可) +console.log('>>> 请在打开的浏览器里完成登录,完成后回到终端按回车...'); +process.stdin.resume(); +await new Promise(r => process.stdin.once('data', r)); + +// 可选:导出 storageState 做备份(即使不用它,持久化目录也保存了) +await context.storageState({ path: `${userDataDir}/storageState.json` }); +await context.close(); + +console.log('>>> 登录态已写入 profiles/site1/,后续可无头复用。'); diff --git a/douyin/out.png b/douyin/out.png new file mode 100644 index 0000000..be1e42a Binary files /dev/null and b/douyin/out.png differ diff --git a/douyin/userscripts/my-script.js b/douyin/userscripts/my-script.js new file mode 100644 index 0000000..e69de29