Library for intercepting AJAX communications

Browserify'd version of ajax-interceptor

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.cn-greasyfork.org/scripts/11365/65323/Library%20for%20intercepting%20AJAX%20communications.js

  1. // ==UserScript==
  2. // @name Library for intercepting AJAX communications
  3. // @version 1.0
  4. // @author slorber (sauce: https://github.com/slorber/ajax-interceptor)
  5. // @description Browserify'd version of ajax-interceptor
  6. // @namespace MrHat.Torn
  7. // ==/UserScript==
  8.  
  9.  
  10. require=(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({"ajax-intercept":[function(require,module,exports){
  11. 'use strict';
  12.  
  13. var COMPLETED_READY_STATE = 4;
  14.  
  15. var RealXHRSend = XMLHttpRequest.prototype.send;
  16.  
  17. var requestCallbacks = [];
  18. var responseCallbacks = [];
  19.  
  20.  
  21. var wired = false;
  22.  
  23.  
  24. function arrayRemove(array,item) {
  25. var index = array.indexOf(item);
  26. if (index > -1) {
  27. array.splice(index, 1);
  28. } else {
  29. throw new Error("Could not remove " + item + " from array");
  30. }
  31. }
  32.  
  33.  
  34. function fireCallbacks(callbacks,xhr) {
  35. for( var i = 0; i < callbacks.length; i++ ) {
  36. callbacks[i](xhr);
  37. }
  38. }
  39.  
  40.  
  41. exports.addRequestCallback = function(callback) {
  42. requestCallbacks.push(callback);
  43. };
  44. exports.removeRequestCallback = function(callback) {
  45. arrayRemove(requestCallbacks,callback);
  46. };
  47.  
  48.  
  49. exports.addResponseCallback = function(callback) {
  50. responseCallbacks.push(callback);
  51. };
  52. exports.removeResponseCallback = function(callback) {
  53. arrayRemove(responseCallbacks,callback);
  54. };
  55.  
  56.  
  57.  
  58. function fireResponseCallbacksIfCompleted(xhr) {
  59. if( xhr.readyState === COMPLETED_READY_STATE ) {
  60. fireCallbacks(responseCallbacks,xhr);
  61. }
  62. }
  63.  
  64. function proxifyOnReadyStateChange(xhr) {
  65. var realOnReadyStateChange = xhr.onreadystatechange;
  66. if ( realOnReadyStateChange ) {
  67. xhr.onreadystatechange = function() {
  68. fireResponseCallbacksIfCompleted(xhr);
  69. realOnReadyStateChange();
  70. };
  71. }
  72. }
  73.  
  74. exports.wire = function() {
  75. if ( wired ) throw new Error("Ajax interceptor already wired");
  76.  
  77. // Override send method of all XHR requests
  78. XMLHttpRequest.prototype.send = function() {
  79.  
  80. // Fire request callbacks before sending the request
  81. fireCallbacks(requestCallbacks,this);
  82.  
  83. // Wire response callbacks
  84. if( this.addEventListener ) {
  85. var self = this;
  86. this.addEventListener("readystatechange", function() {
  87. fireResponseCallbacksIfCompleted(self);
  88. }, false);
  89. }
  90. else {
  91. proxifyOnReadyStateChange(this);
  92. }
  93.  
  94. RealXHRSend.apply(this, arguments);
  95. };
  96. wired = true;
  97. };
  98.  
  99.  
  100. exports.unwire = function() {
  101. if ( !wired ) throw new Error("Ajax interceptor not currently wired");
  102. XMLHttpRequest.prototype.send = RealXHRSend;
  103. wired = false;
  104. };
  105. },{}]},{},[]);