// ==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(); 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); })();