简体   繁体   中英

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);

In the tampermonkey script, I override the fetch to block it, modify its parameter and re-fetch just like this, it works fine.

// ==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;

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. 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.

But as the fetch, it returns a Promise and it seems impossible to override such a gigantic function; and it seems not a ajax request so I cant not use something about XMLHttpRequest .

I am new to Javascript and I know very little, so any clue/tutorial/information will be very appreciated and Thank you.

(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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM