first commit
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
// content.js — 在 xueqiu.com 页面里跑,document.cookie 读非 HttpOnly 的 cookie,
|
||||
// 然后缓存起来等 popup 来取。
|
||||
// 注意:document.cookie 读不到 HttpOnly(xq_a_token 是 HttpOnly 拿不到)
|
||||
// 但能拿到 u、device_id 等普通 cookie,作为辅助数据。
|
||||
|
||||
(function () {
|
||||
const PAGE_CACHE_KEY = "__xueqiu_sync_page_cache__";
|
||||
const TTL_MS = 30_000;
|
||||
|
||||
function read() {
|
||||
const out = { url: location.href, cookies: {}, ts: Date.now() };
|
||||
try {
|
||||
const pairs = document.cookie.split(";");
|
||||
for (const p of pairs) {
|
||||
const idx = p.indexOf("=");
|
||||
if (idx > 0) {
|
||||
const k = p.slice(0, idx).trim();
|
||||
const v = decodeURIComponent(p.slice(idx + 1).trim());
|
||||
if (k) out.cookies[k] = v;
|
||||
}
|
||||
}
|
||||
} catch (_) {}
|
||||
return out;
|
||||
}
|
||||
|
||||
function update() {
|
||||
try {
|
||||
window[PAGE_CACHE_KEY] = read();
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
update();
|
||||
// 兜底:每 5s 刷一次,捕获导航/SPA 切换
|
||||
setInterval(update, 5000);
|
||||
|
||||
// 响应 popup 的查询
|
||||
chrome.runtime.onMessage.addListener((msg, _sender, sendResponse) => {
|
||||
if (msg && msg.type === "xueqiu_sync/get_page_cookies") {
|
||||
let cache = window[PAGE_CACHE_KEY];
|
||||
if (!cache || Date.now() - cache.ts > TTL_MS) {
|
||||
cache = read();
|
||||
window[PAGE_CACHE_KEY] = cache;
|
||||
}
|
||||
sendResponse(cache);
|
||||
return true;
|
||||
}
|
||||
});
|
||||
})();
|
||||
Reference in New Issue
Block a user