fix: 插件正确读取并填入 tt-target-idc,去掉假默认 useast2a

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
eric 2026-08-15 03:09:01 -05:00
parent 3b1c05246c
commit 35728c405e
6 changed files with 94 additions and 29 deletions

View File

@ -43,7 +43,7 @@ function fillCookieForm(payload) {
el.dispatchEvent(new Event("change", { bubbles: true })); el.dispatchEvent(new Event("change", { bubbles: true }));
}; };
setNative(sid, payload.sessionid || ""); setNative(sid, payload.sessionid || "");
setNative(dc, payload.dc_id || ""); setNative(dc, payload.dc_id || payload["tt-target-idc"] || "");
if (user && payload.tiktok_username) setNative(user, payload.tiktok_username); if (user && payload.tiktok_username) setNative(user, payload.tiktok_username);
return { ok: true }; return { ok: true };
} }

View File

@ -1,7 +1,7 @@
{ {
"manifest_version": 3, "manifest_version": 3,
"name": "dylive Helper", "name": "dylive Helper",
"version": "2.1.1", "version": "2.1.2",
"description": "TikTok Cookie + X2 Ops(推送到 dylive /x)", "description": "TikTok Cookie + X2 Ops(推送到 dylive /x)",
"permissions": ["cookies", "storage", "tabs", "alarms", "scripting", "clipboardWrite"], "permissions": ["cookies", "storage", "tabs", "alarms", "scripting", "clipboardWrite"],
"host_permissions": [ "host_permissions": [

View File

@ -29,15 +29,23 @@ export function TikTokPanel() {
const data = (await chrome.runtime.sendMessage({ const data = (await chrome.runtime.sendMessage({
type: "GET_TIKTOK_COOKIES", type: "GET_TIKTOK_COOKIES",
})) as CookieRes; })) as CookieRes;
const realDc = String(data?.dc_id || data?.payload?.dc_id || "").trim();
setPayload(data?.ok ? data.payload || null : null); setPayload(data?.ok ? data.payload || null : null);
setSid(data?.ok ? mask(String(data.sessionid || "")) : "未登录 / 未找到"); setSid(data?.ok ? mask(String(data.sessionid || "")) : "未登录 / 未找到");
setDc(data?.dc_id || "useast2a"); setDc(realDc || "未读到");
if (data?.ok) setStatus({ kind: "ok", text: "已读到 Cookie,可复制或填入控制台" }); if (data?.ok) {
else setStatus({
kind: realDc ? "ok" : "err",
text: realDc
? "已读到 Cookie,可复制或填入控制台"
: "已读到 sessionid,但未读到 tt-target-idc(填入后网页可留空自动探测)",
});
} else {
setStatus({ setStatus({
kind: "err", kind: "err",
text: data?.error || "请先在本浏览器登录 tiktok.com,再点插件", text: data?.error || "请先在本浏览器登录 tiktok.com,再点插件",
}); });
}
}; };
useEffect(() => { useEffect(() => {

View File

@ -1399,38 +1399,80 @@ chrome.runtime.onMessage.addListener((message, _sender, sendResponse) => {
if (message?.type === "GET_TIKTOK_COOKIES") { if (message?.type === "GET_TIKTOK_COOKIES") {
void (async () => { void (async () => {
try { try {
const byName: Record<string, string> = {};
const absorb = (list: chrome.cookies.Cookie[] | null | undefined) => {
for (const c of list || []) {
if (c?.name && c.value) byName[c.name] = String(c.value);
}
};
const urls = [ const urls = [
"https://www.tiktok.com/", "https://www.tiktok.com/",
"https://www.tiktok.com", "https://www.tiktok.com",
"https://tiktok.com/", "https://tiktok.com/",
"https://www.tiktok.com/foryou", "https://www.tiktok.com/foryou",
"https://www.tiktok.com/login",
]; ];
const bags = await Promise.all( const domains = [".tiktok.com", "tiktok.com", ".www.tiktok.com", "www.tiktok.com"];
urls.map((url) => chrome.cookies.getAll({ url }).catch(() => [])),
); await Promise.all([
const byName: Record<string, string> = {}; ...urls.map((url) => chrome.cookies.getAll({ url }).then(absorb).catch(() => undefined)),
for (const c of bags.flat()) { ...domains.map((domain) =>
if (c?.name && c.value) byName[c.name] = c.value; chrome.cookies.getAll({ domain }).then(absorb).catch(() => undefined),
} ),
for (const domain of [".tiktok.com", "tiktok.com", ".www.tiktok.com", "www.tiktok.com"]) { ]);
const extra = await chrome.cookies.getAll({ domain }).catch(() => []);
for (const c of extra) { // Partitioned cookies (CHIPS) are omitted from default getAll in modern Chrome.
if (c?.name && c.value) byName[c.name] = c.value; for (const topLevelSite of ["https://www.tiktok.com", "https://tiktok.com"]) {
try {
absorb(
await chrome.cookies.getAll({
domain: ".tiktok.com",
partitionKey: { topLevelSite },
} as chrome.cookies.GetAllDetails),
);
} catch {
/* older Chrome without partitionKey */
}
try {
absorb(
await chrome.cookies.getAll({
url: `${topLevelSite}/`,
partitionKey: { topLevelSite },
} as chrome.cookies.GetAllDetails),
);
} catch {
/* ignore */
} }
} }
const sessionid = String(
byName.sessionid || byName.sessionid_ss || "", // Explicit name lookups — tt-target-idc is easy to miss via domain-only scans.
).trim(); for (const name of ["sessionid", "sessionid_ss", "tt-target-idc", "uniqueid"]) {
const dc = String( for (const url of urls) {
byName["tt-target-idc"] || byName.tt_target_idc || "", try {
).trim(); const c = await chrome.cookies.get({ url, name });
if (c?.value) byName[c.name] = String(c.value);
} catch {
/* ignore */
}
}
}
const sessionid = String(byName.sessionid || byName.sessionid_ss || "").trim();
const dc = String(byName["tt-target-idc"] || byName.tt_target_idc || "").trim();
const handle = String(byName.uniqueid || byName.uniqueId || "").trim(); const handle = String(byName.uniqueid || byName.uniqueId || "").trim();
const payload = {
sessionid,
dc_id: dc,
"tt-target-idc": dc,
tiktok_username: handle,
};
sendResponse({ sendResponse({
ok: Boolean(sessionid), ok: Boolean(sessionid),
sessionid, sessionid,
dc_id: dc, dc_id: dc,
tiktok_username: handle, tiktok_username: handle,
payload: { sessionid, dc_id: dc, tiktok_username: handle }, payload,
error: sessionid error: sessionid
? "" ? ""
: "未找到 sessionid。请用同一 Chrome 打开并登录 https://www.tiktok.com", : "未找到 sessionid。请用同一 Chrome 打开并登录 https://www.tiktok.com",

View File

@ -43,7 +43,7 @@ function fillCookieForm(payload) {
el.dispatchEvent(new Event("change", { bubbles: true })); el.dispatchEvent(new Event("change", { bubbles: true }));
}; };
setNative(sid, payload.sessionid || ""); setNative(sid, payload.sessionid || "");
setNative(dc, payload.dc_id || ""); setNative(dc, payload.dc_id || payload["tt-target-idc"] || "");
if (user && payload.tiktok_username) setNative(user, payload.tiktok_username); if (user && payload.tiktok_username) setNative(user, payload.tiktok_username);
return { ok: true }; return { ok: true };
} }

View File

@ -211,7 +211,9 @@ export default function TikTokPage() {
const applyCookiePayload = (raw: unknown, source: "clipboard" | "extension" = "clipboard") => { const applyCookiePayload = (raw: unknown, source: "clipboard" | "extension" = "clipboard") => {
const data = typeof raw === "string" ? JSON.parse(raw) : raw; const data = typeof raw === "string" ? JSON.parse(raw) : raw;
const sessionid = String((data as any)?.sessionid || "").trim(); 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 dc_id = String(
(data as any)?.dc_id || (data as any)?.["tt-target-idc"] || (data as any)?.tt_target_idc || "",
).trim();
const username = String((data as any)?.tiktok_username || "").trim(); const username = String((data as any)?.tiktok_username || "").trim();
if (!sessionid) { if (!sessionid) {
throw new Error(source === "extension" ? "插件未返回 sessionid" : "剪贴板里没有 sessionid"); throw new Error(source === "extension" ? "插件未返回 sessionid" : "剪贴板里没有 sessionid");
@ -219,13 +221,17 @@ export default function TikTokPage() {
setSessionId(sessionid); setSessionId(sessionid);
setSessionDcId(dc_id); setSessionDcId(dc_id);
if (username) setSessionUsername(username); if (username) setSessionUsername(username);
return { sessionid, dc_id, username };
}; };
const importFromClipboard = async () => { const importFromClipboard = async () => {
try { try {
const text = await navigator.clipboard.readText(); const text = await navigator.clipboard.readText();
applyCookiePayload(text, "clipboard"); const filled = applyCookiePayload(text, "clipboard");
toast.push("已从剪贴板填入 Cookie", "ok"); toast.push(
filled.dc_id ? `已从剪贴板填入(机房 ${filled.dc_id})` : "已从剪贴板填入 Cookie(未含机房)",
"ok",
);
} catch (e: any) { } catch (e: any) {
toast.push(e?.message || "读取剪贴板失败", "err"); toast.push(e?.message || "读取剪贴板失败", "err");
} }
@ -245,6 +251,7 @@ export default function TikTokPage() {
}; };
useEffect(() => { useEffect(() => {
let lastKey = "";
const applyFromExt = (detail: unknown, error?: string) => { const applyFromExt = (detail: unknown, error?: string) => {
(window as any).__dyliveTiktokCookieGot = true; (window as any).__dyliveTiktokCookieGot = true;
if (error) { if (error) {
@ -252,8 +259,16 @@ export default function TikTokPage() {
return; return;
} }
try { try {
applyCookiePayload(detail, "extension"); const filled = applyCookiePayload(detail, "extension");
toast.push("已从插件填入 Cookie", "ok"); const key = `${filled.sessionid}|${filled.dc_id}`;
if (key === lastKey) return; // postMessage + CustomEvent 各推一次
lastKey = key;
toast.push(
filled.dc_id
? `已从插件填入(机房 ${filled.dc_id})`
: "已从插件填入 sessionid;tt-target-idc 未读到,可手动填或留空自动探测",
filled.dc_id ? "ok" : "warn",
);
} catch (e: any) { } catch (e: any) {
toast.push(e?.message || "插件数据无效", "err"); toast.push(e?.message || "插件数据无效", "err");
} }