Tampermonkey Requests

Simplified HTTP requests for Tampermonkey scripts.

Este script no debería instalarse directamente. Es una biblioteca que utilizan otros scripts mediante la meta-directiva de inclusión // @require https://update.greasyfork.org/scripts/470000/1227959/Tampermonkey%20Requests.js

Tendrás que instalar una extensión para tu navegador como Tampermonkey, Greasemonkey o Violentmonkey si quieres utilizar este script.

You will need to install an extension such as Tampermonkey to install this script.

Tendrás que instalar una extensión como Tampermonkey o Violentmonkey para instalar este script.

Necesitarás instalar una extensión como Tampermonkey o Userscripts para instalar este script.

Tendrás que instalar una extensión como Tampermonkey antes de poder instalar este script.

Necesitarás instalar una extensión para administrar scripts de usuario si quieres instalar este script.

(Ya tengo un administrador de scripts de usuario, déjame instalarlo)

Tendrás que instalar una extensión como Stylus antes de poder instalar este script.

Tendrás que instalar una extensión como Stylus antes de poder instalar este script.

Tendrás que instalar una extensión como Stylus antes de poder instalar este script.

Para poder instalar esto tendrás que instalar primero una extensión de estilos de usuario.

Para poder instalar esto tendrás que instalar primero una extensión de estilos de usuario.

Para poder instalar esto tendrás que instalar primero una extensión de estilos de usuario.

(Ya tengo un administrador de estilos de usuario, déjame instalarlo)

// ==UserScript==
// @name        Tampermonkey Requests
// @description Simplified HTTP requests for Tampermonkey scripts.
// @version     0.0.4
// @author      大碗宽面wtw
// @homepage    https://github.com/bigbowl-wtw/TampermonkeyRequests/
// @supportURL  https://github.com/bigbowl-wtw/TampermonkeyRequests/issues
// @match       none
// @grant       GM_xmlhttpRequest
// @license     MIT
// @namespace   com.github.bigbowl-wtw
// ==/UserScript==

!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.requests=e():t.requests=e()}(self,(()=>(()=>{"use strict";var t,e={d:(t,o)=>{for(var i in o)e.o(o,i)&&!e.o(t,i)&&Object.defineProperty(t,i,{enumerable:!0,get:o[i]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e),r:t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})}},o={};function i(t,...e){return(Object.assign||function(t,...e){for(let o,i=0,r=e.length;i<r;i++){o=e[i];for(const e in o)Object.prototype.hasOwnProperty.call(o,e)&&(t[e]=o[e])}return t}).call(this,t,...e)}e.r(o),e.d(o,{Session:()=>d,default:()=>y,get:()=>l,post:()=>p,session:()=>f}),function(t){t[t.BadRequest=400]="BadRequest",t[t.Unauthorized=401]="Unauthorized",t[t.Forbidden=403]="Forbidden",t[t.NotFound=404]="NotFound",t[t.MethodNotAllowed=405]="MethodNotAllowed",t[t.RequestTimeout=408]="RequestTimeout",t[t.TooManyRequests=429]="TooManyRequests",t[t.InternalServerError=500]="InternalServerError",t[t.BadGateway=502]="BadGateway",t[t.ServiceUnavailable=503]="ServiceUnavailable"}(t||(t={}));class r{get empty(){return!Object.keys(this.cookie).length}constructor(t){var e;this.never=new Set,this.setCookies(null!==(e=t)&&void 0!==e?e:{})}update(t){i(this.cookie,this.normalizeCookie(t))}updateFromString(t){"string"==typeof t&&(t=[t]),this.update(t)}differenceUpdate(t){let e;e=Array.isArray(t)?t.map(this.stringToEntry):Object.entries(s(t)?t.cookie:t);const o=new Set(Object.keys(this.cookie));e=e.filter((([t])=>!o.has(t))),this.update(Object.fromEntries(e))}deleteFromString(t){"string"==typeof t&&(t=[t]);t.map(this.stringToEntry).map((([t])=>t)).forEach((t=>{this.never.add(t),delete this.cookie[t]}))}setCookies(t){this.cookie=new Proxy(this.normalizeCookie(t),{set:(t,e,o)=>(this.never.has(e)||(t[e]=o),!0)})}toString(){return Object.entries(this.cookie).map((([t,e])=>`${t}=${e}`)).join(";")}parseCookieString(t){return Object.fromEntries(t.map(this.stringToEntry))}normalizeCookie(t){return Array.isArray(t)?this.parseCookieString(t):s(t)?t.cookie:t}stringToEntry(t){return t.slice(0,t.indexOf(";")).split("=")}}function s(t){return"function"==typeof t.update}var n=function(t,e){var o={};for(var i in t)Object.prototype.hasOwnProperty.call(t,i)&&e.indexOf(i)<0&&(o[i]=t[i]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var r=0;for(i=Object.getOwnPropertySymbols(t);r<i.length;r++)e.indexOf(i[r])<0&&Object.prototype.propertyIsEnumerable.call(t,i[r])&&(o[i[r]]=t[i[r]])}return o};class a{constructor(t){if(this.cookie=new r,!t)return void(this.headers={});const{cookie:e}=t,o=n(t,["cookie"]);this.headers=o,e&&this.cookie.update(e)}setFromString(t){const e=t.split("\r\n").map((t=>t.split(":"))).map((([t,e])=>[t.toLowerCase(),e.trim()]));for(const[t,o]of e){const e=this.headers[t];e?("string"==typeof e&&(this.headers[t]=[e]),this.headers[t].push(o)):this.headers[t]=o}return this}update(t){if("function"==typeof t.update)this.cookie.update(t.cookie),this.update(t.headers);else{const e=null!=t?t:{},{cookie:o}=e,r=n(e,["cookie"]);i(this.headers,r),o&&this.cookie.update(o)}}getHeaders(){if(!Object.keys(this.headers).length)return this.cookie.empty?{}:{cookie:this.cookie.toString()};const t={};for(const[e,o]of Object.entries(this.headers))t[e]=o.toString();return Object.assign(Object.assign({},t),{cookie:this.cookie.toString()})}append(t,e){"cookie"===t?this.cookie.update([].concat(e)):this[t]?Array.isArray(t)?this[t].concat(e):this[t]=[e].concat(this[t]):this[t]=e}get(t){return this.headers[t]}}var h=function(t,e,o,i){return new(o||(o=Promise))((function(r,s){function n(t){try{h(i.next(t))}catch(t){s(t)}}function a(t){try{h(i.throw(t))}catch(t){s(t)}}function h(t){var e;t.done?r(t.value):(e=t.value,e instanceof o?e:new o((function(t){t(e)}))).then(n,a)}h((i=i.apply(t,e||[])).next())}))},u=function(t,e){var o={};for(var i in t)Object.prototype.hasOwnProperty.call(t,i)&&e.indexOf(i)<0&&(o[i]=t[i]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var r=0;for(i=Object.getOwnPropertySymbols(t);r<i.length;r++)e.indexOf(i[r])<0&&Object.prototype.propertyIsEnumerable.call(t,i[r])&&(o[i[r]]=t[i[r]])}return o};class c{constructor(t,e,o){if(this.pendings=[],this.url=t,this.method=e,this.finalHeader=new a,o){const{query:t,json:e,data:i,cookie:r,auth:s,headers:n}=o,a=u(o,["query","json","data","cookie","auth","headers"]);this.query=t,this.json=e,this.data=i,this.cookie=r,this.auth=s,this.headers=n,this.others=a}}build(t){return h(this,void 0,void 0,(function*(){return this.buildURL().buildHeaderAndCookie(t).buildAuth(t),this.pendings.concat(t.buildHooks.map((e=>e.call(this,t)))),this.toDetails()}))}buildURL(){return this.parseQuery(this.query),this}buildHeaderAndCookie(t){const e=this.finalHeader.cookie;return t.cookie.empty||e.update(t.cookie),this.cookie&&e.update(this.cookie),this.finalHeader.update(t.headers),this.headers&&this.finalHeader.update(this.headers),this}buildAuth(t){var e,o;return this.pendings.push(null===(e=t.auth)||void 0===e?void 0:e.build(this.finalHeader)),this.pendings.push(null===(o=this.auth)||void 0===o?void 0:o.build(this.finalHeader)),this}buildBody(){if(this.json){const t="application/json";this.finalHeader.update({"Content-Type":t});const e=JSON.stringify(this.json);return this.finalData=e,this}if(this.data){if("string"==typeof this.data)return this.finalData=this.data,this;if(this.data instanceof FormData)return this;const t="application/x-www-form-urlencoded";return this.finalHeader.update({"Content-Type":t}),this.finalData=function(t){if(Object.values(t).some((t=>"function"!=typeof t.toString)))throw new TypeError("value must has `.toString`");return Object.entries(t).map((([t,e])=>`${t}=${encodeURIComponent(e.toString())}`)).join("&")}(this.data),this}return this}toDetails(){return h(this,void 0,void 0,(function*(){yield Promise.all(this.pendings);const t=this.url.toString();this.buildBody();const e=Object.assign({url:t,method:this.method,headers:this.finalHeader.getHeaders()},this.others);return this.finalData&&(e.data=this.finalData),e}))}parseQuery(t){let e;e="string"==typeof this.url?new URL(this.url):this.url,t&&Object.entries(t).filter((([t,e])=>"string"==typeof e)).forEach((([t,o])=>e.searchParams.append(t,o.toString()))),this.url=e}}class d{constructor(){this.buildHooks=[];const t=new a;Object.defineProperty(this,"headers",{get:()=>t,set(e){t.update(e)}}),Object.defineProperty(this,"cookie",{get:()=>t.cookie,set(e){t.cookie.setCookies(e)}})}get(t,e){return h(this,void 0,void 0,(function*(){return this.request("GET",t,e)}))}post(t,e){return h(this,void 0,void 0,(function*(){return this.request("POST",t,e)}))}request(e,o,i){return h(this,void 0,void 0,(function*(){const r=new c(o,e,i);return new Promise(((e,o)=>{i.onload||(r.others.onload=r=>{if(!this.cookie.empty){const t=(new a).setFromString(r.responseHeaders);this.cookie.deleteFromString(t["set-cookie"])}200!==r.status||r.status in t?o(r):e((null==i?void 0:i.responseType)?r.response:r)}),i.onerror||(r.others.onerror=t=>o(t)),r.build(this).then((t=>GM_xmlhttpRequest(t)))}))}))}registerBuildHook(t){this.buildHooks.push(t)}}function l(t,e,o){return(new d).get(t,Object.assign({query:e},o))}function p(t,e){return(new d).post(t,Object.assign({},e))}function f(){return new d}const y={get:l,post:p,session:f};return o})()));