虎牙人工兑换改用本机浏览器并新增扩展自动注入登录态

移除服务器浏览器方案(容器/Xvfb/noVNC/CDP 会话管理),后端仅下发
兑换材料;新增内置 Chrome/Edge 扩展一键写入 huya.com 登录态,
未安装扩展时回退控制台脚本手动注入,兑换始终由用户手动完成。
This commit is contained in:
yml2213
2026-09-05 17:55:31 +08:00
parent 95ad900ef0
commit 230d6c1918
15 changed files with 477 additions and 4 deletions
+25
View File
@@ -2,6 +2,7 @@
from __future__ import annotations
import json
from collections.abc import Iterable, Mapping
from requests.cookies import RequestsCookieJar
@@ -67,3 +68,27 @@ def cookie_value(
if item_key == key:
value = "" if item_value is None else str(item_value).strip()
return value
def browser_login_script(
pairs: Mapping[str, str],
target_url: str,
*,
domain: str = ".huya.com",
ttl_days: int = 180,
) -> str:
"""生成在用户自己浏览器控制台执行的登录脚本。
脚本把 Cookie 写入指定域后跳转目标页;Cookie 值以 JSON 数组内联,
保证引号、反斜杠等字符安全转义。
"""
payload = json.dumps([[key, value] for key, value in pairs.items()], ensure_ascii=False)
target = json.dumps(target_url, ensure_ascii=False)
ttl_ms = max(1, ttl_days) * 86_400_000
return (
f"(function(){{var c={payload};"
f"var e=new Date(Date.now()+{ttl_ms}).toUTCString();"
f"for(var i=0;i<c.length;i++){{"
f"document.cookie=c[i][0]+'='+c[i][1]+';domain={domain};path=/;expires='+e;"
f"}}location.href={target};}})();"
)