-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.request.user.js
56 lines (48 loc) · 1.84 KB
/
common.request.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// ==UserScript==
// @name 常用油猴脚本方法:发起请求
// @version 0.0.7
// @author DuangXT
// @require https://raw.githubusercontent.com/DuangXT/some_monkeys_js/main/common.log.user.js
// @require https://raw.githubusercontent.com/DuangXT/some_monkeys_js/main/extend/StringExtend.js
// @require https://raw.githubusercontent.com/DuangXT/some_monkeys_js/main/utils/StringUtils.js
// @require https://raw.githubusercontent.com/DuangXT/some_monkeys_js/main/utils/RequestUtils.js
// @require https://raw.githubusercontent.com/DuangXT/some_monkeys_js/main/common.window.user.js
// @grant GM.xmlHttpRequest
// @grant GM_xmlhttpRequest
// @grant unsafeWindow
// ==/UserScript==
// common level 1
gmlog('公共库:发起请求');
/** 重写fetch */
function rewriteFetch() {
window.unsafeWindow.fetch = (url, options) => {
return originalFetch(url, options).then(async (response) => {
const responseClone = response.clone();
let res = await responseClone.json();
res.data.push('重写fetch修改数据')
return new Response(JSON.stringify(res), response);
});
};
}
/**
* @deprecated 跨域问题无法解决
*/
function xmlHttpRequest(url, callback, method='GET', referer){
referer = RequestUtils.allowRefererJson(referer && StringUtils.isString(referer) ? referer : location.href);
try {
let gmXmlHttpRequest = GM.xmlHttpRequest || GM_xmlhttpRequest;
if(gmXmlHttpRequest){
gmXmlHttpRequest({
url: url, method: method,
header: referer,
onload: function(response) {
callback(response.responseText);
}
});
return;
}
}catch (e) {
log('你报错你吗呢?', e);
}
RequestUtils.xmlHttpRequest(url, callback, method, referer);
}