http

LEORChn JavaScript HTTP Lib

25.04.2020 itibariyledir. En son verisyonu görün.

Bu script direkt olarak kurulamaz. Başka scriptler için bir kütüphanedir ve meta yönergeleri içerir // @require https://update.greasyfork.org/scripts/401997/797848/http.js

Bu betiği kurabilmeniz için Tampermonkey, Greasemonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Userscripts gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

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

Bu komut dosyasını yüklemek için bir kullanıcı komut dosyası yöneticisi uzantısı yüklemeniz gerekecek.

(Zaten bir kullanıcı komut dosyası yöneticim var, kurmama izin verin!)

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.

(Zateb bir user-style yöneticim var, yükleyeyim!)

function http(){
	var pointer = 0,
		method, url, formed, dofun, dofail, onprogress;
	arr(arguments).forEach(function(e){
		switch(pointer){
			case 0:
				e = e.split(' ', 2); // 允许在第一个参数中用空格将 http-method 与 url 隔开,而省去 引号+逗号+引号 的麻烦
				method = e[0].toUpperCase();
				if(e.length > 1){
					pointer++; // 偏移到下一个
					e = e[1];
				}else break;
			case 1: url = e; break;
			case 2:
				if(e instanceof Function){ // 允许不添加 http-body 而直接撰写行为。
					pointer++; // 偏移到下一个
				}else{
					formed = e;
					break;
				}
			case 3: dofun = e; break;
			case 4: dofail = e; break;
			case 5: onprogress = e;
		}
		pointer++;
	});
	var x = 'ActiveXObject' in window?
		new ActiveXObject("Microsoft.XMLHTTP"):
		new XMLHttpRequest();
	if(location.protocol.includes('https'))
		url=url.replace('^http:', 'https:');
	x.open(method, url, true);
	x.timeout=60000;
	x.responseType="text"; // IE要求先open才能设置timeout和responseType
	x.onload=dofun;
	x.ontimeout=x.onerror= dofail? dofail: null;
	x.onprogress=onprogress;
	if(formed)
		x.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'); // TODO: 暂不明确IE是否适用
	x.send(formed?formed:'');
}
function httpj(){
	var a = arr(arguments);
	if(!a[0].includes(' ')){
		a[0] += ' ' + a[1];
		a.splice(1, 1);
	}
	if(a[1] instanceof Function) a.splice(1, 0, null); // 如果 method+url 之后不是 String 而是 Function,判断为 onSuccess,将一个 null 插入到 formed
	while(a.length < 5) a[a.length] = null; // 在剩下的空位填满null
	// 始终转换为如此格式:0=method+url, 1=formed, 2=onSuccess, 3=onFail, 4=onProgress
	http(a[0], a[1], // method+url, formed
		function(){
			var j,
				stat = this.status,
				resp = this.responseText;
			try{
				j = eval('('+ (resp || '{}') +')');
			}catch(e){}
			if(Array.isArray(j)) // JSON Array
				j = { stat:stat, data:j }
			else if(j instanceof Object) // JSON Object
				j.stat = stat;
			else // Nothing, text/plain
				j = { stat:stat, data:resp }
			a[2](j);
		},
		a[3], a[4]); // fail, progress
}

// edit: 2020-3-20 2:21
// edit: 2020-4-26 3:50