0

I am creating a chrome extension for my website for people to be able to get partial data and complete it with their browser extension (very good for privacy), no need to implement the logic in the backend (having access to their data). I want it to work like this (with example):

  1. User requests Animal (that particular user likes cats, but I don't know that due to privacy, all I know is that I will send an animal). Think of it as purely the string "Animal" in a JSON that would look like "userWants: 'Animal'"

  2. Browser receives Animal and knows the Animal this user likes is a Cat. Changes the "userWants" property's content from "Animal" to "Cat".

  3. Displays "Cat" in the browser instead of the default "Animal".

I have tried the following (as the main JS file for my extension) PLEASE IGNORE IT AS IT IS NONSENSE:

console.log("Im here!");
function modifyJSON(json) {
// Modify the JSON object here as needed
// Example: json.title = "Modified Title";
json.userWants = "Cat";
return json;
}

const originalFetch = window.fetch;
window.fetch = async function (...args) {
const response = await originalFetch.apply(this, args);

    if (response.url.endsWith("/fetch-json")) {
        const clonedResponse = response.clone();
        const json = await response.json();
        const modifiedJSON = modifyJSON(json);
    
        return new Response(JSON.stringify(modifiedJSON), {
            status: clonedResponse.status,
            statusText: clonedResponse.statusText,
            headers: clonedResponse.headers,
        });
    } else {
        return response;
    }

};

What I would like to know (my question):

I am looking for information on the technologies/APIs/approaches for me to intercept JSON payloads and modify their content before their data reaches DOM objects (HTML elements). Then again, displaying "Cat", instead of "Animal".

Thank you, Temp.

  • 1
    "*PLEASE IGNORE IT AS IT IS NONSENSE*" - now what is your question? – Bergi Mar 27 '23 at 22:32
  • Basically the approach for this, you are right I will edit it. I am not looking for anyone to write code, rather inform me of the technologies and/or approach to achieve such interceptions and modifications of JSON payload. – NotYourClone Mar 27 '23 at 22:37
  • Have you tried to run the code above? Does it work? Do you have any problems with it? – RAllen Mar 27 '23 at 22:45
  • @RAllen yes I tried, it does not work, I believe the `window.fetch` is not what I need. I get say AJAX data or whatever, I get data after the website is up and running in browser, like when a user presses a button and the request goes out. I just need to modify the response from the server in the browser. – NotYourClone Mar 27 '23 at 22:49
  • @NotYourClone probably your website is using pure AJAX requests. In this case, you will need to override `XMLHttpRequest` class or its methods. You can quickly check if your website is using `XMLHttpRequest` by doing something like `XMLHttpRequest = undefined` in the browser console. If this breaks your website and it starting to show errors in the console then you definitely need to override `XMLHttpRequest` or its methods. – RAllen Mar 27 '23 at 22:58
  • @RAllen, that is very good to know, I tried it and it did not break my website (I am using the fetchAPI on the actual client JS). Nonetheless, a very important thing to know I did not know. – NotYourClone Mar 27 '23 at 23:01
  • @NotYourClone no worries. Also, check this question/answer. It could be helpful https://stackoverflow.com/questions/13765031/scrape-eavesdrop-ajax-data-using-javascript – RAllen Mar 27 '23 at 23:04

0 Answers0