HttpRequest

HttpRequest for any type of request and HttpRequestHTML to request webpage. Supports caching of responses to handle status 304.

当前为 2020-06-20 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.gf.qytechs.cn/scripts/405144/818473/HttpRequest.js

  1. // ==UserScript==
  2. // @name HttpRequest
  3. // @namespace hoehleg.userscripts.private
  4. // @version 0.1
  5. // @description HttpRequest for any type of request and HttpRequestHTML to request webpage. Supports caching of responses to handle status 304.
  6. // @author Gerrit Höhle
  7. //
  8. // @grant GM_xmlhttpRequest
  9. //
  10. // @require https://gf.qytechs.cn/scripts/405143-simplecache/code/SimpleCache.js
  11. // @require https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js
  12. //
  13. // ==/UserScript==
  14.  
  15. /* jshint esnext: true */
  16. /* globals SimpleCache, $, jQuery */
  17. const HttpRequest = (() => {
  18. const responsesCache = new SimpleCache({ keyExtractor: (httpRequest) => [ httpRequest.method, httpRequest.url, httpRequest.headers, httpRequest.data, httpRequest.params ].join() });
  19.  
  20. const urlWithParams = (url, paramsObject) => {
  21. const params = Object.entries(paramsObject).map(([key, value]) => key + '=' + value).join('&');
  22. return (url.length > 0) ? url + '?' + params : params;
  23. };
  24.  
  25. return class HttpRequest {
  26. constructor({ method, url = window.location.href, headers = {}, data = '', keepInCacheTimoutMs = 10 * 60000, params = {} } = {}) {
  27. Object.assign(this, { method, url, headers, data, keepInCacheTimoutMs, params });
  28. }
  29.  
  30. send() {
  31. return new Promise((resolve, reject) => {
  32. let method, url, onload, onerror, headers, data;
  33. method = this.method.toUpperCase();
  34. url = this.url;
  35. headers = this.headers;
  36. data = this.data;
  37.  
  38. onload = (response) => {
  39. switch (response.status) {
  40. case 200:
  41. if (this.keepInCacheTimoutMs > 0) {
  42. responsesCache.set(this, response, this.keepInCacheTimoutMs);
  43. }
  44. break;
  45. case 304:
  46. if (this.isCached()) {
  47. response = this.getFromCache();
  48. response.status = 304;
  49. }
  50. break;
  51. default:
  52. reject(Error(`Status: ${response.status}, Error: ${response.statusText}`));
  53. return;
  54. }
  55. resolve(response);
  56. };
  57.  
  58. onerror = () => {
  59. reject(Error('network error'));
  60. };
  61.  
  62. switch (method) {
  63. case 'GET':
  64. if (this.params) {
  65. url = urlWithParams(url, this.params);
  66. }
  67. break;
  68. case 'POST':
  69. case 'PUT':
  70. headers = Object.assign({ 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' }, headers || {});
  71. if (this.params) {
  72. data = urlWithParams(data, this.params);
  73. }
  74. break;
  75. }
  76. GM_xmlhttpRequest({ method, url, onload, onerror, headers, data });
  77. });
  78. }
  79.  
  80. isCached() {
  81. return responsesCache.has(this);
  82. }
  83.  
  84. getFromCache() {
  85. return responsesCache.get(this);
  86. }
  87. static send(...args) {
  88. return new HttpRequest(...args).send();
  89. }
  90. };
  91. })();
  92.  
  93. class HttpRequestHtml extends HttpRequest {
  94. constructor({ url, method = "GET", headers, data, keepInCacheTimoutMs, params } = {}) {
  95. super({ method, url, headers, data, keepInCacheTimoutMs, params });
  96. }
  97. send() {
  98. return super.send().then(response => new Promise(resolve => {
  99. if (response.status == 200) {
  100. response.html = new DOMParser().parseFromString(response.responseText, 'text/html');
  101. resolve(response);
  102. }
  103. }));
  104. }
  105. static send(...args) {
  106. return new HttpRequestHtml(...args).send();
  107. }
  108. }

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址