fix(pugongying): 使用 GM_xmlhttpRequest 解决跨域请求 CORS 问题

对代理 API 请求改用 GM_xmlhttpRequest 绕过浏览器 CORS 限制,
蒲公英官方 API 仍使用原生 fetch(同源无需跨域)。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
wxs 2026-03-13 21:04:53 +08:00
parent 1c21be5e77
commit 465e1f7746

View File

@ -4,7 +4,8 @@
// @version 0.1.1
// @description 输入达人主页链接或达人 ID勾选字段后导出 Excel
// @match https://pgy.xiaohongshu.com/*
// @grant none
// @grant GM_xmlhttpRequest
// @connect api.internal.intelligrow.cn
// @require https://cdn.jsdelivr.net/npm/xlsx@0.18.5/dist/xlsx.full.min.js
// ==/UserScript==
@ -14,6 +15,27 @@
module.exports = api;
}
})(typeof globalThis !== "undefined" ? globalThis : this, function factory(root) {
function gmFetch(url, options) {
return new Promise((resolve, reject) => {
const headers = options && options.headers ? options.headers : {};
GM_xmlhttpRequest({
method: (options && options.method) || "GET",
url,
headers,
onload(res) {
resolve({
ok: res.status >= 200 && res.status < 300,
status: res.status,
json: () => Promise.resolve(JSON.parse(res.responseText)),
});
},
onerror(err) {
reject(new Error("GM_xmlhttpRequest 网络错误: " + (err.statusText || url)));
},
});
});
}
const API_BASE =
"https://pgy.xiaohongshu.com/api/solar/cooperator/user/blogger/";
const PROXY_API_BASE = "https://api.internal.intelligrow.cn";
@ -395,7 +417,9 @@
async function fetchSupplementalPayload(userId, fetchImpl, config) {
const extra =
typeof config.extraHeaders === "function" ? config.extraHeaders() : {};
const response = await fetchImpl(config.buildUrl(userId), {
const hasExtra = Object.keys(extra).length > 0;
const fetcher = hasExtra && typeof GM_xmlhttpRequest === "function" ? gmFetch : fetchImpl;
const response = await fetcher(config.buildUrl(userId), {
method: "GET",
credentials: "include",
headers: {