User:Quarl/util.js
Appearance
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. |
![]() | Documentation for this user script can be added at User:Quarl/util. |
// 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 trim_lines(s) {
return s.replace(/^\n+/, '').replace(/\n+$/, '');
}
function string_quote_escape(str) {
if (!str) return str;
return "'" + str.replace(/\'/g, '\\\'').replace(/\%27/g, '\\\'') + "'";
}
// wiki article name escaping
function wpaescape(s) {
// encodeURIComponent is better than 'escape' for unicode chars;
// it also escapes '+'.
// Don't escape ':'
return encodeURIComponent(s.replace(/ /g,'_')).replace(/%3A/g,':').replace(/%2F/g,'/');
}
wpaencode = wpaescape;
function wpadecode(s) {
return decodeURIComponent(s).replace(/_/g,' ');
}
wpaunescape = wpadecode;
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 createHref(href, title, inner) {
var a = document.createElement('a');
a.href = href;
a.title = title;
a.innerHTML = inner;
return a;
}
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);
}
function copyArray(a) {
var r = new Array();
for (var i=0; i < a.length; i++)
r[i] = a[i];
return r;
}
// 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, props) {
var req = HTTPClient();
if (!req) return null;
// add optional arguments
if (props) {
for (var k in props) {
req[k] = props[k];
}
}
req.open("GET", url, true);
req.overrideMimeType('text/xml');
// using onload instead of onreadystatechange allows multiple asynchronous requests
// TODO: since we now have access to 'req' as a variable, we could change back.
// Is there any advantage to using onreadystatechange?
req.onload = function(event) {
var req = event.target;
if (req.readyState == 4) callback(req);
};
req.send(null);
return req;
}
function buildParams(paramArray) {
var params = '';
for (k in paramArray) {
params += '&' + k + '=' + encodeURIComponent(paramArray[k]);
}
params = params.replace(/^&/,'');
return params;
}
function asyncPostXML(url, parameters, callback, props) {
var req = HTTPClient();
if (!req) return null;
if (typeof parameters != 'string') parameters = buildParams(parameters);
// add optional arguments
if (props) {
for (var k in props) {
req[k] = props[k];
}
}
req.open("POST", url, true);
req.overrideMimeType('text/xml');
req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
req.setRequestHeader("Content-length", parameters.length);
req.setRequestHeader("Connection", "close");
req.onload = function(event) {
var req = event.target;
if (req.readyState == 4) callback(req);
};
req.send(parameters);
return req;
}
//
// </nowiki></pre>