Super Light AJAX plugin

Super Light AJAX plugin by IvanSkvortsov

目前為 2017-08-04 提交的版本,檢視 最新版本

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.cn-greasyfork.org/scripts/32009/209806/Super%20Light%20AJAX%20plugin.js

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name           Super Light AJAX plugin
// @description    Super Light AJAX plugin by IvanSkvortsov
// @description:ru Супер упрощенная версия AJAX плагина от IvanSkvortsov
// @include        *://*
// @author         IvanSkvortsov
// @date           2017.08.04
// @version        1.0.2
// @grant          none
// ==/UserScript==
(function(){
	var lqueue = [], lrequestInProgress = false, lqueueCleaned = true;
	function XHttpRequestLight(){
		var request = lqueue[0], xhttp = null, i, key,
			activeXs = ['Msxml3.XMLHTTP', 'Msxml2.XMLHTTP.6.0', 'Msxml2.XMLHTTP.3.0', 'Msxml2.XMLHTTP', 'Microsoft.XMLHTTP',];
		if(!( request && (lrequestInProgress === false || request.async === true) && lqueue.length > 0 ))
			return;
		// handle request:
		request.method = request.method || 'GET';
		if( request.method.toUpperCase() === 'GET' && request.data ){
			request.url += '?' + request.data;
			request.data = null;
		}
		request.async = typeof request.async === 'boolean' ? request.async : true;
		// createXmlHttpRequest:
		if( window.XMLHttpRequest )
			xhttp = new XMLHttpRequest();
		else if( window.ActiveXObject ){
			for( i = 0; i < activeXs.length; ++i ){
				try{xhttp = new ActiveXObject(activeXs[i]);}
				catch(error){continue;}
				break;
			}
			if( !xhttp ){
				console.error("[XHttpRequestLight] can't create ActiveXObject");
				return;
			}
		}else{
			console.error("[XHttpRequestLight] can't create XmlHttpRequest object");
			return;
		}
		xhttp.open( request.method, request.url, request.async );
		lrequestInProgress = true;
		lqueue.shift();
		for( key in request.headers )
			xhttp.setRequestHeader( key, request.headers[key] );
		xhttp.url = request.url;
		var events = ['onabort', 'onerror', 'onload', 'onloadend', 'onloadstart', 'onprogress', 'onreadystatechange', 'ontimeout',];
		// handle XmlHttpRequest events
		events.forEach(function(type){
			if( request[type] ){
				xhttp[type] = function(event){
					xhttp.lengthComputable = event.lengthComputable || false;
					xhttp.loaded = event.loaded || 0;
					xhttp.total = event.total || 0;
					request[type].call(xhttp, xhttp.responseText);
					if( type === 'onerror' || type === 'onload' || (type === 'onreadystatechange' && xhttp.readyState == 4 && xhttp.status == 200 && !request.onload)){
						lrequestInProgress = false;
						request.delay = request.delay > 20 ? request.delay : 20;
						setTimeout( XHttpRequestLight, request.delay );
					}
				};
			}
		});
	}
	window.ISL = window.ISL || {};
	// AJAX API
	ISL.lajaxAddXHR = function(){
		var request;
		switch(arguments.length){
			case 0:
				return;
			case 1:
				request = arguments[0];
				if( request.length )
					request.forEach( function(req){ lqueue.push(req);} );
				else
					lqueue.push(request);
				break;
			default:
				console.error("[lajaxAddXHR] invalid number of arguments: " + arguments.length);
				return;
		}
		lqueueCleaned = false;
	};
	ISL.lajax = function(){
		this.lajaxAddXHR(arguments);
		XHttpRequestLight();
	};
	ISL.lajaxClean = function(){
		lqueue.length = 0;
		lrequestInProgress = false;
		lqueueCleaned = true;
	};
	ISL.lajaxLength = function(){ return lqueue.length; };
})();