Greasy Fork

Shined up real nice.

xhrHijacker

Hijack XHR no matter if you use `@grant` or not.

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @require https://greasyfork.org/scripts/26651-xhrhijacker/code/xhrHijacker.js?version=171120

After trying this script, you can ask a question about it, review it, or report it.
Author
Tithen-Firion
Ratings
0 0 0
Version
0.0.1.20170123090020
Created
Updated
License
N/A
Applies to
All sites

Author's Description

Allows you to hijack XHR in both UserScripts using and not using @grant. You can change method, url, add headers, abort, use loaded data. You can't change loaded data though.

Example usage:

xhrHijacker(function(xhr, id, origin, args) {
  // id is unique string, use it to recognise your xhr between ready states
  // origin can be: open|send|readystatechange|load
  // args are used only with origin set to open or send
  if(origin == "open") {
    // happens before real open
    args[0] = "GET";
  } else if(origin == "send") {
    // happens before real send
    xhr.setRequestHeader("X-Foo", "Bar");
  } else if(origin == "readystatechange") {
    //you can abort XHR after it is sent
    if(xhr.readyState == 2)
      xhr.abort();
  } else if(origin == "load") {
    console.log(xhr.getAllResponseHeaders());
    console.log(xhr.responseType);
    console.log(xhr.response);
    console.log(xhr.status);
  }
});

Share