User:Subh83/JavaScriptTools/utilsCommunications.js

/****************************************************
* Created by Subhrajit Bhattacharya [[User:Subh83]] *
* Licensed under GNU-GPL v3.0                       *
*****************************************************/
var UserSubh83_utilsCommunications = true;


// ================================================================
/*** URL encoding, special character encoding, URL reading, etc. utils ***/

function XEncodeURIComponent(str) {
    var ret = encodeURIComponent(str);
    ret = ret.replace(/;/g, "%3B");
    ret = ret.replace(/\\/g, "%22");
    ret = ret.replace(/'/g, "%27");

    return ret;
}

function HTMLSpecialCharactersEncode(str) {
    return str.replace(/&/g, "&amp;").replace(/>/g, "&gt;").replace(/</g, "&lt;").replace(/"/g, "&quot;");
}

function GETparam(name) {
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec(window.location.href);
    if( results == null ) return false;
    else return results[1];
}


// ================================================================
/*** XMLHttp request helper functions ***/

// From User:Supadawg/util.js
function createXMLHTTP( method, uri, callback, options ) {
    var xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest()
                                        : window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP")
                                                               : null;
    if (xmlhttp) {
        xmlhttp.onreadystatechange = callback;
        xmlhttp.open( method, uri, true );

        if (options && options.headers)
            for (var key in options.headers)
                xmlhttp.setRequestHeader(key, options.headers[key]);

        var body = null;
        if ( options && options["body"] )
            body = options["body"];
        xmlhttp.send( body );
    }
    return xmlhttp;
}


// Wikipedia specific: Fetch the source text of a wiki page
var xmlhttpWIKITEXT;
var xmlhttpWIKITEXTSent = false;
var xmlhttpWIKITEXTDone = false;

var FetchWikiTextAndPerformAction_lastReturn = false;
function FetchWikiTextAndPerformAction(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) {
        var fetchURL = (wgServer+wgScript)+"?action=raw&title="+pagename;
        if (secNum >= 0) fetchURL += "&section="+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);
            return ret;
        } else 
            alert("Problem retrieving data - status: " + xmlhttpWIKITEXT.status);
    }
}

// ================================================================
/*** For handling cookies ***/

function setCookie(name, value) {
    document.cookie = name + "=" +  value + "; path=/";
}

function getCookie(name) {
    var nameeq = name + "=";
    var cookies = document.cookie.split(';');
    for(var i=0; i < cookies.length; i++) {
        nameval = cookies[i].split("=");
        if ( nameval[0].replace(/^\s*/, "").replace(/\s*$/, "") == name)
            return nameval[1];
    }
    return null;
}

function delCookie(name) {
    setCookie(name, "", -1);
}

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.