User:EpochFail/WPAPIHandler.js

/**
 * Wikipedia API Handler
 *
 * A simple object for interacting with Wikipedia's API.
 */
function WPAPIHandler(){
}

WPAPIHandler.requests = new Array();
WPAPIHandler.timeoutSeconds = REQUESTTIMEOUT;

 
/**
 * Perform Request
 *
 * Uses a callback function to perform a request to the WP api.
 *
 * @param	params	An object with members reflecting the parameters to be used when calling the API
 * @param	func	The function to be called with the results of the request to the API
 * @param	args	A list of arguements to pass to the function along with the result of the request
 */
WPAPIHandler.performGET = function(params, successFun, args, errorFun){
	paramString = "?foo=lol"
	for(key in params){
		paramString += "&" + key + "=" + escape(params[key])
	}
	
	url = "http://en.wikipedia.org/w/api.php" + paramString
	
	request = this.createRequest();
	if(!request){
		errorFun("Your browser does not support the ability to make requests to the Wikipedia database.");
		return;
	}
	request.open("GET", url, true);
	request.setRequestHeader("Pragma", "cache=yes");
	request.setRequestHeader("Cache-Control", "no-transform");
	request.successFun = successFun
	request.errorFun = errorFun
	request.args = args
	request.onreadystatechange = function() {
		if (this.readyState != 4)
			return;
		if (this.status != 200){
			this.errorFun("The Wikipedia server responded with an error: " + this.status + " " + this.statusText + ": " + this.responseText);
			return;
		}
		clearTimeout(this.timeoutRef);
		this.successFun(this.responseText, this.args);
		return;
	}
	request.send(null)
	currentRequestId = this.requests.length
	this.requests[currentRequestId] = request
	request.timeoutRef = setTimeout("WPAPIHandler.timeout(" + currentRequestId + ")", this.timeoutSeconds)
}


WPAPIHandler.timeout = function(requestId){
	request = this.requests[requestId];
	request.errorFun("Request to Wikipedia database timed out.");
	try{
		request.abort();
	}catch(e){
		//then don't abort I guess 
	}
}


 
 
/**
 * Perform Request
 *
 * Uses a callback function to perform a request to the WP api.
 *
 * @param	params	An object with members reflecting the parameters to be used when calling the API
 * @param	func	The function to be called with the results of the request to the API
 * @param	args	A list of arguements to pass to the function along with the result of the request
 */
WPAPIHandler.performPOST = function(params, successFun, args, errorFun){
	paramString = "foo=lol"
	for(key in params){
		paramString += "&" + key + "=" + this.escape(params[key])
	}
	url = "http://en.wikipedia.org/w/api.php"
	
	request = this.createRequest();
	if(!request){
		errorFun("Your browser does not support the ability to make requests to the Wikipedia database.");
		return;
	}
	request.open("POST", url, true);
	request.setRequestHeader("Pragma", "cache=yes");
	request.setRequestHeader("Cache-Control", "no-transform");
	request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	request.setRequestHeader("Content-length", paramString.length);
	request.setRequestHeader("Connection", "close");
	request.successFun = successFun
	request.errorFun = errorFun
	request.args = args
	request.onreadystatechange = function() {
		if (this.readyState != 4)
			return;
		if (this.status != 200){
			this.errorFun("The Wikipedia server responded with an error: " + this.status + " " + this.statusText + ": " + this.responseText);
			return;
		}
		clearTimeout(this.timeoutRef);
		this.successFun(this.responseText, this.args);
		return;
	}
	request.send(paramString)
	currentRequestId = this.requests.length
	this.requests[currentRequestId] = request
	request.timeoutRef = setTimeout("WPAPIHandler.timeout(" + currentRequestId + ")", this.timeoutSeconds)
}
 
/**
 * Create Request
 *
 * Generates an appropriate XMLHttpRequest object for the browser being
 * used or raises an exception.
 */
WPAPIHandler.createRequest = function() {
	var requestObject;
	try {
		requestObject = new XMLHttpRequest();
	} catch (e) {
		try {
			requestObject=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				requestObject=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (oc) {
				requestObject=null;
			}
		}
	}
 
	return requestObject;
}
 
WPAPIHandler.escape = function(string){
	resultString = escape(string);
	return resultString.replace(/\+/, "%2B")
}

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.