Giter VIP home page Giter VIP logo

chrome-extension-get-window-and-xhr-fetch-response-body's Introduction

image

Here is example show how to get inspected page's window and response body(not request body).

Using injected js to get window, and pass it to content_script.js with window.postMessage, but you need to use a library to stringify the window firstly. Due to window may contain recursive object, it will fail the JSON.stringify. So I use library https://github.com/Canop/JSON.prune here.

There are three solution to get response body in chrome extension;

chrome.devtools.network.onRequestFinished.addListener(request => {
  request.getContent((body) => {
    if (request.request && request.request.url) {
      if (request.request.url.includes('facebook.com')) {

         //continue with custom code
         var bodyObj = JSON.parse(body);//etc.
      }
}
});
});

2.using debugger api https://stackoverflow.com/questions/18534771/chrome-extension-how-to-get-http-response-body, use this solution the waring messasge of debugger will display above the inspected page all the time.

var currentTab;
var version = "1.0";

chrome.tabs.query( //get current Tab
    {
        currentWindow: true,
        active: true
    },
    function(tabArray) {
        currentTab = tabArray[0];
        chrome.debugger.attach({ //debug at current tab
            tabId: currentTab.id
        }, version, onAttach.bind(null, currentTab.id));
    }
)

function onAttach(tabId) {

    chrome.debugger.sendCommand({ //first enable the Network
        tabId: tabId
    }, "Network.enable");

    chrome.debugger.onEvent.addListener(allEventHandler);
}

function allEventHandler(debuggeeId, message, params) {

    if (currentTab.id != debuggeeId.tabId) {
        return;
    }

    if (message == "Network.responseReceived") { //response return 
        chrome.debugger.sendCommand({
            tabId: debuggeeId.tabId
        }, "Network.getResponseBody", {
            "requestId": params.requestId
        }, function(response) {
            // you get the response body here!
            // you can close the debugger tips by:
            chrome.debugger.detach(debuggeeId);
        });
    }

}

3. using injected js to change prototype of XML and fetch of inspected page (recommend), this example will take this solution

* be notice that the chrome.webRequest api will only intercepted the request body instead of response body.

Reference

  1. Chrome Extension Api https://developer.chrome.com/docs/extensions/reference/

  2. How to get http(XHR) response body https://stackoverflow.com/questions/18534771/chrome-extension-how-to-get-http-response-body https://stackoverflow.com/questions/61789895/electron-how-to-intercept-http-response-body https://stackoverflow.com/questions/8939467/chrome-extension-to-read-http-response

  3. How to get http(Fetch) response body https://blog.logrocket.com/intercepting-javascript-fetch-api-requests-responses/

chrome-extension-get-window-and-xhr-fetch-response-body's People

Contributors

ansonhwang86 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.