HttpRequest

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

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

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.gf.qytechs.cn/scripts/405144/815221/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://gf.qytechs.cn/scripts/405144-httprequest/code/HttpRequest.js
  12. //
  13. // @require https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js
  14. //
  15. // ==/UserScript==
  16.  
  17. /* jshint esnext: true */
  18. /* globals globals $, jQuery, SimpleCache */
  19. const HttpRequest = (() => {
  20. const responsesCache = new SimpleCache({ keyExtractor: (httpRequest) => [ httpRequest.method, httpRequest.url, httpRequest.headers, httpRequest.data, httpRequest.params ].join() });
  21.  
  22. const urlWithParams = (url, paramsObject) => {
  23. const params = Object.entries(paramsObject).map(([key, value]) => key + '=' + value).join('&');
  24. return (url.length > 0) ? url + '?' + params : params;
  25. };
  26.  
  27. return class HttpRequest {
  28. constructor({ method, url = window.location.href, onLoad = () => {}, onError = () => {}, headers = '', data = '', keepInCacheTimoutMs = 10 * 60000, params = {} } = {}) {
  29. Object.assign(this, { method, url, onLoad, onError, headers, data, keepInCacheTimoutMs });
  30. }
  31.  
  32. send() {
  33. const method = this.method.toUpperCase();
  34. let url = this.url;
  35. let headers = this.headers;
  36. let data = this.data;
  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. this.onError(response);
  52. return;
  53. }
  54. this.onLoad(response);
  55. };
  56. const onerror = this.onError;
  57.  
  58. switch (method) {
  59. case 'GET':
  60. if (this.params) {
  61. url = urlWithParams(url, this.params);
  62. }
  63. break;
  64. case 'POST':
  65. case 'PUT':
  66. Object.assign(headers, { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' });
  67. if (this.params) {
  68. data = urlWithParams(data, this.params);
  69. }
  70. break;
  71. }
  72.  
  73. return GM_xmlhttpRequest({ method, url, onload, onerror: this.onError, headers: this.headers, data });
  74. }
  75.  
  76. isCached() {
  77. return responsesCache.has(this);
  78. }
  79.  
  80. getFromCache() {
  81. return responsesCache.get(this);
  82. }
  83. };
  84. })();
  85.  
  86. class HttpRequestHtml extends HttpRequest {
  87. constructor({ url, method = "GET", onSuccess = () => {}, onError, headers, data, keepInCacheTimoutMs } = {}) {
  88. super({ method, url, onLoad: (response) => {
  89. if (response.status == 200) {
  90. response.responseHtml = new DOMParser().parseFromString(response.responseText, 'text/html');
  91. onSuccess(response);
  92. }
  93. }, onError, headers, data, keepInCacheTimoutMs });
  94. }
  95. }

QingJ © 2025

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