GM XHR

jQuery AJAX wrapper for GM_xmlhttpRequest

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.cn-greasyfork.org/scripts/401399/938754/GM%20XHR.js

  1. // ==UserScript==
  2. // @name GM XHR
  3. // @author Ryan Greenberg, Martin Monperrus, Damien Clark, RobotOilInc
  4. // @version 1.1
  5. // @description jQuery AJAX wrapper for GM_xmlhttpRequest
  6. // @homepageURL https://greasyfork.org/scripts/401399-gm-xhr
  7. // @supportURL https://greasyfork.org/scripts/401399-gm-xhr
  8. // @grant GM_xmlhttpRequest
  9. // ==/UserScript==
  10.  
  11. /* jshint esversion: 6 */
  12.  
  13. function GM_XHR() {
  14. 'use strict';
  15.  
  16. this.type = null;
  17. this.url = null;
  18. this.async = null;
  19. this.username = null;
  20. this.password = null;
  21. this.status = null;
  22. this.headers = {};
  23. this.readyState = null;
  24.  
  25. this.abort = function () {
  26. this.readyState = 0;
  27. };
  28.  
  29. this.getAllResponseHeaders = function (name) {
  30. if (this.readyState !== 4) return '';
  31. return this.responseHeaders;
  32. };
  33.  
  34. this.getResponseHeader = function (header) {
  35. let value = null;
  36. if (this.responseHeaders) {
  37. const regex = new RegExp(`^${header}: (.*)$`, 'igm');
  38. const result = [];
  39.  
  40. let match = regex.exec(this.responseHeaders);
  41. while (match !== null) {
  42. result.push(match[1]);
  43. match = regex.exec(this.responseHeaders);
  44. }
  45.  
  46. if (result.length > 0) {
  47. value = result.join(', ');
  48. }
  49. }
  50.  
  51. return value;
  52. };
  53.  
  54. this.open = function (type, url, async, username, password) {
  55. this.type = type || null;
  56. this.url = url || null;
  57. this.async = async || null;
  58. this.username = username || null;
  59. this.password = password || null;
  60. this.readyState = 1;
  61. };
  62.  
  63. this.setRequestHeader = function (name, value) {
  64. this.headers[name] = value;
  65. };
  66.  
  67. this.send = function (data) {
  68. this.data = data;
  69. const that = this;
  70.  
  71. if (typeof GM.xmlHttpRequest === 'undefined' && typeof GM_xmlhttpRequest === 'undefined') {
  72. throw new Error("You need to enable 'GM.xmlHttpRequest' or 'GM_xmlhttpRequest'.");
  73. }
  74.  
  75. // Detect if using older GM API (or other userscript engines)
  76. const agent = (typeof GM_xmlhttpRequest === 'undefined') ? GM.xmlHttpRequest : GM_xmlhttpRequest;
  77.  
  78. // https://github.com/scriptish/scriptish/wiki/GM_xmlhttpRequest
  79. agent({
  80. method: this.type,
  81. url: this.url,
  82. headers: this.headers,
  83. data: this.data,
  84. responseType: this.responseType,
  85. onload(rsp) {
  86. for (const k in Object.getOwnPropertyNames(rsp)) {
  87. that[Object.getOwnPropertyNames(rsp)[k]] = rsp[Object.getOwnPropertyNames(rsp)[k]];
  88. }
  89.  
  90. if (that.onload) that.onload(); else that.onreadystatechange();
  91. },
  92. onerror(rsp) {
  93. for (const k in Object.getOwnPropertyNames(rsp)) {
  94. that[Object.getOwnPropertyNames(rsp)[k]] = rsp[Object.getOwnPropertyNames(rsp)[k]];
  95. }
  96.  
  97. if (that.onload) that.onload(); else that.onreadystatechange();
  98. },
  99. });
  100. };
  101. }