xFetch

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

Από την 24/12/2024. Δείτε την τελευταία έκδοση.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

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.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==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);
})()