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 }));
};
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);
return { ok: true };
}

View File

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

View File

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

View File

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

View File

@ -43,7 +43,7 @@ function fillCookieForm(payload) {
el.dispatchEvent(new Event("change", { bubbles: true }));
};
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);
return { ok: true };
}

View File

@ -211,7 +211,9 @@ export default function TikTokPage() {
const applyCookiePayload = (raw: unknown, source: "clipboard" | "extension" = "clipboard") => {
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 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();
if (!sessionid) {
throw new Error(source === "extension" ? "插件未返回 sessionid" : "剪贴板里没有 sessionid");
@ -219,13 +221,17 @@ export default function TikTokPage() {
setSessionId(sessionid);
setSessionDcId(dc_id);
if (username) setSessionUsername(username);
return { sessionid, dc_id, username };
};
const importFromClipboard = async () => {
try {
const text = await navigator.clipboard.readText();
applyCookiePayload(text, "clipboard");
toast.push("已从剪贴板填入 Cookie", "ok");
const filled = applyCookiePayload(text, "clipboard");
toast.push(
filled.dc_id ? `已从剪贴板填入(机房 ${filled.dc_id})` : "已从剪贴板填入 Cookie(未含机房)",
"ok",
);
} catch (e: any) {
toast.push(e?.message || "读取剪贴板失败", "err");
}
@ -245,6 +251,7 @@ export default function TikTokPage() {
};
useEffect(() => {
let lastKey = "";
const applyFromExt = (detail: unknown, error?: string) => {
(window as any).__dyliveTiktokCookieGot = true;
if (error) {
@ -252,8 +259,16 @@ export default function TikTokPage() {
return;
}
try {
applyCookiePayload(detail, "extension");
toast.push("已从插件填入 Cookie", "ok");
const filled = applyCookiePayload(detail, "extension");
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) {
toast.push(e?.message || "插件数据无效", "err");
}