[英]How can I replace the result of a specific fetch?
The website uses a fetch
to get a json for its list view(by webpacked js so I can't reengineered it);该网站使用
fetch
获取 json 的列表视图(通过 webpacked js,所以我无法重新设计它);
In the tampermonkey script, I override the fetch to block it, modify its parameter and re-fetch just like this, it works fine.在 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 is the api's limit; 180是api的限制;
But I am not satisfied, because 180 is not enough so I had already to figured out a way to get a json, with same format but with 1800 results.但我并不满意,因为 180 还不够,所以我已经想办法获得 json,格式相同但结果为 1800。 There is the script:
有脚本:
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;
And I want to hijack the json with my own json which contains more results.我想用我自己的 json 来劫持 json,它包含更多的结果。
But as the fetch, it returns a Promise and it seems impossible to override such a gigantic function;但是作为获取,它返回一个 Promise 并且似乎不可能覆盖如此巨大的 function; and it seems not a ajax request so I cant not use something about
XMLHttpRequest
.而且似乎不是 ajax 请求,所以我不能不使用有关
XMLHttpRequest
的东西。
I am new to Javascript and I know very little, so any clue/tutorial/information will be very appreciated and Thank you.我是 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);
}
}
})();
referenced: Intercepting JavaScript Fetch API requests and responses参考: 拦截 JavaScript 获取 API 请求和响应
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.