Greasy Fork

Shined up real nice.

Light AJAX plugin

Light AJAX plugin by IvanSkvortsov

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @require https://greasyfork.org/scripts/32001-light-ajax-plugin/code/Light%20AJAX%20plugin.js?version=209692

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// ==UserScript==
// @name           Light AJAX plugin
// @description    Light AJAX plugin by IvanSkvortsov
// @description:ru Упрощенная версия AJAX плагина от IvanSkvortsov
// @include        *://*
// @author         IvanSkvortsov
// @date           2017.08.04
// @version        1.0.0
// @grant          none
// ==/UserScript==

(function(){
	var queue = [], requestInProgress = false, queueCleaned = true;
	function createXHttp()
	{
		if( window.XMLHttpRequest )
			return new XMLHttpRequest();
		else if( window.ActiveXObject ){
			var types = [
				'Msxml3.XMLHTTP',
				'Msxml2.XMLHTTP.6.0',
				'Msxml2.XMLHTTP.3.0',
				'Msxml2.XMLHTTP',
				'Microsoft.XMLHTTP',
			];
			for( let i = 0; i < types.length; ++i )
			{
				try{
					return new ActiveXObject(types[i]);
				}catch(error){}
			}
			console.error("[createXHttp] can't create ActiveXObject");
		}else{
			console.error("[createXHttp] can't create xhttp object");
		}
		return null;
	}
	function getXHttpRequest(){
		if( !(queue.length > 0) )
			return null;
		var request = queue[0];
		request.method = request.method || 'GET';
		if( request.method.toUpperCase() === 'GET' && request.data && request.data !== '' )
		{
			request.url += '?' + request.data;
			request.data = '';
		}
		request.async = (typeof request.async !== 'boolean' ? true : request.async );
		return request;
	}
	function handleXHttpEvent( type, xhttp, request, event ){
		var response;
		if( request[type] )
		{
			response = xhttp;
			response.lengthComputable = xhttp.lengthComputable || event.lengthComputable || false;
			response.loaded = xhttp.loaded || event.loaded || 0;
			response.total = xhttp.total || event.total || 0;
			response.url = xhttp.responseURL || request.url;
			
			request[type].call( response, response.responseText );
			if( type === 'onerror' || type === 'onload' ||
				(type === 'onreadystatechange' && response.readyState == 4 && response.status == 200 && !request.onload ) )
			{
				requestInProgress = false;
				request.delay = request.delay > 20 ? request.delay : 20;
				setTimeout( XHttpRequest, request.delay );
			}
		}
	}
	function initXHttpEvents( xhttp, request ){
		var types = ['onabort', 'onerror', 'onload', 'onloadend', 'onloadstart', 'onprogress',
			'onreadystatechange', 'ontimeout',];
		function addXHttpEventListener(type){
			if( request[type] )
				xhttp[type] = function XHttpEvents(event){handleXHttpEvent(type, xhttp, request, event);};
		}
		types.forEach( addXHttpEventListener );
	}
	function initXHttp( xhttp, request ){
		initXHttpEvents( xhttp, request );
		for( let key in request.headers )
			xhttp.setRequestHeader( key, request.headers[key] );
	}
	function XHttpRequest(){
		var request, xhttp;
		request = getXHttpRequest();
		if( request && (requestInProgress === false || request.async === true) && queue.length > 0 )
		{
			queue.shift();
			requestInProgress = true;
			xhttp = createXHttp();
			initXHttp( xhttp, request );
			xhttp.open( request.method, request.url, request.async );
			xhttp.send( request.data || null );
		}
	}
	function constructXHttpRequest( url, settings ){
		var request = createNewObject(settings);
		request.url = url;
		return request;
	}
	function createNewObject( obj ){
		if( !obj )
			return {};
		var new_obj = {}, key, val;
		for( key in obj )
		{
			val = obj[key];
			if( Object.prototype.hasOwnProperty.call(obj, key) && val )
				new_obj[key] = val;
		}
		return new_obj;
	}
	window.ISL = window.ISL || {};
	// AJAX API
	ISL.ajaxClean = function(){
		queue.length = 0;
		requestInProgress = false;
		queueCleaned = true;
	};
	ISL.ajaxLength = function(){ return queue.length; };
	ISL.ajaxAddXHR = function(){
		var request, url, settings;
		switch( arguments.length )
		{
			case 0:
				return;
			case 1:
				request = arguments[0];
				if( request.length )
					request.forEach( function(req){ queue.push(req);} );
				else
					queue.push(request);
				break;
			case 2:
				url = arguments[0];
				settings = arguments[1];
				if( url.length )
					url.forEach( function(u){queue.push(constructXHttpRequest(u, settings));});
				else
					queue.push( constructXHttpRequest(url, settings) );
				break;
			default:
				console.error("[ajaxAddXHR] invalid number of arguments");
				return;
		}
		queueCleaned = false;
	};
	ISL.ajax = function(){
		this.ajaxAddXHR(arguments);
		XHttpRequest();
	};
})();