User:Subh83/JavaScriptTools/utilsCommunications.js
Appearance
From Wikipedia, the free encyclopedia
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump.
This code will be executed when previewing this page.
This code will be executed when previewing this page.
Documentation for this user script can be added at User:Subh83/JavaScriptTools/utilsCommunications.
/**************************************************** * Created by Subhrajit Bhattacharya [[User:Subh83]] * * Licensed under GNU-GPL v3.0 * *****************************************************/ varUserSubh83_utilsCommunications=true; // ================================================================ /*** URL encoding, special character encoding, URL reading, etc. utils ***/ functionXEncodeURIComponent(str){ varret=encodeURIComponent(str); ret=ret.replace(/;/g,"%3B"); ret=ret.replace(/\\/g,"%22"); ret=ret.replace(/'/g,"%27"); returnret; } functionHTMLSpecialCharactersEncode(str){ returnstr.replace(/&/g,"&").replace(/>/g,">").replace(/</g,"<").replace(/"/g,"""); } functionGETparam(name){ varregexS="[\\?&]"+name+"=([^&#]*)"; varregex=newRegExp(regexS); varresults=regex.exec(window.location.href); if(results==null)returnfalse; elsereturnresults[1]; } // ================================================================ /*** XMLHttp request helper functions ***/ // From User:Supadawg/util.js functioncreateXMLHTTP(method,uri,callback,options){ varxmlhttp=window.XMLHttpRequest?newXMLHttpRequest() :window.ActiveXObject?newActiveXObject("Microsoft.XMLHTTP") :null; if(xmlhttp){ xmlhttp.onreadystatechange=callback; xmlhttp.open(method,uri,true); if(options&&options.headers) for(varkeyinoptions.headers) xmlhttp.setRequestHeader(key,options.headers[key]); varbody=null; if(options&&options["body"]) body=options["body"]; xmlhttp.send(body); } returnxmlhttp; } // Wikipedia specific: Fetch the source text of a wiki page varxmlhttpWIKITEXT; varxmlhttpWIKITEXTSent=false; varxmlhttpWIKITEXTDone=false; varFetchWikiTextAndPerformAction_lastReturn=false; functionFetchWikiTextAndPerformAction(pagename,secNum,ActionFun,params,cycles){ // 'ActionFun' needs to be handle to a function that takes 2 parameters. // The first is the wikitext, and the second is a structure containing other parameters. // That is, ActionFun(WikiText, params); // Set 'secNum' to -1 to get full page. cycles=(typeof(cycles)!='undefined')?cycles:50; if(cycles<=0)return; FetchWikiTextAndPerformAction_lastReturn=false; if(!xmlhttpWIKITEXTSent){ varfetchURL=(wgServer+wgScript)+"?action=raw&title="+pagename; if(secNum>=0)fetchURL+="§ion="+secNum; xmlhttpWIKITEXTSent=true; xmlhttpWIKITEXT=createXMLHTTP("GET",fetchURL,function(){xmlhttpWIKITEXTDone=true;}); } if(!xmlhttpWIKITEXTDone||!xmlhttpWIKITEXT||xmlhttpWIKITEXT.readyState!=4) setTimeout(FetchWikiTextAndPerformAction,200,pagename,secNum,ActionFun,params,cycles-1); else{ xmlhttpWIKITEXTSent=false; xmlhttpWIKITEXTDone=false; if(xmlhttpWIKITEXT.status==200){ // Main actions FetchWikiTextAndPerformAction_lastReturn=ActionFun(xmlhttpWIKITEXT.responseText,params); returnret; }else alert("Problem retrieving data - status: "+xmlhttpWIKITEXT.status); } } // ================================================================ /*** For handling cookies ***/ functionsetCookie(name,value){ document.cookie=name+"="+value+"; path=/"; } functiongetCookie(name){ varnameeq=name+"="; varcookies=document.cookie.split(';'); for(vari=0;i<cookies.length;i++){ nameval=cookies[i].split("="); if(nameval[0].replace(/^\s*/,"").replace(/\s*$/,"")==name) returnnameval[1]; } returnnull; } functiondelCookie(name){ setCookie(name,"",-1); }