xFetch

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

2024-12-24 या दिनांकाला. सर्वात नवीन आवृत्ती पाहा.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

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