{"record":{"id":"f5bf85be452c1a9d","repo":"hmjz100/LinkSwift","slug":"greasemonkey-xmlhttprequest","errorCode":null,"errorMessage":"GreaseMonkey 兼容 XMLHttpRequest 不可用。","messagePattern":"GreaseMonkey 兼容 XMLHttpRequest 不可用。","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"critical","filePath":"（改）网盘直链下载助手.user.js","lineNumber":1099,"sourceCode":"\t\t\t\tconst a = document.createElement(\"a\");\r\n\t\t\t\ta.href = url;\r\n\t\t\t\ta.download = filename;\r\n\t\t\t\ta.rel = \"noopener\"\r\n\t\t\t\ta.click();\r\n\t\t\t\tURL.revokeObjectURL(url);\r\n\t\t\t}\r\n\t\t},\r\n\r\n\t\t/**\r\n\t\t * 可跨域 xmlhttpRequest 请求\r\n\t\t * @author hmjz100\r\n\t\t * @description 封装 `GreaseMonkey-Compatible_xmlhttpRequest` 实现的跨域请求，与原始函数参数相同，支持回调和 await 两种用法\r\n\t\t * @param {Object} option - 请求配置对象\r\n\t\t * @returns {XMLHttpRequest|Promise} 请求对象实例或 Promise\r\n\t\t */\r\n\t\txmlHttpRequest(option) {\r\n\t\t\tconst xmlHttpRequest = (typeof GM_xmlhttpRequest === \"function\") ? GM_xmlhttpRequest : (typeof GM?.xmlHttpRequest === \"function\") ? GM.xmlHttpRequest : null;\r\n\t\t\tif (!xmlHttpRequest || base.isType(xmlHttpRequest) !== \"function\") throw new Error(\"GreaseMonkey 兼容 XMLHttpRequest 不可用。\");\r\n\r\n\t\t\treturn xmlHttpRequest({ withCredentials: true, ...option });;\r\n\t\t},\r\n\r\n\t\t/**\r\n\t\t * 发送 POST 请求\r\n\t\t * @author 油小猴\r\n\t\t * @author hmjz100\r\n\t\t * @description 一般用于请求 API，支持智能格式化数据、智能编码请求数据\r\n\t\t * @param {String} url - 请求地址\r\n\t\t * @param {Object|String} data - 请求数据\r\n\t\t * @param {Object} headers - 请求头配置\r\n\t\t * @param {String} [type=\"json\"] - 响应类型（支持 `json`, `blob` 等）\r\n\t\t * @param {Boolean} [withOrigin=true] - 是否携带跨域信息\r\n\t\t * @returns {Promise} 包含响应数据的 `Promise` 对象\r\n\t\t */\r\n\t\tasync post(url, data, headers, type = \"json\", withOrigin = true) {\r\n\t\t\tlet _data = data;\r","sourceCodeStart":1081,"sourceCodeEnd":1117,"githubUrl":"https://github.com/hmjz100/LinkSwift/blob/417ea5e28a3e1a90339710e17356e9fd22b8a34b/（改）网盘直链下载助手.user.js#L1081-L1117","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before (metadata without the grant)\n// @grant none\n\n// after\n// @grant GM_xmlhttpRequest\n// @grant GM.xmlHttpRequest","handlingStrategy":"type-guard","validationCode":"if (typeof GM_xmlhttpRequest !== \"function\" && typeof GM?.xmlHttpRequest !== \"function\") {\n  alert(\"本脚本需要支持 GM_xmlhttpRequest 的脚本管理器（如 Tampermonkey/Violentmonkey）\");\n  return;\n}","typeGuard":"function hasGmXhr() {\n  const xhr = (typeof GM_xmlhttpRequest === \"function\") ? GM_xmlhttpRequest\n            : (typeof GM?.xmlHttpRequest === \"function\") ? GM.xmlHttpRequest : null;\n  return typeof xhr === \"function\";\n}","tryCatchPattern":"try {\n  const res = await base.xmlHttpRequest(option);\n} catch (e) {\n  if (String(e.message).includes(\"GreaseMonkey 兼容 XMLHttpRequest 不可用\")) {\n    // fall back to fetch (same-origin only) or prompt user to install Tampermonkey/Violentmonkey\n  } else throw e;\n}","preventionTips":["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."],"tags":["userscript","greasemonkey","environment","cross-origin"],"backgroundTag":"missing-gm-api","analyzedSha":"417ea5e28a3e1a90339710e17356e9fd22b8a34b","analyzedAt":"2026-09-02T18:13:32.837Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T21:17:11.164Z"}