Includes : HttpRequest

HttpRequest Function

  1. // ==UserScript==
  2. // @name Includes : HttpRequest
  3. // @description HttpRequest Function
  4. // @author You
  5. // @version 1.0
  6. // @language en
  7. // @include nowhere
  8. // @exclude *
  9. // @namespace https://greasyfork.org/users/1385333
  10. // ==/UserScript==
  11.  
  12. /**************************************************************************
  13.  
  14. This program is free software: you can redistribute it and/or modify
  15. it under the terms of the GNU General Public License as published by
  16. the Free Software Foundation, either version 3 of the License, or
  17. (at your option) any later version.
  18.  
  19. This program is distributed in the hope that it will be useful,
  20. but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  22. the GNU General Public License for more details.
  23.  
  24. You should have received a copy of the GNU General Public License
  25. along with this program. If not, see <http://www.gnu.org/licenses/>.
  26.  
  27. **************************************************************************/
  28.  
  29. HttpRequest = function () {};
  30. HttpRequest.open = function (params) {
  31. return new (function (params) {
  32. if (!/^https?:\/\//.test(params.url)) {
  33. params.url = "http://" + params.url;
  34. }
  35.  
  36. this.options = {
  37. "method" : params.method.toUpperCase() || "GET",
  38. "url" : params.url,
  39. "headers" : { "User-Agent" : window.navigator.userAgent },
  40. "synchronous" : !!params.synchronous,
  41. "onload" : function (e) {
  42. var obj = params.parameters || {};
  43.  
  44. obj.response = {
  45. "raw" : e,
  46. "text" : e.responseText,
  47. "xml" : e.responseXML
  48. };
  49.  
  50. if (/^Content-Type: (?:text|application)\/(?:x-)?json/m.test(e.responseHeaders)) {
  51. try {
  52. obj.response.json = (typeof JSON != "undefined" && typeof JSON.parse == "function" ? JSON.parse(e.responseText) : eval("(" + e.responseText + ")") );
  53. } catch (e) {
  54. obj.response.json = {};
  55. }
  56. }
  57.  
  58. if (!obj.response.xml) {
  59. if (/^Content-Type: text\/xml/m.test(e.responseHeaders)) {
  60. obj.response.xml = new DOMParser().parseFromString(e.responseText, "text/xml");
  61. } else if (/^Content-Type: text\/html/m.test(e.responseHeaders)) {
  62. var doc = document.implementation.createHTMLDocument("");
  63. doc.documentElement.innerHTML = e.responseText;
  64.  
  65. obj.response.xml = doc;
  66. }
  67. }
  68.  
  69. if (typeof params.onsuccess == "function") {
  70. params.onsuccess(obj);
  71. }
  72. }
  73. };
  74.  
  75. if ("headers" in params) {
  76. for (var header in params.headers) {
  77. this.options.headers[header] = params.headers[header];
  78. }
  79. }
  80.  
  81. this.send = function (content) {
  82. if (content) {
  83. if (content instanceof unsafeWindow.HTMLCollection || content instanceof HTMLCollection) {
  84. content = Array.prototype.slice.apply(content);
  85. }
  86.  
  87. var data = {};
  88. if (content instanceof Array) {
  89. for (var e of content) { // Using 'for...of' loop here
  90. if (!/^(?:radio|checkbox)$/i.test(e.type) || e.checked) {
  91. if (e.checked && /^checkbox$/i.test(e.type)) {
  92. if (e.name in data) {
  93. data[e.name].push(e.value);
  94. } else {
  95. data[e.name] = [e.value];
  96. }
  97. } else {
  98. data[e.name] = e.value;
  99. }
  100. }
  101. }
  102.  
  103. content = data;
  104. }
  105.  
  106. if (typeof content == "object") {
  107. var x = "";
  108. for (var key in content) {
  109. if (content[key] instanceof Array) {
  110. var keyarr = key.replace(/^\s+|\s+$/g, "");
  111. if (!/\[\w*\]$/.test(key))
  112. keyarr += "[]";
  113.  
  114. for (var v of content[key]) { // Using 'for...of' loop for array items
  115. x += "&" + encodeURIComponent(keyarr) + "=" + encodeURIComponent(v);
  116. }
  117. } else {
  118. x += "&" + encodeURIComponent(key) + "=" + encodeURIComponent(content[key]);
  119. }
  120. }
  121.  
  122. content = x.substr(1);
  123.  
  124. if ("POST" == this.options.method) {
  125. this.options.headers["Content-Type"] = "application/x-www-form-urlencoded";
  126. this.options.data = content;
  127. } else {
  128. this.options.url += (/\?/.test(this.options.url) ? "&" : "?") + content;
  129. }
  130. } else {
  131. this.options.data = content;
  132. }
  133. }
  134.  
  135. this.result = GM_xmlhttpRequest(this.options);
  136.  
  137. return this;
  138. }
  139. })(params);
  140. };