Options
All
  • Public
  • Public/Protected
  • All
Menu

Class XSpy

After a document loads XSpy class, it is available by calling window.xspy or just xspy.
Note: Not window.XSpy but window.xspy. (All lower characters)

name

xspy

Hierarchy

  • XSpy

Index

Properties

Static Readonly OriginalFetch

OriginalFetch: fetch = originalFetch

Original fetch. window.fetch will be replaced to customized fetch on xspy.enable() called. Regardless of whether xspy.enable() called, xspy.OriginalFetch always returns original fetch.

example
if(window.fetch !== xspy.OriginalFetch){
  // xspy is enabled and `window.fetch` is replaced by spying module.
}

Static Readonly OriginalXHR

OriginalXHR: {} = window.XMLHttpRequest

Original XMLHttpRequest. window.XMLHttpRequest will be replaced to customized XMLHttpRequest on xspy.enable() called. Regardless of whether xspy.enable() called, xspy.OriginalXHR always returns original XMLHttpRequest.

example
if(window.XMLHttpRequest !== xspy.OriginalXHR){
  // xspy is enabled and `XMLHttpRequest` is replaced by spying module.
}

Type declaration

Methods

Static clearAll

  • clearAll(): void
  • Remove all request/response listeners.

    example
    xspy.clearAll();
    // xspy.getRequestListeners().length === 0
    // xspy.getResponseListeners().length === 0

    Returns void

Static clearRequestHandler

  • clearRequestHandler(): void
  • Remove all request listeners.

    example
    xspy.clearRequestHandler();
    // xspy.getRequestListeners().length === 0

    Returns void

Static clearResponseHandler

  • clearResponseHandler(): void
  • Remove all response listeners.

    example
    xspy.clearResponseHandler();
    // xspy.getResponseListeners().length === 0

    Returns void

Static disable

  • disable(): void
  • Stop to listen request/response. It replaces back to original XMLHttpRequest and window.fetch.

    example
    xspy.disable(); // XMLHttpRequest and fetch are set back to original modules.

    Returns void

Static enable

  • enable(): void
  • Start to listen request/response from XMLHttpRequest/fetch. Request/Response hook is enabled by replacing XMLHttpRequest and window.fetch to the ones in this library.

    Note: This does not polyfill window.fetch for Internet Explorer which does not originally implement window.fetch.

    example
    xspy.enable(); // XMLHttpRequest and fetch are replaced by spying modules.

    Returns void

Static getRequestListeners

  • Get an array of request listeners. Note that any operations (push/pop/unshift/shift) on the array does not affect saved listeners.

    example
    var listeners = xspy.getRequestListeners();

    Returns ListenerForRequest<"xhr" | "fetch">[]

Static getResponseListeners

Static isEnabled

  • isEnabled(): boolean
  • Check xspy is enabled and listening request/response.

    example
    if(xspy.isEnabled()){
      // then `window.XMLHttpRequest !== xspy.OriginalXHR`
    }

    Returns boolean

Static offRequest

  • Remove a request listener.

    example
    var listener = function(request){...};
    xspy.onRequest(listener);  // xspy.getRequestListeners().length === 1
    xspy.offRequest(listener); // xspy.getRequestListeners().length === 0

    Parameters

    Returns void

Static offResponse

  • Remove a response listener.

    example
    var listener = function(request){...};
    xspy.onResponse(listener);  // xspy.getResponseListeners().length === 1
    xspy.offResponse(listener); // xspy.getResponseListeners().length === 0

    Parameters

    Returns void

Static onRequest

  • Add custom request listener to index n. (listener at index n=0 will be called first) If you do not specify n, it appends listener to the last. (Called after all previous listeners finishes.)

    This listener will be called just before web request by window.fetch() or xhr.sent() departs from browser. You can modify the request object(i.e. headers, body) before it is sent.

    Note that when you supplies listener as 2 parameters function(request and callback), request will not be dispatched until you manually run callback() function in handler.

    If you run callback() without any arguments or with non-object value like false, request processing goes forward without generating fake response.

    If you run callback(res) with a fake response object, it immediately returns the fake response after all onRequest listeners finishes. In this case, real request never flies to any external network.

    example

    Modify request before dispatched

    xspy.onRequest(function (request){
      request.method = "POST";
      request.url = "...";
      ...
      return;
    });
    example

    Return fake response after waiting 3000ms

    xspy.onRequest(function listenerForRequest(request, sendResponse){
      setTimeout(function(){
        var response = {
          status: 200,
          statusText: "OK",
          headers: {"content-type": "application/json"},
        };
    
        if(request.ajaxType === "xhr"){
          response = {
            ajaxType: "xhr",
            response: {result: 3},
            responseType: "json",
            responseText: "{'result':3}",
          };
        }
        else{ // ajaxType === "fetch"
          response = {
            ajaxType: "fetch",
            ok: true,
            redirected: false,
            type: "basic",
            body: "{'result':3}",
            url: "https://..."
          };
        }
    
        sendResponse(response); // after 3000ms elapses, send fake response.
      }, 3000);
    });
    example

    Return fake response after awaited async operation

    xspy.onRequest(async (request, sendResponse) => {
      var result = await someAsyncOperation();
    
      var response = {...};
      sendResponse(response);
    });

    Parameters

    Returns void

Static onResponse

  • Add custom response listener to index n. (listener at index n=0 will be called first) If you do not specify n, it appends listener to the last. (Called after all previous listeners finishes.)

    This listener will be called just before API response is available at window.fetch().then(res => ...) or xhr.onreadystatechange, and so on. You can modify the response object before it is available to the original requester.

    Note that when you supplies listener as 3 parameters function(request, response and callback), response will not be returned to the original requester until you manually run callback() function in handler.

    example

    Modify response before it is available to user scripts

    xspy.onResponse(function (request, response){
      response.status = 400;
      response.statusText = "Bad Request";
      response.body = "";
      ...
    });
    example

    Return response after waiting 3000ms

    xspy.onResponse(function listenerForResponse(request, response, next){
      setTimeout(function(){
        var modifiedResponse = {...response, status: 200, statusText: "OK"};
        if(response.ajaxType === "fetch"){
          modifiedResponse.ok = true;
        }
        next(modifiedResponse); // after 3000ms elapses, send the response.
      }, 3000);
    });
    example

    Return response after awaited async operation

    xspy.onResponse(async (request, response, next) => {
      var result = await someAsyncOperation();
    
      var response = {...};
      next(response);
    });

    Parameters

    Returns void

Generated using TypeDoc