🏠 Home 

Greasy Fork is available in English.

GM XHR

jQuery AJAX wrapper for GM_xmlhttpRequest

สคริปต์นี้ไม่ควรถูกติดตั้งโดยตรง มันเป็นคลังสำหรับสคริปต์อื่น ๆ เพื่อบรรจุด้วยคำสั่งเมทา // @require https://update.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. /* jshint esversion: 6 */
  11. function GM_XHR() {
  12. 'use strict';
  13. this.type = null;
  14. this.url = null;
  15. this.async = null;
  16. this.username = null;
  17. this.password = null;
  18. this.status = null;
  19. this.headers = {};
  20. this.readyState = null;
  21. this.abort = function () {
  22. this.readyState = 0;
  23. };
  24. this.getAllResponseHeaders = function (name) {
  25. if (this.readyState !== 4) return '';
  26. return this.responseHeaders;
  27. };
  28. this.getResponseHeader = function (header) {
  29. let value = null;
  30. if (this.responseHeaders) {
  31. const regex = new RegExp(`^${header}: (.*)$`, 'igm');
  32. const r###lt = [];
  33. let match = regex.exec(this.responseHeaders);
  34. while (match !== null) {
  35. r###lt.push(match[1]);
  36. match = regex.exec(this.responseHeaders);
  37. }
  38. if (r###lt.length > 0) {
  39. value = r###lt.join(', ');
  40. }
  41. }
  42. return value;
  43. };
  44. this.open = function (type, url, async, username, password) {
  45. this.type = type || null;
  46. this.url = url || null;
  47. this.async = async || null;
  48. this.username = username || null;
  49. this.password = password || null;
  50. this.readyState = 1;
  51. };
  52. this.setRequestHeader = function (name, value) {
  53. this.headers[name] = value;
  54. };
  55. this.send = function (data) {
  56. this.data = data;
  57. const that = this;
  58. if (typeof GM.xmlHttpRequest === 'undefined' && typeof GM_xmlhttpRequest === 'undefined') {
  59. throw new Error("You need to enable 'GM.xmlHttpRequest' or 'GM_xmlhttpRequest'.");
  60. }
  61. // Detect if using older GM API (or other userscript engines)
  62. const agent = (typeof GM_xmlhttpRequest === 'undefined') ? GM.xmlHttpRequest : GM_xmlhttpRequest;
  63. // https://github.com/scriptish/scriptish/wiki/GM_xmlhttpRequest
  64. agent({
  65. method: this.type,
  66. url: this.url,
  67. headers: this.headers,
  68. data: this.data,
  69. responseType: this.responseType,
  70. onload(rsp) {
  71. for (const k in Object.getOwnPropertyNames(rsp)) {
  72. that[Object.getOwnPropertyNames(rsp)[k]] = rsp[Object.getOwnPropertyNames(rsp)[k]];
  73. }
  74. if (that.onload) that.onload(); else that.onreadystatechange();
  75. },
  76. onerror(rsp) {
  77. for (const k in Object.getOwnPropertyNames(rsp)) {
  78. that[Object.getOwnPropertyNames(rsp)[k]] = rsp[Object.getOwnPropertyNames(rsp)[k]];
  79. }
  80. if (that.onload) that.onload(); else that.onreadystatechange();
  81. },
  82. });
  83. };
  84. }