HttpRequest

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

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

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.gf.qytechs.cn/scripts/405144/817109/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. const method = this.method.toUpperCase();
  33. let url = this.url;
  34. let headers = this.headers;
  35. let data = this.data;
  36.  
  37. const onload = (response) => {
  38. switch (response.status) {
  39. case 200:
  40. if (this.keepInCacheTimoutMs > 0) {
  41. responsesCache.set(this, response, this.keepInCacheTimoutMs);
  42. }
  43. break;
  44. case 304:
  45. if (this.isCached()) {
  46. response = this.getFromCache();
  47. response.status = 304;
  48. }
  49. break;
  50. default:
  51. reject(Error(`Status: ${response.status}, Error: ${response.statusText}`));
  52. return;
  53. }
  54. resolve(response);
  55. };
  56.  
  57. const onerror = () => {
  58. reject(Error('network error'));
  59. };
  60.  
  61. switch (method) {
  62. case 'GET':
  63. if (this.params) {
  64. url = urlWithParams(url, this.params);
  65. }
  66. break;
  67. case 'POST':
  68. case 'PUT':
  69. Object.assign(headers, { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' });
  70. if (this.params) {
  71. data = urlWithParams(data, this.params);
  72. }
  73. break;
  74. }
  75.  
  76. GM_xmlhttpRequest({ method, url, onload, onerror: this.onError, headers: this.headers, data });
  77. });
  78. }
  79.  
  80. isCached() {
  81. return responsesCache.has(this);
  82. }
  83.  
  84. getFromCache() {
  85. return responsesCache.get(this);
  86. }
  87. };
  88. })();
  89.  
  90. class HttpRequestHtml extends HttpRequest {
  91. constructor({ url, method = "GET", headers, data, keepInCacheTimoutMs, params } = {}) {
  92. super({ method, url, headers, data, keepInCacheTimoutMs, params });
  93. }
  94. send() {
  95. return super.send().then(response => new Promise(resolve => {
  96. if (response.status == 200) {
  97. response.html = new DOMParser().parseFromString(response.responseText, 'text/html');
  98. resolve(response);
  99. }
  100. }));
  101. }
  102. }

QingJ © 2025

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