diff --git a/extensions/tiktok-cookie-helper/README.md b/extensions/tiktok-cookie-helper/README.md new file mode 100644 index 0000000..2a5f3a2 --- /dev/null +++ b/extensions/tiktok-cookie-helper/README.md @@ -0,0 +1,26 @@ +# dylive TikTok Cookie 助手 + +TikTok 的 `sessionid` 一般是 **HttpOnly**,页面 JS / 普通油猴读不到。 +**推荐 Chrome 插件**(`chrome.cookies` 可读 HttpOnly)。油猴脚本需要 Tampermonkey 的 `GM.cookie`。 + +## Chrome 插件(推荐) + +1. 打开 `chrome://extensions` +2. 打开右上角「开发者模式」 +3. 「加载已解压的扩展程序」→ 选本目录 `extensions/tiktok-cookie-helper` +4. 浏览器登录 [tiktok.com](https://www.tiktok.com) +5. 点工具栏图标 → **复制 JSON** 或 **填入控制台** +6. 到 [dylive /tiktok](https://dylive.nomadro.com/tiktok) 打开「粘贴 Cookie」,点保存 + +## 油猴脚本 + +1. 安装 Tampermonkey +2. 导入 `tiktok-cookie.user.js` +3. 打开并登录 tiktok.com,点右下角「导出 Cookie → dylive」 +4. 回到控制台「从剪贴板导入」 + +若提示不支持 `GM.cookie`,改用 Chrome 插件。 + +## 安全 + +`sessionid` 等同账号密码。不要发给别人,不要贴到聊天软件。 diff --git a/extensions/tiktok-cookie-helper/background.js b/extensions/tiktok-cookie-helper/background.js new file mode 100644 index 0000000..ebabdd8 --- /dev/null +++ b/extensions/tiktok-cookie-helper/background.js @@ -0,0 +1,46 @@ +const TIKTOK_URLS = [ + "https://www.tiktok.com/", + "https://www.tiktok.com", +]; + +function cookieMap(cookies) { + const out = {}; + for (const c of cookies || []) { + if (c && c.name) out[c.name] = c.value || ""; + } + return out; +} + +async function readTikTokCookies() { + const bags = await Promise.all( + TIKTOK_URLS.map((url) => chrome.cookies.getAll({ url }).catch(() => [])), + ); + const byName = cookieMap(bags.flat()); + const extra = await chrome.cookies.getAll({ domain: ".tiktok.com" }).catch(() => []); + Object.assign(byName, cookieMap(extra)); + + const sessionid = String(byName.sessionid || byName.sessionid_ss || "").trim(); + const dc = String(byName["tt-target-idc"] || byName.tt_target_idc || "").trim() || "useast2a"; + const username = String(byName["uniqueid"] || byName.uniqueId || "").trim(); + return { + ok: Boolean(sessionid), + sessionid, + dc_id: dc, + tiktok_username: username, + payload: { + sessionid, + dc_id: dc, + tiktok_username: username, + }, + }; +} + +chrome.runtime.onMessage.addListener((msg, _sender, sendResponse) => { + if (msg?.type === "GET_TIKTOK_COOKIES") { + readTikTokCookies() + .then(sendResponse) + .catch((err) => sendResponse({ ok: false, error: String(err) })); + return true; + } + return false; +}); diff --git a/extensions/tiktok-cookie-helper/content-dylive.js b/extensions/tiktok-cookie-helper/content-dylive.js new file mode 100644 index 0000000..0c3e350 --- /dev/null +++ b/extensions/tiktok-cookie-helper/content-dylive.js @@ -0,0 +1,34 @@ +function fillCookieForm(payload) { + const sid = document.getElementById("tiktok-sessionid"); + const dc = document.getElementById("tiktok-dc-id"); + const user = document.getElementById("tiktok-username"); + if (!sid || !dc) { + window.dispatchEvent(new CustomEvent("dylive-tiktok-cookie", { detail: payload })); + return { ok: false, reason: "form-hidden" }; + } + const setNative = (el, value) => { + const proto = Object.getPrototypeOf(el); + const desc = Object.getOwnPropertyDescriptor(proto, "value"); + desc?.set?.call(el, value); + el.dispatchEvent(new Event("input", { bubbles: true })); + el.dispatchEvent(new Event("change", { bubbles: true })); + }; + setNative(sid, payload.sessionid || ""); + setNative(dc, payload.dc_id || "useast2a"); + if (user && payload.tiktok_username) setNative(user, payload.tiktok_username); + return { ok: true }; +} + +chrome.runtime.onMessage.addListener((msg, _sender, sendResponse) => { + if (msg?.type !== "DYLIVE_FILL_TIKTOK_COOKIE") return; + sendResponse(fillCookieForm(msg.payload || {})); +}); + +window.addEventListener("dylive-tiktok-cookie-request", async () => { + try { + const data = await chrome.runtime.sendMessage({ type: "GET_TIKTOK_COOKIES" }); + if (data?.ok) window.dispatchEvent(new CustomEvent("dylive-tiktok-cookie", { detail: data.payload })); + } catch { + /* popup / page 未授权时忽略 */ + } +}); diff --git a/extensions/tiktok-cookie-helper/icon.png b/extensions/tiktok-cookie-helper/icon.png new file mode 100644 index 0000000..0018142 Binary files /dev/null and b/extensions/tiktok-cookie-helper/icon.png differ diff --git a/extensions/tiktok-cookie-helper/manifest.json b/extensions/tiktok-cookie-helper/manifest.json new file mode 100644 index 0000000..207fc7b --- /dev/null +++ b/extensions/tiktok-cookie-helper/manifest.json @@ -0,0 +1,36 @@ +{ + "manifest_version": 3, + "name": "dylive TikTok Cookie", + "version": "1.0.0", + "description": "从已登录的 tiktok.com 读取 sessionid / tt-target-idc,一键复制或填入 dylive 控制台。", + "icons": { + "128": "icon.png" + }, + "action": { + "default_title": "导出 TikTok Cookie", + "default_popup": "popup.html", + "default_icon": "icon.png" + }, + "permissions": ["cookies", "tabs", "clipboardWrite", "scripting"], + "host_permissions": [ + "https://www.tiktok.com/*", + "https://*.tiktok.com/*", + "https://dylive.nomadro.com/*", + "http://127.0.0.1/*", + "http://localhost/*" + ], + "background": { + "service_worker": "background.js" + }, + "content_scripts": [ + { + "matches": [ + "https://dylive.nomadro.com/tiktok*", + "http://127.0.0.1:*/tiktok*", + "http://localhost:*/tiktok*" + ], + "js": ["content-dylive.js"], + "run_at": "document_idle" + } + ] +} diff --git a/extensions/tiktok-cookie-helper/popup.css b/extensions/tiktok-cookie-helper/popup.css new file mode 100644 index 0000000..ffe9560 --- /dev/null +++ b/extensions/tiktok-cookie-helper/popup.css @@ -0,0 +1,66 @@ +:root { + color-scheme: dark; +} +body { + margin: 0; + width: 320px; + font: 13px/1.45 system-ui, sans-serif; + background: #0f1419; + color: #e8eef5; +} +header { + padding: 12px 14px 8px; +} +h1 { + margin: 0; + font-size: 15px; +} +header p { + margin: 4px 0 0; + color: #8b98a8; + font-size: 12px; +} +.status { + margin: 0 14px 10px; + padding: 8px 10px; + border-radius: 8px; + font-size: 12px; +} +.status.idle { background: #1c2430; color: #9aa8b8; } +.status.ok { background: #113228; color: #7ee0a8; } +.status.err { background: #3a1518; color: #ff9aa3; } +dl { + margin: 0 14px 12px; + display: grid; + grid-template-columns: 110px 1fr; + gap: 6px 8px; +} +dt { color: #8b98a8; } +dd { + margin: 0; + font-family: ui-monospace, Consolas, monospace; + word-break: break-all; +} +.actions { + display: grid; + gap: 6px; + padding: 0 14px 8px; +} +button { + border: 0; + border-radius: 8px; + padding: 8px 10px; + cursor: pointer; + background: #ff2d55; + color: #fff; + font-weight: 600; +} +button:disabled { opacity: 0.45; cursor: not-allowed; } +button#fill, button#open-dy { background: #2a3340; } +button#open-tt { background: #1c2430; color: #d7e0ea; } +.hint { + margin: 0; + padding: 0 14px 14px; + color: #8b98a8; + font-size: 11px; +} diff --git a/extensions/tiktok-cookie-helper/popup.html b/extensions/tiktok-cookie-helper/popup.html new file mode 100644 index 0000000..67a2343 --- /dev/null +++ b/extensions/tiktok-cookie-helper/popup.html @@ -0,0 +1,29 @@ + + + + + dylive TikTok Cookie + + + +
+

TikTok Cookie

+

登录 tiktok.com 后点导出,无需 F12

+
+
正在读取…
+
+
sessionid
+
—
+
tt-target-idc
+
—
+
+
+ + + + +
+

sessionid 等同密码,只粘贴到自己的 dylive 控制台。

+ + + diff --git a/extensions/tiktok-cookie-helper/popup.js b/extensions/tiktok-cookie-helper/popup.js new file mode 100644 index 0000000..6dbc3ae --- /dev/null +++ b/extensions/tiktok-cookie-helper/popup.js @@ -0,0 +1,64 @@ +const CONSOLE_URL = "https://dylive.nomadro.com/tiktok"; +let lastPayload = null; + +function mask(v) { + const s = String(v || ""); + if (s.length <= 8) return s ? "***" : "—"; + return `${s.slice(0, 4)}…${s.slice(-4)}`; +} + +function setStatus(kind, text) { + const el = document.getElementById("status"); + el.className = `status ${kind}`; + el.textContent = text; +} + +async function loadCookies() { + const data = await chrome.runtime.sendMessage({ type: "GET_TIKTOK_COOKIES" }); + lastPayload = data?.ok ? data.payload : null; + document.getElementById("sid").textContent = data?.ok ? mask(data.sessionid) : "未登录 / 未找到"; + document.getElementById("dc").textContent = data?.dc_id || "useast2a"; + document.getElementById("copy").disabled = !data?.ok; + document.getElementById("fill").disabled = !data?.ok; + if (data?.ok) setStatus("ok", "已读到 Cookie,可复制或填入控制台"); + else setStatus("err", data?.error || "请先在本浏览器登录 tiktok.com,再点插件"); +} + +document.getElementById("copy").addEventListener("click", async () => { + if (!lastPayload) return; + await navigator.clipboard.writeText(JSON.stringify(lastPayload)); + setStatus("ok", "已复制 JSON,到控制台点「从剪贴板导入」"); +}); + +document.getElementById("fill").addEventListener("click", async () => { + if (!lastPayload) return; + const tabs = await chrome.tabs.query({}); + const target = tabs.find((t) => /\/tiktok(\?|$)/.test(t.url || "") && /dylive\.nomadro\.com|127\.0\.0\.1|localhost/.test(t.url || "")); + if (!target?.id) { + await chrome.tabs.create({ url: CONSOLE_URL }); + setStatus("idle", "已打开控制台。打开「粘贴 Cookie」后再点一次「填入控制台」"); + return; + } + await chrome.tabs.update(target.id, { active: true }); + try { + await chrome.tabs.sendMessage(target.id, { type: "DYLIVE_FILL_TIKTOK_COOKIE", payload: lastPayload }); + setStatus("ok", "已填入控制台表单,确认后点保存"); + } catch { + await chrome.scripting.executeScript({ + target: { tabId: target.id }, + files: ["content-dylive.js"], + }); + await chrome.tabs.sendMessage(target.id, { type: "DYLIVE_FILL_TIKTOK_COOKIE", payload: lastPayload }); + setStatus("ok", "已填入控制台表单,确认后点保存"); + } +}); + +document.getElementById("open-tt").addEventListener("click", () => { + chrome.tabs.create({ url: "https://www.tiktok.com/login" }); +}); + +document.getElementById("open-dy").addEventListener("click", () => { + chrome.tabs.create({ url: CONSOLE_URL }); +}); + +void loadCookies(); diff --git a/extensions/tiktok-cookie-helper/tiktok-cookie.user.js b/extensions/tiktok-cookie-helper/tiktok-cookie.user.js new file mode 100644 index 0000000..7c46af5 --- /dev/null +++ b/extensions/tiktok-cookie-helper/tiktok-cookie.user.js @@ -0,0 +1,74 @@ +// ==UserScript== +// @name dylive TikTok Cookie 导出 +// @namespace https://dylive.nomadro.com +// @version 1.0.0 +// @description 在已登录的 tiktok.com 一键复制 sessionid / tt-target-idc(需 GM.cookie,可读取 HttpOnly) +// @match https://www.tiktok.com/* +// @match https://*.tiktok.com/* +// @grant GM.cookie +// @grant GM.setClipboard +// @grant GM_setClipboard +// @run-at document-idle +// ==/UserScript== + +(function () { + "use strict"; + + function mask(v) { + const s = String(v || ""); + if (s.length <= 8) return s ? "***" : "无"; + return s.slice(0, 4) + "…" + s.slice(-4); + } + + async function readCookies() { + if (!GM?.cookie?.list) { + throw new Error("当前油猴不支持 GM.cookie。请改用 Chrome 插件(能读 HttpOnly sessionid)。"); + } + const list = await GM.cookie.list({ url: location.origin + "/" }); + const map = Object.fromEntries((list || []).map((c) => [c.name, c.value])); + const sessionid = String(map.sessionid || map.sessionid_ss || "").trim(); + const dc_id = String(map["tt-target-idc"] || "").trim() || "useast2a"; + if (!sessionid) throw new Error("未找到 sessionid,请先登录 TikTok"); + return { sessionid, dc_id, tiktok_username: "" }; + } + + async function copyJson(payload) { + const text = JSON.stringify(payload); + if (typeof GM_setClipboard === "function") GM_setClipboard(text); + else if (GM?.setClipboard) await GM.setClipboard(text); + else await navigator.clipboard.writeText(text); + } + + const btn = document.createElement("button"); + btn.textContent = "导出 Cookie → dylive"; + btn.style.cssText = [ + "position:fixed", + "right:16px", + "bottom:16px", + "z-index:2147483647", + "padding:10px 14px", + "border:0", + "border-radius:999px", + "background:#fe2c55", + "color:#fff", + "font:600 13px/1 system-ui,sans-serif", + "cursor:pointer", + "box-shadow:0 8px 24px rgba(0,0,0,.35)", + ].join(";"); + btn.addEventListener("click", async () => { + try { + const payload = await readCookies(); + await copyJson(payload); + btn.textContent = "已复制 " + mask(payload.sessionid); + setTimeout(() => { + btn.textContent = "导出 Cookie → dylive"; + }, 2500); + } catch (err) { + btn.textContent = String(err.message || err); + setTimeout(() => { + btn.textContent = "导出 Cookie → dylive"; + }, 4000); + } + }); + document.documentElement.appendChild(btn); +})(); diff --git a/frontend/src/app/tiktok/page.tsx b/frontend/src/app/tiktok/page.tsx index 4ded0bf..58fb566 100644 --- a/frontend/src/app/tiktok/page.tsx +++ b/frontend/src/app/tiktok/page.tsx @@ -159,6 +159,45 @@ export default function TikTokPage() { void load(); }; + const applyCookiePayload = (raw: unknown) => { + const data = typeof raw === "string" ? JSON.parse(raw) : raw; + const sessionid = String((data as any)?.sessionid || "").trim(); + const dc_id = String((data as any)?.dc_id || (data as any)?.["tt-target-idc"] || "").trim(); + const username = String((data as any)?.tiktok_username || "").trim(); + if (!sessionid) throw new Error("剪贴板里没有 sessionid"); + setSessionId(sessionid); + setSessionDcId(dc_id || "useast2a"); + if (username) setSessionUsername(username); + }; + + const importFromClipboard = async () => { + try { + const text = await navigator.clipboard.readText(); + applyCookiePayload(text); + toast.push("已从剪贴板填入 Cookie", "ok"); + } catch (e: any) { + toast.push(e?.message || "读取剪贴板失败", "err"); + } + }; + + const importFromExtension = () => { + window.dispatchEvent(new Event("dylive-tiktok-cookie-request")); + }; + + useEffect(() => { + const onFill = (ev: Event) => { + const detail = (ev as CustomEvent).detail; + try { + applyCookiePayload(detail); + toast.push("已从插件填入 Cookie", "ok"); + } catch (e: any) { + toast.push(e?.message || "插件数据无效", "err"); + } + }; + window.addEventListener("dylive-tiktok-cookie", onFill as EventListener); + return () => window.removeEventListener("dylive-tiktok-cookie", onFill as EventListener); + }, [toast]); + const saveSession = async () => { if (!sessionChannelId) { toast.push("请选择账号", "err"); @@ -506,15 +545,29 @@ export default function TikTokPage() {

粘贴 TikTok Cookie

- 在浏览器登录 tiktok.com → F12 → Application → Cookies → 复制{" "} - sessionid 和{" "} - tt-target-idc(后者默认 useast2a)。 - sessionid 等同密码,请勿泄露。 + 推荐用 Chrome 插件或油猴导出 Cookie(无需 F12)。sessionid 等同密码,请勿泄露。

+
+ + +
  1. - 浏览器打开{" "} + 加载 Chrome 插件:extensions/tiktok-cookie-helper + (chrome://extensions → 开发者模式 → 加载已解压) +
  2. +
  3. + 登录{" "} tiktok.com - {" "} - 并登录 + + ,点插件「复制 JSON」或「填入控制台」
  4. -
  5. F12 → Application → Cookies → 复制 sessionid
  6. -
  7. 在账号卡片上点「粘贴 Cookie」保存
  8. +
  9. 本页打开「粘贴 Cookie」→「从剪贴板导入」→ 保存
)}