El sitio web usa una fetch para obtener un json para su vista de lista (por js empaquetado en la web, por lo que no puedo rediseñarlo);
En el script de tampermonkey, anulo la búsqueda para bloquearlo, modifico su parámetro y vuelvo a buscar así, funciona bien.
// ==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 es el límite de la API;
Pero no estoy satisfecho, porque 180 no es suficiente, así que ya tenía que encontrar una manera de obtener un json, con el mismo formato pero con resultados de 1800. Ahí está el guión:
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;Y quiero secuestrar el json con mi propio json que contiene más resultados.
Pero como la búsqueda, devuelve una Promesa y parece imposible anular una función tan gigantesca; y parece que no es una solicitud de ajax, por lo que no puedo usar algo sobre XMLHttpRequest .
Soy nuevo en Javascript y sé muy poco, por lo que cualquier pista/tutorial/información será muy apreciada y gracias.
(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); } } })();referenciado: interceptar solicitudes y respuestas de JavaScript Fetch API