Jump to content

User:Quarl/util.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Quarl (talk | contribs) at 05:27, 23 January 2006 (args). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// User:Quarl/util.js - miscellaneous utility functions for Wikipedia user scripts

// quarl 2006-01-09 initial version

// <pre><nowiki>

/////////////////////////////////////////////////////////////
// STRING UTILITY FUNCTIONS

function trimspaces(s) {
    if (!s) return s;
    s = s.replace(/^ +/,'');
    s = s.replace(/ +$/,'');
    return s;
}

function string_quote_escape(str) {
    if (!str) return str;
    return "'" + str.replace(/\'/g, '\\\'').replace(/\%27/g, '\\\'') + "'";
}

// wiki article name escaping
function wpaescape(s) {
    // Need to escape '+'; don't escape ':'
    return escape(s.replace(/ /g,'_')).replace(/[+]/g, '%2B').replace(/%3A/,':');
}

function url_getpath(s) {
    return s.replace(/^http:\/\/[^/]+/, '');
}

////////////////////////////////////////////////////////////
// DOM UTILITY FUNCTIONS
function getElementsByClass(searchClass,node,tag) {
/* This script and many more are available free online at
The JavaScript Source :: http://javascript.internet.com
Created by: Dustin Diaz :: http://www.dustindiaz.com/ */
  var classElements = new Array();
  if (node == null)
    node = document;
  if (tag == null)
    tag = '*';
  var els = node.getElementsByTagName(tag);
  var elsLen = els.length;
  var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
  for (i = 0, j = 0; i < elsLen; i++) {
    if (pattern.test(els[i].className) ) {
      classElements[j] = els[i];
      j++;
    }
  }
  return classElements;
}

function add_before(node, newnode) {
    node.parentNode.insertBefore(newnode, node);
    return newnode;
}

function add_after(node, newnode) {
    node.parentNode.insertBefore(newnode, node.nextSibling);
    return newnode;
}

function findHref(href) {
    href = escape(href);
    var links=document.links;
    for(i=0;i<links.length;++i) {
        // unescape and reescape to ensure canonical escaping
        if (escape(unescape(links[i].href)) == href) return links[i];
    }
    return null;
}

function insertNode(node, newNode) {
    if (!node) return null;

    node.parentNode.replaceChild(newNode, node);
    newNode.appendChild(node);
    return newNode;
}

function hookEventObj(obj, hookName, hookFunct) {
    if (!obj) return;

    if (obj.addEventListener)
        obj.addEventListener(hookName, hookFunct, false);
    else if (obj.attachEvent)
        obj.attachEvent("on" + hookName, hookFunct);
}

// AJAX functions

function HTTPClient() {
  var http;
  if(window.XMLHttpRequest) {
    http = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    try {
      http = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        http = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        http = false;
      }
    }
  }
  return http;
}

function asyncDownloadXML(url, callback) {
    var xmlhttp = HTTPClient();
    if (!xmlhttp) return null;
    // add optional arguments
    for (int i = 2; i+1 < arguments.length; i += 2) {
        xmlhttp[arguments[i]] = arguments[i+1];
    }
    xmlhttp.open("GET", url, true);
    xmlhttp.overrideMimeType('text/xml');
    // using onload instead of onreadystatechange allows multiple asynchronous requests
    xmlhttp.onload = function(event) { 
        var req = event.target;
        if (req.readyState == 4) callback(req);
    };
    xmlhttp.send(null);
    return xmlhttp;
}

//

// </nowiki></pre>