您需要先安装一个扩展,例如 篡改猴、Greasemonkey 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 Userscripts ,之后才能安装此脚本。
您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。
您需要先安装用户脚本管理器扩展后才能安装此脚本。
Imitating Python's Requests library based on GM_xmlHttpRequest.
当前为
此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.greasyfork.icu/scripts/470000/1214468/GM%20Requests.js
Based on GM_xmlHttpRequest
, GM Requests is a library that emulates Python's Requests library.
@require
tag in a script// ==UserScript==
// @name My Tampermonkey Script
// @description Example script using the library
// @require https://your-domain.com/path-to-your-library/my-library.js
// ==/UserScript==
requests.get('https://github.com');
import
in local codeFirst, install GM Requests:
npm install https://github.com/bigbowl-wtw/GM-Requests.git
Import in your code:
import requests from 'gm-requests';
requests.get('https://github.com');
requests.get
let ret = await requests.get(
'https://httpbin.org/get',
{ foo: 'bar' },
{ responseType: 'json' }
);
requests.get<TResolve = any, TContext = object>(
url: string | URL,
query?: Query,
options?: Options<TContext>
): Promise<TResolve>
url
: The URL of the request.
query
: Query parameters to be sent.
options
: Parameters passed to GM_xmlHttpRequest
, excluding url
, method
, headers
, and cookies
.
requests.post
let ret = await requests.post(
'https://httpbin.org/get',
{
data: { foo: 'bar' },
responseType: 'json'
}
);
requests.post<TResolve = any, TContext = object>(
url: string | URL,
options?: Options<TContext>
): Promise<TResolve>
url
: The URL of the request.
options
:
json?: any
: An object that can be converted to a JSON string.data: { [key: string]: string }
: Data sent with 'application/x-www-form-urlencoded'
encoding.GM_xmlHttpRequest
, excluding url
, method
, headers
, and cookies
.json
and data
only have an effect in post
requests.
Similar to requests, requests.get
and requests.post
can send requests with specified headers
or cookies
by including them in the options
.
cookies
:requests.get('https://httpbin/get', { cookies: { foo: 'bar' } });
The above usage will generate the following cookie:
Cookie: foo=bar;
cookies
is an object with string values, where the keys are the cookie names and the values are the cookie values:
type ICookieSet = {
[name: string]: string;
};
Cookies: ICookieSet
The same cookie cannot have multiple values.
All cookies set via cookies
will be appended after the cookies managed by the browser, as determined by GM_xmlHttpRequest
.
headers
:requests.get('https://httpbin/get', { cookies: { foo: 'bar' } });
The above usage will generate the following header:
foo: bar
headers
is an object with string or string[] values, where the keys are the header names and the values are the header values. When the value is a string[], it represents multiple values for the header, and they will be separated by commas when sending the request:
headers: {
[header: string]: string | string[];
} & {
cookie?: {
[name: string]: string;
};
}
headers.cookie
and cookies
According to the behavior of GM_xmlHttpRequest
, the priority is headers.cookie
>
cookies
, and this library follows the same behavior.
Session
Similar to requests, Session
is used to maintain custom cookies across requests. However, cookies set in the server response via the Set-Cookie
header will be managed by the browser, and Session
will not handle them. It will mark and delete them, and if the same named cookie is passed again in future requests, Session
will ignore them.
let session = new requests.Session();
session.headers = { foo: 'bar' };
// header will be overwritten as { foo: 'com.github.bigbowl-wtw/gm-requests' }
session.headers.update({ foo: 'com.github.bigbowl-wtw/gm-requests' });
// header will be updated to { foo: [ 'com.github.bigbowl-wtw/gm-requests', 'bar' ]}
session.headers.append('foo', 'bar');
session.cookies = { test: 'A' };
session.cookie.update({ test: 'B' });
When headers contain cookies, Session.cookies
will be updated (not Session.headers.cookie
).
requests.session
requests.session
returns a Session
instance (equivalent to requests).
let session: requests.Session = requests.session();