[英]How can I replace the result of a specific fetch?
该网站使用fetch
获取 json 的列表视图(通过 webpacked js,所以我无法重新设计它);
在 Tampermonkey 脚本中,我覆盖了 fetch 以阻止它,修改它的参数并重新获取,就像这样,它工作正常。
// ==UserScript==
// @name not important
// @version 0.23
// @namespace none
// @match https://www.aaaaaaaaaaaaaa.net/*
// ==/UserScript==
(function () {
unsafeWindow.fetch = (...arg) => {
if (arg[0].indexOf('limit=18') > -1) {
arg[0] = arg[0].replace('limit=18', 'limit=180');
return fetch(...arg);
} else {
return fetch(...arg);
}
}
})();
180是api的限制;
但我并不满意,因为 180 还不够,所以我已经想办法获得 json,格式相同但结果为 1800。 有脚本:
var json = (await (await fetch('https://www.abcdefg.net/ajax/illust/123456789/rec/init?limit=1&lang=zh')).json());
var jsonBody =json.body;
var array = [jsonBody.illusts[0].id];
var array2 = jsonBody.nextIds;
var combination = array.concat(array2);
jsonBody.nextIds = [];
jsonBody.illusts= [];
var illustComb = [];
for(let i = 0; i<5; i++)
{
var jsonBody = (await (await fetch('https://www.abcdefg.net/ajax/illust/'+ combination[i]+'/rec/init?limit=1&lang=zh')).json()).body;
illustComb= illustComb.concat(jsonBody.illusts);
}
var filtered = [];
for(let i = 0; i<illustComb.length; i++)
{
if(! illustComb[i].isAdContainer == true)
filtered.push(illustComb[i]);
}
jsonBody.illusts=filtered;
json.body = jsonBody;
return json;
我想用我自己的 json 来劫持 json,它包含更多的结果。
但是作为获取,它返回一个 Promise 并且似乎不可能覆盖如此巨大的 function; 而且似乎不是 ajax 请求,所以我不能不使用有关XMLHttpRequest
的东西。
我是 Javascript 的新手,我知道的很少,所以任何线索/教程/信息将不胜感激,谢谢。
(function () {
unsafeWindow.fetch = async (...arg) => {
console.log('fetch arg', ...arg); if (arg[0].indexOf('limit=18') > -1) {
let illust_id = arg[0].match(/(?<=\/illust\/)\d+/)[0];
arg[0] = arg[0].replace('limit=18', 'limit=180');
var origjs = (await (await fetch('https://www.abcdefg.net/ajax/illust/123456789/rec/init?limit=1&lang=zh')).json());
//omitted, refered to the question.
origjs.body = jsonBody;
let [resource, config] = arg;
let response = await fetch(resource, config); const json = () =>
response
.clone()
.json()
.then((data) => (data= origjs));
response.json = json;
return response;
}
else {
return fetch(...arg);
}
}
})();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.