您需要先安装一个扩展,例如 篡改猴、Greasemonkey 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 Userscripts ,之后才能安装此脚本。
您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。
您需要先安装用户脚本管理器扩展后才能安装此脚本。
Imitating Python's Requests library based on GM_xmlHttpRequest.
当前为
此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/470000/1214468/GM%20Requests.js
// ==UserScript== // @name GM Requests // @description Imitating Python's Requests library based on GM_xmlHttpRequest. // @version 0.0.1 // @author 大碗宽面wtw // @homepage https://github.com/bigbowl-wtw/gm-requests/ // @supportURL https://github.com/bigbowl-wtw/gm-requests/issues // @match *://*/* // @grant GM_xmlhttpRequest // @license MIT // @namespace com.github.bigbowl-wtw // ==/UserScript== (function webpackUniversalModuleDefinition(root, factory) { if (typeof exports === "object" && typeof module === "object") module.exports = factory(); else if (typeof define === "function" && define.amd) define([], factory); else if (typeof exports === "object") exports["requests"] = factory(); else root["requests"] = factory(); })(self, (() => (() => { "use strict"; var __webpack_require__ = {}; (() => { __webpack_require__.d = (exports, definition) => { for (var key in definition) { if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) { Object.defineProperty(exports, key, { enumerable: true, get: definition[key] }); } } }; })(); (() => { __webpack_require__.o = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop); })(); (() => { __webpack_require__.r = exports => { if (typeof Symbol !== "undefined" && Symbol.toStringTag) { Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); } Object.defineProperty(exports, "__esModule", { value: true }); }; })(); var __webpack_exports__ = {}; __webpack_require__.r(__webpack_exports__); __webpack_require__.d(__webpack_exports__, { Session: () => session, get: () => get, post: () => post, session: () => src_session }); var ErrorStatus; (function(ErrorStatus) { ErrorStatus[ErrorStatus["BadRequest"] = 400] = "BadRequest"; ErrorStatus[ErrorStatus["Unauthorized"] = 401] = "Unauthorized"; ErrorStatus[ErrorStatus["Forbidden"] = 403] = "Forbidden"; ErrorStatus[ErrorStatus["NotFound"] = 404] = "NotFound"; ErrorStatus[ErrorStatus["MethodNotAllowed"] = 405] = "MethodNotAllowed"; ErrorStatus[ErrorStatus["RequestTimeout"] = 408] = "RequestTimeout"; ErrorStatus[ErrorStatus["TooManyRequests"] = 429] = "TooManyRequests"; ErrorStatus[ErrorStatus["InternalServerError"] = 500] = "InternalServerError"; ErrorStatus[ErrorStatus["BadGateway"] = 502] = "BadGateway"; ErrorStatus[ErrorStatus["ServiceUnavailable"] = 503] = "ServiceUnavailable"; })(ErrorStatus || (ErrorStatus = {})); var __spreadArray = undefined && undefined.__spreadArray || function(to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; function utils_assign(target) { var rest = []; for (var _i = 1; _i < arguments.length; _i++) { rest[_i - 1] = arguments[_i]; } var __assign = Object.assign || function(t) { var others = []; for (var _i = 1; _i < arguments.length; _i++) { others[_i - 1] = arguments[_i]; } for (var s = void 0, i = 0, n = others.length; i < n; i++) { s = others[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.call.apply(__assign, __spreadArray([ this, target ], rest, false)); } function assignDeepCopy(target) { var rest = []; for (var _i = 1; _i < arguments.length; _i++) { rest[_i - 1] = arguments[_i]; } rest = rest.filter((function(r) { return r !== undefined; })); utils_assign.apply(void 0, __spreadArray([ target ], rest, false)); return utils_assign.call.apply(utils_assign, __spreadArray([ this, target ], JSON.parse(JSON.stringify(rest)), false)); } function bodyToString(data) { if (Object.values(data).some((function(v) { return !(typeof v in [ "string", "number" ]); }))) throw new TypeError("value must be `string` or `number`"); return Object.entries(data).map((function(_a) { var key = _a[0], value = _a[1]; return "".concat(key, "=").concat(encodeURIComponent(value)); })).join("&"); } var SimpleCookieJar = function() { function SimpleCookieJar(cookies) { var _a; this.never = new Set; this.setCookies((_a = cookies) !== null && _a !== void 0 ? _a : {}); } Object.defineProperty(SimpleCookieJar.prototype, "empty", { get: function() { return !Object.keys(this.cookies).length; }, enumerable: false, configurable: true }); SimpleCookieJar.prototype.update = function(cookies) { utils_assign(this.cookies, this.normalizeCookie(cookies)); }; SimpleCookieJar.prototype.updateFromString = function(cookieStringArray) { if (typeof cookieStringArray === "string") cookieStringArray = [ cookieStringArray ]; this.update(cookieStringArray); }; SimpleCookieJar.prototype.differenceUpdate = function(cookies) { var cookieEntries; if (Array.isArray(cookies)) cookieEntries = cookies.map(this.stringToEntry); else cookieEntries = Object.entries(isCookieJar(cookies) ? cookies.cookies : cookies); var set = new Set(Object.keys(this.cookies)); cookieEntries = cookieEntries.filter((function(_a) { var name = _a[0]; return !set.has(name); })); this.update(Object.fromEntries(cookieEntries)); }; SimpleCookieJar.prototype.deleteFromString = function(cookieStringArray) { var _this = this; if (typeof cookieStringArray === "string") cookieStringArray = [ cookieStringArray ]; var names = cookieStringArray.map(this.stringToEntry).map((function(_a) { var name = _a[0]; return name; })); names.forEach((function(name) { _this.never.add(name); delete _this.cookies[name]; })); }; SimpleCookieJar.prototype.setCookies = function(cookies) { var _this = this; this.cookies = new Proxy(this.normalizeCookie(cookies), { set: function(target, name, value) { if (_this.never.has(name)) return true; target[name] = value; return true; } }); }; SimpleCookieJar.prototype.parseCookieString = function(cookieStrings) { return Object.fromEntries(cookieStrings.map(this.stringToEntry)); }; SimpleCookieJar.prototype.normalizeCookie = function(cookies) { if (Array.isArray(cookies)) return this.parseCookieString(cookies); if (isCookieJar(cookies)) return cookies.cookies; return cookies; }; SimpleCookieJar.prototype.stringToEntry = function(cookieString) { return cookieString.slice(0, cookieString.indexOf(";")).split("="); }; return SimpleCookieJar; }(); function isCookieJar(obj) { return typeof obj.update === "function"; } var __assign = undefined && undefined.__assign || function() { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __rest = undefined && undefined.__rest || function(s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var Header = function() { function Header(headers) { this.cookie = new SimpleCookieJar; if (!headers) return; var cookie = headers.cookie, others = __rest(headers, [ "cookie" ]); this.headers = others; if (cookie) { this.cookie.update(cookie); } } Header.prototype.setFromString = function(headerString) { var entrise = headerString.split("\r\n").map((function(kv) { return kv.split(":"); })).map((function(_a) { var k = _a[0], v = _a[1]; return [ k.toLowerCase(), v.trim() ]; })); for (var _i = 0, entrise_1 = entrise; _i < entrise_1.length; _i++) { var _a = entrise_1[_i], name_1 = _a[0], value = _a[1]; var header = this.headers[name_1]; if (header) { if (typeof header === "string") this.headers[name_1] = [ header ]; this.headers[name_1].push(value); } else { this.headers[name_1] = value; } } return this; }; Header.prototype.update = function(headers) { var cookie = headers.cookie, others = __rest(headers, [ "cookie" ]); utils_assign(this.headers, others); if (cookie) { this.cookie.update(cookie); } }; Header.prototype.getHeaders = function() { if (!Object.keys(this.headers).length) { if (this.cookie.empty) return {}; return { cookie: this.cookie.toString() }; } var headers = {}; for (var _i = 0, _a = Object.entries(this.headers); _i < _a.length; _i++) { var _b = _a[_i], name_2 = _b[0], value = _b[1]; headers[name_2] = value.toString(); } return __assign(__assign({}, headers), { cookie: this.cookie.toString() }); }; Header.prototype.append = function(header, value) { if (header === "cookie") this.cookie.update([ header ]); else if (!this[header]) this[header] = value; else if (!Array.isArray(header)) this[header] = [ value ].concat(this[header]); else this[header].concat(value); }; Header.prototype.get = function(name) { return this.headers[name]; }; return Header; }(); var session_assign = undefined && undefined.__assign || function() { session_assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return session_assign.apply(this, arguments); }; var __awaiter = undefined && undefined.__awaiter || function(thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P((function(resolve) { resolve(value); })); } return new (P || (P = Promise))((function(resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); })); }; var __generator = undefined && undefined.__generator || function(thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), throw: verb(1), return: verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function(v) { return step([ n, v ]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [ op[0] & 2, t.value ]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [ 0 ]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [ 6, e ]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var session_rest = undefined && undefined.__rest || function(s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var Details = function() { function Details(url, method, options) { this.url = url; this.method = method; this.finalHeader = new Header; if (options) { var query = options.query, json = options.json, data = options.data, cookie = options.cookie, auth = options.auth, headers = options.headers, others = session_rest(options, [ "query", "json", "data", "cookie", "auth", "headers" ]); this.query = query; this.json = json; this.data = data; this.cookie = cookie; this.auth = auth; this.headers = headers; this.others = others; } } Details.prototype.build = function(session) { return __awaiter(this, void 0, void 0, (function() { var _this = this; return __generator(this, (function(_a) { this.buildURL().buildHeaderAndCookie(session).buildAuth(session); this.pendings.concat(session.buildHooks.map((function(hook) { return hook.call(_this, session); }))); return [ 2, this.toDetails() ]; })); })); }; Details.prototype.buildURL = function() { this.parseQuery(this.query); return this; }; Details.prototype.buildHeaderAndCookie = function(session) { var finalCookie = this.finalHeader.cookie; if (session.cookies) finalCookie.update(session.cookies); if (this.cookie) finalCookie.update(this.cookie); this.finalHeader.update(session.headers); if (this.headers) this.finalHeader.update(this.headers); return this; }; Details.prototype.buildAuth = function(session) { var _a; this.pendings.push((_a = session.auth) === null || _a === void 0 ? void 0 : _a.build(this.finalHeader)); return this; }; Details.prototype.buildBody = function() { if (this.json) { var contentType = "application/json"; this.headers["Content-Type"] = contentType; var data = JSON.stringify(this.json); this.finalData = data; return this; } if (this.data) { if (this.data instanceof FormData) return this; var contentType = "application/x-www-form-urlencoded"; this.headers["Content-Type"] = contentType; this.finalData = bodyToString(this.data); return this; } return this; }; Details.prototype.toDetails = function() { return __awaiter(this, void 0, void 0, (function() { var url, details; return __generator(this, (function(_a) { switch (_a.label) { case 0: return [ 4, Promise.all(this.pendings) ]; case 1: _a.sent(); url = this.url.toString(); this.buildBody(); details = session_assign({ url, method: this.method, headers: this.finalHeader.getHeaders() }, this.others); if (this.finalData) details.data = this.finalData; return [ 2, details ]; } })); })); }; Details.prototype.parseQuery = function(query) { var url; if (typeof this.url === "string") url = new URL(this.url); else url = this.url; if (query) Object.entries(query).filter((function(_a) { var _ = _a[0], v = _a[1]; return typeof v === "string"; })).forEach((function(_a) { var k = _a[0], v = _a[1]; return url.searchParams.append(k, v.toString()); })); this.url = url; }; return Details; }(); var Session = function() { function Session() { var _this = this; this.buildHooks = []; var headers = new Header; Object.defineProperty(this, "headers", { get: function() { return headers; }, set: function(value) { headers.cookie.setCookies(value); } }); Object.defineProperty(this, "cookies", { get: function() { return _this.headers.cookie; }, set: function(value) { this.headers.cookie.setCookies(value); } }); } Session.prototype.get = function(url, options) { return __awaiter(this, void 0, void 0, (function() { return __generator(this, (function(_a) { return [ 2, this.request("GET", url, options) ]; })); })); }; Session.prototype.post = function(url, options) { return __awaiter(this, void 0, void 0, (function() { return __generator(this, (function(_a) { return [ 2, this.request("POST", url, options) ]; })); })); }; Session.prototype.request = function(method, url, options) { return __awaiter(this, void 0, void 0, (function() { var details; var _this = this; return __generator(this, (function(_a) { details = new Details(url, method, options); return [ 2, new Promise((function(resolve, reject) { if (!options.onload) { details.others.onload = function(resp) { if (!_this.cookies.empty) { var respHeaders = (new Header).setFromString(resp.responseHeaders); _this.cookies.deleteFromString(respHeaders["set-cookie"]); } if (resp.status === 200 && resp.status in ErrorStatus) resolve((options === null || options === void 0 ? void 0 : options.responseType) ? resp.response : resp); else reject(resp); }; } if (!options.onerror) details.others.onerror = function(resp) { return reject(resp); }; details.build(_this).then((function(dtl) { return GM_xmlhttpRequest(dtl); })); })) ]; })); })); }; Session.prototype.registerBuildHook = function(hook) { this.buildHooks.push(hook); }; return Session; }(); const session = Session; var src_assign = undefined && undefined.__assign || function() { src_assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return src_assign.apply(this, arguments); }; function get(url, query, options) { return (new session).get(url, src_assign({ query }, options)); } function post(url, options) { return (new session).post(url, src_assign({}, options)); } function src_session() { return new session; } return __webpack_exports__; })()));