获取 cookies 成功

This commit is contained in:
cheney 2026-07-17 18:46:36 +08:00
commit 5bbfe35c9c
6 changed files with 731 additions and 0 deletions

176
.gitignore vendored Normal file
View File

@ -0,0 +1,176 @@
node_modules/.package-lock.json
node_modules/.bin/playwright
node_modules/.bin/playwright-core
node_modules/.bin/playwright-core.cmd
node_modules/.bin/playwright-core.ps1
node_modules/.bin/playwright.cmd
node_modules/.bin/playwright.ps1
node_modules/playwright/cli.js
node_modules/playwright/index.d.ts
node_modules/playwright/index.js
node_modules/playwright/index.mjs
node_modules/playwright/jsx-runtime.js
node_modules/playwright/jsx-runtime.mjs
node_modules/playwright/LICENSE
node_modules/playwright/NOTICE
node_modules/playwright/package.json
node_modules/playwright/README.md
node_modules/playwright/test.d.ts
node_modules/playwright/test.js
node_modules/playwright/test.mjs
node_modules/playwright/ThirdPartyNotices.txt
node_modules/playwright/lib/errorContext.js
node_modules/playwright/lib/globals.js
node_modules/playwright/lib/index.js
node_modules/playwright/lib/isomorphic.js
node_modules/playwright/lib/isomorphic.js.txt
node_modules/playwright/lib/package.js
node_modules/playwright/lib/program.js
node_modules/playwright/lib/util.js
node_modules/playwright/lib/agents/agentParser.js
node_modules/playwright/lib/agents/copilot-setup-steps.yml
node_modules/playwright/lib/agents/generateAgents.js
node_modules/playwright/lib/agents/playwright-test-coverage.prompt.md
node_modules/playwright/lib/agents/playwright-test-generate.prompt.md
node_modules/playwright/lib/agents/playwright-test-generator.agent.md
node_modules/playwright/lib/agents/playwright-test-heal.prompt.md
node_modules/playwright/lib/agents/playwright-test-healer.agent.md
node_modules/playwright/lib/agents/playwright-test-plan.prompt.md
node_modules/playwright/lib/agents/playwright-test-planner.agent.md
node_modules/playwright/lib/cli/reportActions.js
node_modules/playwright/lib/cli/testActions.js
node_modules/playwright/lib/common/index.js
node_modules/playwright/lib/common/index.js.txt
node_modules/playwright/lib/loader/loaderProcessEntry.js
node_modules/playwright/lib/loader/loaderProcessEntry.js.txt
node_modules/playwright/lib/matchers/expect.js
node_modules/playwright/lib/matchers/expect.js.LICENSE
node_modules/playwright/lib/matchers/expect.js.txt
node_modules/playwright/lib/mcp/test/browserBackend.js
node_modules/playwright/lib/mcp/test/generatorTools.js
node_modules/playwright/lib/mcp/test/plannerTools.js
node_modules/playwright/lib/mcp/test/seed.js
node_modules/playwright/lib/mcp/test/streams.js
node_modules/playwright/lib/mcp/test/testBackend.js
node_modules/playwright/lib/mcp/test/testContext.js
node_modules/playwright/lib/mcp/test/testTool.js
node_modules/playwright/lib/mcp/test/testTools.js
node_modules/playwright/lib/runner/index.js
node_modules/playwright/lib/runner/index.js.txt
node_modules/playwright/lib/transform/babelBundle.js
node_modules/playwright/lib/transform/babelBundle.js.LICENSE
node_modules/playwright/lib/transform/babelBundle.js.txt
node_modules/playwright/lib/transform/esmLoader.js
node_modules/playwright/lib/transform/esmLoader.js.LICENSE
node_modules/playwright/lib/transform/esmLoader.js.txt
node_modules/playwright/lib/worker/workerProcessEntry.js
node_modules/playwright/lib/worker/workerProcessEntry.js.txt
node_modules/playwright/types/test.d.ts
node_modules/playwright/types/testReporter.d.ts
node_modules/playwright-core/browsers.json
node_modules/playwright-core/cli.js
node_modules/playwright-core/index.d.ts
node_modules/playwright-core/index.js
node_modules/playwright-core/index.mjs
node_modules/playwright-core/LICENSE
node_modules/playwright-core/NOTICE
node_modules/playwright-core/package.json
node_modules/playwright-core/README.md
node_modules/playwright-core/ThirdPartyNotices.txt
node_modules/playwright-core/bin/install_media_pack.ps1
node_modules/playwright-core/bin/install_webkit_wsl.ps1
node_modules/playwright-core/bin/reinstall_chrome_beta_linux.sh
node_modules/playwright-core/bin/reinstall_chrome_beta_mac.sh
node_modules/playwright-core/bin/reinstall_chrome_beta_win.ps1
node_modules/playwright-core/bin/reinstall_chrome_stable_linux.sh
node_modules/playwright-core/bin/reinstall_chrome_stable_mac.sh
node_modules/playwright-core/bin/reinstall_chrome_stable_win.ps1
node_modules/playwright-core/bin/reinstall_msedge_beta_linux.sh
node_modules/playwright-core/bin/reinstall_msedge_beta_mac.sh
node_modules/playwright-core/bin/reinstall_msedge_beta_win.ps1
node_modules/playwright-core/bin/reinstall_msedge_dev_linux.sh
node_modules/playwright-core/bin/reinstall_msedge_dev_mac.sh
node_modules/playwright-core/bin/reinstall_msedge_dev_win.ps1
node_modules/playwright-core/bin/reinstall_msedge_stable_linux.sh
node_modules/playwright-core/bin/reinstall_msedge_stable_mac.sh
node_modules/playwright-core/bin/reinstall_msedge_stable_win.ps1
node_modules/playwright-core/lib/bootstrap.js
node_modules/playwright-core/lib/coreBundle.js
node_modules/playwright-core/lib/package.js
node_modules/playwright-core/lib/serverRegistry.js
node_modules/playwright-core/lib/serverRegistry.js.LICENSE
node_modules/playwright-core/lib/utilsBundle.js
node_modules/playwright-core/lib/utilsBundle.js.LICENSE
node_modules/playwright-core/lib/xdg-open
node_modules/playwright-core/lib/entry/cliDaemon.js
node_modules/playwright-core/lib/entry/dashboardApp.js
node_modules/playwright-core/lib/entry/mcp.js
node_modules/playwright-core/lib/entry/oopBrowserDownload.js
node_modules/playwright-core/lib/server/deviceDescriptorsSource.json
node_modules/playwright-core/lib/server/chromium/appIcon.png
node_modules/playwright-core/lib/server/electron/loader.js
node_modules/playwright-core/lib/tools/cli-client/channelSessions.js
node_modules/playwright-core/lib/tools/cli-client/cli.js
node_modules/playwright-core/lib/tools/cli-client/help.json
node_modules/playwright-core/lib/tools/cli-client/minimist.js
node_modules/playwright-core/lib/tools/cli-client/output.js
node_modules/playwright-core/lib/tools/cli-client/program.js
node_modules/playwright-core/lib/tools/cli-client/registry.js
node_modules/playwright-core/lib/tools/cli-client/session.js
node_modules/playwright-core/lib/tools/cli-client/skill/SKILL.md
node_modules/playwright-core/lib/tools/cli-client/skill/references/element-attributes.md
node_modules/playwright-core/lib/tools/cli-client/skill/references/playwright-tests.md
node_modules/playwright-core/lib/tools/cli-client/skill/references/request-mocking.md
node_modules/playwright-core/lib/tools/cli-client/skill/references/running-code.md
node_modules/playwright-core/lib/tools/cli-client/skill/references/session-management.md
node_modules/playwright-core/lib/tools/cli-client/skill/references/spec-driven-testing.md
node_modules/playwright-core/lib/tools/cli-client/skill/references/storage-state.md
node_modules/playwright-core/lib/tools/cli-client/skill/references/test-generation.md
node_modules/playwright-core/lib/tools/cli-client/skill/references/tracing.md
node_modules/playwright-core/lib/tools/cli-client/skill/references/video-recording.md
node_modules/playwright-core/lib/tools/dashboard/appIcon.png
node_modules/playwright-core/lib/tools/trace/SKILL.md
node_modules/playwright-core/lib/tools/utils/extension.js
node_modules/playwright-core/lib/tools/utils/socketConnection.js
node_modules/playwright-core/lib/vite/dashboard/index.html
node_modules/playwright-core/lib/vite/dashboard/playwright-logo.svg
node_modules/playwright-core/lib/vite/dashboard/assets/codicon-DCmgc-ay.ttf
node_modules/playwright-core/lib/vite/dashboard/assets/firefox-1bWoP6pv.svg
node_modules/playwright-core/lib/vite/dashboard/assets/firefox-beta-k3eOH_eK.svg
node_modules/playwright-core/lib/vite/dashboard/assets/firefox-nightly-Cp5nfeDT.svg
node_modules/playwright-core/lib/vite/dashboard/assets/index-BY2S1tHT.css
node_modules/playwright-core/lib/vite/dashboard/assets/index-C_5TMfeg.js
node_modules/playwright-core/lib/vite/dashboard/assets/safari-na3_-uQk.svg
node_modules/playwright-core/lib/vite/htmlReport/index.html
node_modules/playwright-core/lib/vite/htmlReport/report.css
node_modules/playwright-core/lib/vite/htmlReport/report.js
node_modules/playwright-core/lib/vite/recorder/index.html
node_modules/playwright-core/lib/vite/recorder/playwright-logo.svg
node_modules/playwright-core/lib/vite/recorder/assets/codeMirrorModule-DeBYQozu.js
node_modules/playwright-core/lib/vite/recorder/assets/codeMirrorModule-DYBRYzYX.css
node_modules/playwright-core/lib/vite/recorder/assets/codicon-DCmgc-ay.ttf
node_modules/playwright-core/lib/vite/recorder/assets/index-4ZiSSCmn.css
node_modules/playwright-core/lib/vite/recorder/assets/index-Bq-mQf8S.js
node_modules/playwright-core/lib/vite/traceViewer/codeMirrorModule.DYBRYzYX.css
node_modules/playwright-core/lib/vite/traceViewer/codicon.DCmgc-ay.ttf
node_modules/playwright-core/lib/vite/traceViewer/defaultSettingsView.CjdS-WJx.css
node_modules/playwright-core/lib/vite/traceViewer/index.CzXZzn5A.css
node_modules/playwright-core/lib/vite/traceViewer/index.DMMX1gXU.js
node_modules/playwright-core/lib/vite/traceViewer/index.html
node_modules/playwright-core/lib/vite/traceViewer/manifest.webmanifest
node_modules/playwright-core/lib/vite/traceViewer/playwright-logo.svg
node_modules/playwright-core/lib/vite/traceViewer/snapshot.html
node_modules/playwright-core/lib/vite/traceViewer/snapshot.v8KI4P3m.js
node_modules/playwright-core/lib/vite/traceViewer/sw.bundle.js
node_modules/playwright-core/lib/vite/traceViewer/uiMode.BZQ54Kgt.css
node_modules/playwright-core/lib/vite/traceViewer/uiMode.html
node_modules/playwright-core/lib/vite/traceViewer/uiMode.Ut8wwJNp.js
node_modules/playwright-core/lib/vite/traceViewer/xtermModule.DYP7pi_n.css
node_modules/playwright-core/lib/vite/traceViewer/assets/codeMirrorModule-LEHpjmcn.js
node_modules/playwright-core/lib/vite/traceViewer/assets/defaultSettingsView-BNmKHKpQ.js
node_modules/playwright-core/lib/vite/traceViewer/assets/urlMatch-BYQrIQwR.js
node_modules/playwright-core/lib/vite/traceViewer/assets/xtermModule-CsJ4vdCR.js
node_modules/playwright-core/types/protocol.d.ts
node_modules/playwright-core/types/structs.d.ts
node_modules/playwright-core/types/types.d.ts
config.json

20
Readme.md Normal file
View File

@ -0,0 +1,20 @@
# 抖小云插件
1. 基于 playwright 获取 cookie
Cookie 及 sec_user_id 是同步功能的核心,需严格按步骤获取,避免遗漏或错误。
1.1 提取Cookie以及 sec_uer_id
打开 抖音网页版 (https://www.douyin.com/) 并登录目标账号;
进入个人主页按下F12进入开发者模式、并切换到 网络(也可能叫network).
在筛选框中输入/follow.
点击自己头像边上的 关注 按钮、会弹出你的关注列表,然后在右侧网络请求里面会出现多个请求,随便选一个.
在请求的标签里面切换到负载(也可能叫payload)
找到sec_user_id复制值即可
在请求的标签里面切换到标头(也可能叫Headers)
往下拉直到出现Cookie ,复制完整的值,注意前后不要带换行符,很多人会多复制个换行符出来.
2. 已知 Cookie 及 sec_user_id, 实现清空收藏和点赞功能

210
get-cookie.js Normal file
View File

@ -0,0 +1,210 @@
// 获取抖音 Cookie 和 sec_user_id 的 Playwright 脚本
// 用法: node get-cookie.js
// 会自动打开 Chrome 浏览器,请扫码登录抖音
import { chromium } from "playwright";
import { writeFileSync, readFileSync } from "fs";
import { resolve, dirname } from "path";
import { fileURLToPath } from "url";
const __dirname = dirname(fileURLToPath(import.meta.url));
const CONFIG_PATH = resolve(__dirname, "config.json");
async function main() {
console.log("🚀 启动浏览器...");
const browser = await chromium.launch({
channel: "chrome",
headless: false,
args: ["--disable-blink-features=AutomationControlled"],
});
const context = await browser.newContext({
viewport: { width: 1366, height: 768 },
userAgent:
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36",
});
const page = await context.newPage();
// 监听网络请求,捕获 sec_user_id
let capturedSecUserId = null;
page.on("request", (request) => {
const url = request.url();
if (url.includes("follow") || url.includes("aweme")) {
try {
const postData = request.postData();
if (postData && postData.includes("sec_user_id")) {
const parsed = JSON.parse(postData);
if (parsed.sec_user_id) {
capturedSecUserId = parsed.sec_user_id;
console.log("✅ 从请求中捕获到 sec_user_id:", capturedSecUserId);
}
}
} catch (e) {
// 忽略解析错误
}
}
});
// 也监听响应,从 body 中提取 sec_user_id
page.on("response", async (response) => {
const url = response.url();
if (url.includes("user/self") || url.includes("aweme")) {
try {
const body = await response.text();
const match = body.match(/"sec_uid":"([^"]+)"/);
if (match && !capturedSecUserId) {
capturedSecUserId = match[1];
console.log("✅ 从响应中捕获到 sec_user_id:", capturedSecUserId);
}
} catch (e) {
// 忽略
}
}
});
console.log("📱 正在打开抖音登录页...");
await page.goto("https://www.douyin.com/", {
waitUntil: "domcontentloaded",
timeout: 30000,
});
console.log("🔑 请在浏览器中扫码登录抖音...");
console.log(" (等待登录完成,检测到登录后会自动继续)");
// 等待登录成功:检测 cookie 中是否有 sessionid
await waitForLogin(context);
console.log("✅ 登录成功!");
// 导航到个人主页以触发 sec_user_id 相关请求
console.log("📋 正在访问个人主页获取 sec_user_id...");
await page.goto("https://www.douyin.com/user/self?from_tab_name=main", {
waitUntil: "networkidle",
timeout: 30000,
});
// 等待页面加载
await page.waitForTimeout(3000);
// 尝试从页面数据中提取 sec_user_id
if (!capturedSecUserId) {
capturedSecUserId = await extractSecUserIdFromPage(page);
}
// 如果还没获取到,尝试点击关注按钮触发请求
if (!capturedSecUserId) {
console.log("🖱️ 尝试触发关注列表请求以获取 sec_user_id...");
try {
// 点击关注按钮
const followBtn = page.locator('[data-e2e="follow-count"]').first();
if (await followBtn.isVisible({ timeout: 5000 })) {
await followBtn.click();
await page.waitForTimeout(3000);
}
} catch (e) {
console.log(" 未能找到关注按钮,尝试其他方式...");
}
// 再次尝试从请求中获取
if (!capturedSecUserId) {
capturedSecUserId = await extractSecUserIdFromPage(page);
}
}
// 获取所有 cookies
const cookies = await context.cookies();
const cookieString = cookies
.map((c) => `${c.name}=${c.value}`)
.join("; ");
console.log("\n========================================");
console.log("📋 获取结果:");
console.log("========================================");
console.log("Cookie:", cookieString.substring(0, 200) + "...");
console.log("sec_user_id:", capturedSecUserId || "❌ 未获取到");
console.log("========================================\n");
// 读取现有配置
let config = {};
try {
config = JSON.parse(readFileSync(CONFIG_PATH, "utf8"));
} catch (e) {
console.log(" 未找到现有配置,将创建新配置");
}
// 更新配置
config.cookie = cookieString;
if (capturedSecUserId) {
config.sec_user_id = capturedSecUserId;
}
writeFileSync(CONFIG_PATH, JSON.stringify(config, null, 4), "utf8");
console.log(`💾 配置已保存到: ${CONFIG_PATH}`);
if (!capturedSecUserId) {
console.log("\n⚠ 未能自动获取 sec_user_id");
console.log(" 请参考 README 手动获取:");
console.log(" 1. 按 F12 打开开发者工具 -> Network");
console.log(" 2. 筛选 'follow'");
console.log(" 3. 点击关注按钮,在请求 Payload 中找到 sec_user_id");
console.log(" 4. 手动填入 config.json");
}
await browser.close();
console.log("👋 浏览器已关闭,脚本执行完毕");
}
// 等待用户登录成功
async function waitForLogin(context) {
return new Promise((resolve) => {
const checkInterval = setInterval(async () => {
const cookies = await context.cookies();
const hasSession = cookies.some((c) => c.name === "sessionid");
if (hasSession) {
clearInterval(checkInterval);
resolve();
}
}, 2000);
});
}
// 从页面中提取 sec_user_id
async function extractSecUserIdFromPage(page) {
// 方法1: 从页面 URL 提取
const url = page.url();
const urlMatch = url.match(/user\/([^?]+)/);
if (urlMatch && urlMatch[1] !== "self") {
return urlMatch[1];
}
// 方法2: 从页面 HTML 中的 __INITIAL_STATE__ 提取
try {
const html = await page.content();
const match = html.match(/"sec_uid":"([^"]+)"/);
if (match) return match[1];
} catch (e) {
// 忽略
}
// 方法3: 从 window.__INITIAL_STATE__ 提取
try {
const secUid = await page.evaluate(() => {
const state = window.__INITIAL_STATE__;
if (state?.user?.sec_uid) return state.user.sec_uid;
return null;
});
if (secUid) return secUid;
} catch (e) {
// 忽略
}
return null;
}
main().catch((err) => {
console.error("❌ 脚本执行出错:", err.message);
process.exit(1);
});

60
package-lock.json generated Normal file
View File

@ -0,0 +1,60 @@
{
"name": "p20260129-douxiaoyun",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "p20260129-douxiaoyun",
"version": "1.0.0",
"license": "ISC",
"dependencies": {
"playwright": "^1.61.1"
}
},
"node_modules/fsevents": {
"version": "2.3.2",
"resolved": "https://registry.npmmirror.com/fsevents/-/fsevents-2.3.2.tgz",
"integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==",
"hasInstallScript": true,
"license": "MIT",
"optional": true,
"os": [
"darwin"
],
"engines": {
"node": "^8.16.0 || ^10.6.0 || >=11.0.0"
}
},
"node_modules/playwright": {
"version": "1.61.1",
"resolved": "https://registry.npmmirror.com/playwright/-/playwright-1.61.1.tgz",
"integrity": "sha512-DWnY5o3YbLWK4GovuAVwpqL+1VwGNdUGrRr++8j8PtQQzvAVZUIMjKQ90fY689sEJZJBbZVw1rXaOKSTitkzPQ==",
"license": "Apache-2.0",
"dependencies": {
"playwright-core": "1.61.1"
},
"bin": {
"playwright": "cli.js"
},
"engines": {
"node": ">=18"
},
"optionalDependencies": {
"fsevents": "2.3.2"
}
},
"node_modules/playwright-core": {
"version": "1.61.1",
"resolved": "https://registry.npmmirror.com/playwright-core/-/playwright-core-1.61.1.tgz",
"integrity": "sha512-h7Qlt6m4REp25qvIdvbDtVmD4LqVXfpRxhORv9L0jzETM05p4fuPJ3dKyuSXQxDSbXnmS79HAgi9589lGSpLkg==",
"license": "Apache-2.0",
"bin": {
"playwright-core": "cli.js"
},
"engines": {
"node": ">=18"
}
}
}
}

15
package.json Normal file
View File

@ -0,0 +1,15 @@
{
"name": "p20260129-douxiaoyun",
"version": "1.0.0",
"description": "宸茬煡 Cookie 鍙?sec_user_id, 瀹炵幇娓呯┖鏀惰棌鍜岀偣璧炲姛鑳?",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" \u0026\u0026 exit 1",
"get-cookie": "node get-cookie.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"playwright": "^1.61.1"
}
}

250
src/index.js Normal file

File diff suppressed because one or more lines are too long