hmjz100/LinkSwift · critical · Error
GreaseMonkey 兼容 XMLHttpRequest 不可用。
Error message
GreaseMonkey 兼容 XMLHttpRequest 不可用。
What it means
The userscript's base.xmlHttpRequest wrapper (网盘直链下载助手.user.js:1099) sends cross-origin requests through GreaseMonkey APIs. It looks up GM_xmlhttpRequest first, then GM.xmlHttpRequest; if neither exists as a function it throws this error because cross-domain requests cannot be performed. This is an environment capability check, thrown before any request is made.
Source
Thrown at (改)网盘直链下载助手.user.js:1099
const a = document.createElement("a");
a.href = url;
a.download = filename;
a.rel = "noopener"
a.click();
URL.revokeObjectURL(url);
}
},
/**
* 可跨域 xmlhttpRequest 请求
* @author hmjz100
* @description 封装 `GreaseMonkey-Compatible_xmlhttpRequest` 实现的跨域请求,与原始函数参数相同,支持回调和 await 两种用法
* @param {Object} option - 请求配置对象
* @returns {XMLHttpRequest|Promise} 请求对象实例或 Promise
*/
xmlHttpRequest(option) {
const xmlHttpRequest = (typeof GM_xmlhttpRequest === "function") ? GM_xmlhttpRequest : (typeof GM?.xmlHttpRequest === "function") ? GM.xmlHttpRequest : null;
if (!xmlHttpRequest || base.isType(xmlHttpRequest) !== "function") throw new Error("GreaseMonkey 兼容 XMLHttpRequest 不可用。");
return xmlHttpRequest({ withCredentials: true, ...option });;
},
/**
* 发送 POST 请求
* @author 油小猴
* @author hmjz100
* @description 一般用于请求 API,支持智能格式化数据、智能编码请求数据
* @param {String} url - 请求地址
* @param {Object|String} data - 请求数据
* @param {Object} headers - 请求头配置
* @param {String} [type="json"] - 响应类型(支持 `json`, `blob` 等)
* @param {Boolean} [withOrigin=true] - 是否携带跨域信息
* @returns {Promise} 包含响应数据的 `Promise` 对象
*/
async post(url, data, headers, type = "json", withOrigin = true) {
let _data = data;
View on GitHub (pinned to 417ea5e28a)
Solutions
- Install or switch to a userscript manager that supports GM_xmlhttpRequest / GM.xmlHttpRequest (Tampermonkey, Violentmonkey, FireMonkey).
- Verify the script's metadata header keeps `@grant GM_xmlhttpRequest` (and `@grant GM.xmlHttpRequest` if used); do not run with `@grant none`.
- Update the userscript manager to a version that implements GM.xmlHttpRequest, or check that Greasemonkey 4+ is used with the modern GM.* API.
- Update the userscript itself; older copies may lack a fallback path for your manager's API surface.
- Confirm the script is actually being executed by the manager (script enabled, matching @match/@include URL), not pasted into the page.
Example fix
// before (metadata without the grant) // @grant none // after // @grant GM_xmlhttpRequest // @grant GM.xmlHttpRequest
Defensive patterns
Strategy: type-guard
Validate before calling
if (typeof GM_xmlhttpRequest !== "function" && typeof GM?.xmlHttpRequest !== "function") {
alert("本脚本需要支持 GM_xmlhttpRequest 的脚本管理器(如 Tampermonkey/Violentmonkey)");
return;
} Type guard
function hasGmXhr() {
const xhr = (typeof GM_xmlhttpRequest === "function") ? GM_xmlhttpRequest
: (typeof GM?.xmlHttpRequest === "function") ? GM.xmlHttpRequest : null;
return typeof xhr === "function";
} Try / catch
try {
const res = await base.xmlHttpRequest(option);
} catch (e) {
if (String(e.message).includes("GreaseMonkey 兼容 XMLHttpRequest 不可用")) {
// fall back to fetch (same-origin only) or prompt user to install Tampermonkey/Violentmonkey
} else throw e;
} Prevention
- Keep `@grant GM_xmlhttpRequest` (and `@grant GM.xmlHttpRequest`) in the script header; never use `@grant none`.
- Use a manager known to implement GM.xmlHttpRequest: Tampermonkey, Violentmonkey, or Greasemonkey 4+.
- Feature-check the API once at script init and show a friendly install/setup message instead of failing mid-request.
- Always run the script via the userscript manager, never paste it into the page console.
When it happens
Trigger: Calling base.xmlHttpRequest(option) (or any network helper built on it, e.g. base.post for fetching download links) in a userscript manager or context that exposes neither the legacy global GM_xmlhttpRequest nor GM.xmlHttpRequest.
Common situations: Running the script outside a userscript manager (pasted into the page console or as a bookmarklet); using a manager without GM.xmlHttpRequest support (e.g. old Greasemonkey 4 migration gaps, userscript support in some mobile browsers); sandbox/permission config where @grant GM_xmlhttpRequest was stripped or the manager disabled it.
Related errors
AI-assisted analysis of hmjz100/LinkSwift@417ea5e28a (2026-09-02).
Data as JSON: /api/errors/f5bf85be452c1a9d.
Report an issue: GitHub.