xFetch

12/10/2024, 8:45:56 AM

Stan na 24-12-2024. Zobacz najnowsza wersja.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name          xFetch
// @description   12/10/2024, 8:45:56 AM
// @namespace     Violentmonkey Scripts
// @match         *://*.*/*
// @grant         GM_xmlhttpRequest
// @grant         unsafeWindow
// @run-at        document-idle
// @version       0.0.1
// @author        me
// @license       MIT
// ==/UserScript==
typeOf=a=>a.constructor.name;

function xFetch(url,option,retAsXhr=true) {
  return new Promise((rs,rj)=>{
    const detail={};
    switch ( typeOf(url) ) {
      case 'Request':
        detail.url=url.url;
        if (url?.method) detail.method=url.method; else detail.method='GET';
        if (url?.mod) detail.mod=url.mod;
        if (url?.headers) detail.headers=url.headers;
        if (url?.body)  detail.data=url.body;
        if (url?.referrerPolicy)  detail.referrerPolicy=url.referrerPolicy;
        if (url?.redirect)  detail.redirect=url.redirect;
        if (url?.priority)  detail.priority=url.priority;
        if (url?.keepalive)  detail.keepalive=url.keepalive;
        if (url?.integrity)  detail.integrity=url.integrity;
        if (url?.credentials)  detail.credentials=url.credentials;
        if (url?.referrer)  detail.referrer=url.referrer;
        if (url?.cache)  detail.cache=url.cache;
        // if (url?.responseType) detail.responseType=url.responseType; //'text', 'json', 'blob', 'arraybuffer', 'document';
        detail.responseType='arraybuffer';
        break;

      case 'URL':
      default:
        detail.url=url.toString();
        if (option?.mod) detail.mod=option.mod;
        if (option?.method) detail.method=option.method; else detail.method='GET';
        if (option?.headers) detail.headers=option.headers;
        if (option?.body)  detail.data=option.body;
        if (option?.referrerPolicy)  detail.referrerPolicy=option.referrerPolicy;
        if (option?.redirect)  detail.redirect=option.redirect;
        if (option?.priority)  detail.priority=option.priority;
        if (option?.keepalive)  detail.keepalive=option.keepalive;
        if (option?.integrity)  detail.integrity=option.integrity;
        if (option?.credentials)  detail.credentials=option.credentials;
        if (option?.referrer)  detail.referrer=option.referrer;
        if (option?.cache)  detail.cache=option.cache;
        // if (option?.responseType) detail.responseType=option.responseType; //'text', 'json', 'blob', 'arraybuffer', 'document';
        detail.responseType='arraybuffer';
    }
    retAsXhr ? detail.onload=rs :
    detail.onload=res=>rs(new Response(res.response,{  //return a Response like fetch, but still not working with stream()
      headers:res.headers,
      ok:res.statusText=='OK',
      type:'basic',
      redirected:detail.url!=res.finalUrl,
      status:res.status,
      statusText:res.statusText,
      url:res.finalUrl,
    }));
    detail.onerror=rj;
    GM_xmlhttpRequest(detail);
  })
}

(function (){
  unsafeWindow.xhr=GM_xmlhttpRequest;
  unsafeWindow.xFetch=xFetch;
  unsafeWindow.fetch=(a,b)=>xFetch(a,b,false);
  // window.fetch=(a,b)=>xFetch(a,b,false);
})()