Options
All
  • Public
  • Public/Protected
  • All
Menu

xspy - v0.0.3

Index

Type aliases

CallbackForRequest

CallbackForRequest<T>: { moveToHeaderReceived: (dummyResponse: T extends "xhr" ? Pick<ResponseByXHR, "headers" | "status" | "statusText"> : never) => void; moveToLoading: (dummyResponse: T extends "xhr" ? Pick<ResponseByXHR, "headers" | "status" | "statusText" | "responseText"> : never) => void }

When called in xspy's request listener, immediately return fake response without dispatching request to external network after all listeners run.

You can move readyState one by one by calling callback.moveToHeaderReceived and/or callback.moveToLoading.

example

Return fake response step by step with simulating loading.

xspy.onRequest(function listenerForRequest(request, sendResponse){
  if(request.ajaxType === "xhr"){
    sendResponse.moveToHeaderReceived({
      headers: {"content-type": "application/json"},
      status: 200,
      statusText: "OK",
    });

    sendResponse.moveToLoading({
      headers: {"content-type": "application/json"},
      status: 200,
      statusText: "OK",
      responseText: "{'result':"
    });

    sendResponse({
      ajaxType: "xhr",
      status: 200,
      statusText: "OK",
      headers: {"content-type": "application/json"},
      response: {result: 3},
      responseType: "json",
      responseText: "{'result':3}",
    });
  }
});

Type parameters

  • T: "xhr" | "fetch"

Type declaration

    • (dummyResponse: T extends "xhr" ? ResponseByXHR : ResponseByFetch): unknown
    • Return dummy response to user scripts after all request listeners run.

      example
       var dummyResponse = {
         ajaxType: "fetch",
         status: 200,
         statusText: "OK",
         headers: {"content-type": "application/json"},
         ok: true,
         redirected: false,
         type: "basic",
         body: "{'result':3",
         url: "https://..."
       };
      
       callbackForRequest(dummyResponse);

      Parameters

      • dummyResponse: T extends "xhr" ? ResponseByXHR : ResponseByFetch

      Returns unknown

  • moveToHeaderReceived: (dummyResponse: T extends "xhr" ? Pick<ResponseByXHR, "headers" | "status" | "statusText"> : never) => void

    Move readyState to HEADERS_RECEIVED. This is only effective for xhr response.

    example
    callbackForRequest.moveToHeaderReceived({
      headers: {"content-type": "application/json"},
      status: 200,
      statusText: "OK",
    });
      • (dummyResponse: T extends "xhr" ? Pick<ResponseByXHR, "headers" | "status" | "statusText"> : never): void
      • Parameters

        • dummyResponse: T extends "xhr" ? Pick<ResponseByXHR, "headers" | "status" | "statusText"> : never

        Returns void

  • moveToLoading: (dummyResponse: T extends "xhr" ? Pick<ResponseByXHR, "headers" | "status" | "statusText" | "responseText"> : never) => void

    Move readyState to LOADING. This is only effective for xhr response.

    example
    callbackForRequest.moveToLoading({
       headers: {"content-type": "application/json"},
       status: 200,
       statusText: "OK",
       responseText: "{'result':"
     });
      • (dummyResponse: T extends "xhr" ? Pick<ResponseByXHR, "headers" | "status" | "statusText" | "responseText"> : never): void
      • Parameters

        • dummyResponse: T extends "xhr" ? Pick<ResponseByXHR, "headers" | "status" | "statusText" | "responseText"> : never

        Returns void

CallbackForResponse

CallbackForResponse<T>: (modifiedResponse?: T extends "xhr" ? ResponseByXHR : ResponseByFetch) => unknown

When called in xspy's response listener, return modified response to user script. This callback is useful for asynchronous operation.

example

Return fake response step by step with simulating loading.

xspy.onResponse(async function listenerForResponse(request, response, callbackForResponse){
  var isValid = await checkResponse(); // Doing some async operation.

  if(isValid){
    callbackForResponse();
  }
  else{
    var modifiedResponse = {
      ...response,
      status: 400,
      statusText: "Bad Request"
    };
    if(response.ajaxType === "fetch"){
      modifiedResponse.ok = false;
    }
    callbackForResponse(modifiedResponse);
  }
});

Type parameters

  • T: "xhr" | "fetch"

Type declaration

    • (modifiedResponse?: T extends "xhr" ? ResponseByXHR : ResponseByFetch): unknown
    • Parameters

      • Optional modifiedResponse: T extends "xhr" ? ResponseByXHR : ResponseByFetch

      Returns unknown

ListenerForRequest

ListenerForRequest<T>: (this: T extends "xhr" ? XMLHttpRequest : unknown, request: T extends "xhr" ? RequestByXHR : RequestByFetch, sendResponse?: CallbackForRequest<T>) => unknown

Listen XHR/fetch request to be dispatched from user script and modify/view request as you like. This listener behaves differently when defined as 1 parameter function or 2 parameters function.

When second parameter sendResponse is specified as a function argument, original request stops until sendResponse is called in listener function.

example

Modify request before dispatched

xspy.onRequest(function listenerForRequest(request){
  request.method = "POST";
  request.url = "...";
  ...
});
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);
});

Type parameters

  • T: "xhr" | "fetch"

Type declaration

    • (this: T extends "xhr" ? XMLHttpRequest : unknown, request: T extends "xhr" ? RequestByXHR : RequestByFetch, sendResponse?: CallbackForRequest<T>): unknown
    • Parameters

      • this: T extends "xhr" ? XMLHttpRequest : unknown
      • request: T extends "xhr" ? RequestByXHR : RequestByFetch
      • Optional sendResponse: CallbackForRequest<T>

      Returns unknown

ListenerForResponse

ListenerForResponse<T>: (this: T extends "xhr" ? XMLHttpRequest : unknown, request: T extends "xhr" ? RequestByXHR : RequestByFetch, response: T extends "xhr" ? ResponseByXHR : ResponseByFetch, next?: CallbackForResponse<T>) => unknown

Listen on XHR/fetch response returned from a server to modify/view response as you like. This listener behaves differently when defined as 2 parameter function or 3 parameters function.

When third parameter next is specified as a function argument, user scripts will not receive response until next is called in listener function.

example

Modify response before it is available to user scripts

xspy.onResponse(function listenerForResponse(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);
});

Type parameters

  • T: "xhr" | "fetch"

Type declaration

    • (this: T extends "xhr" ? XMLHttpRequest : unknown, request: T extends "xhr" ? RequestByXHR : RequestByFetch, response: T extends "xhr" ? ResponseByXHR : ResponseByFetch, next?: CallbackForResponse<T>): unknown
    • Parameters

      • this: T extends "xhr" ? XMLHttpRequest : unknown
      • request: T extends "xhr" ? RequestByXHR : RequestByFetch
      • response: T extends "xhr" ? ResponseByXHR : ResponseByFetch
      • Optional next: CallbackForResponse<T>

      Returns unknown

Generated using TypeDoc