User:Zocky/utils.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:Zocky/utils. |
///////////
// Notes
//////////
var active_note, active_note_top, active_note_left;
var mouse_x, mouse_y;
var note_top=100;
function new_note(x,y,w,h,t,content)
{
note_top++;
var note = el("div",
"id", "note_" + note_top,
"class", "toc",
"style","background:#FFFF88; border:solid black 1px; padding:2px; z-index:" + note_top
+ "; position:fixed; display:block; minimum-width:"+w+"px; left:" + x + "px; top:" + y + "px",
"onMouseDown", "active_note=this;",
tx('[ '), el("a","href","javascript:close_note('note_"+note_top+"')",tx("×")), tx(' ] '),
tx('[ '), el("a","href","javascript:toggle_note('note_"+note_top+"')",tx("-")), tx(' ] '),
el("b",t),
el("div",
"id", "note_" + note_top + "_content",
"style", "background:#FFFFCC; minimum-height:"+h+"px; border:solid silver 1px; ",
"onMouseDown" , "return 0",
content
));
note.addEventListener("mousedown", pick_note, false);
add_stuff('globalWrapper',note);
active_note=note;
}
function close_note(n)
{
document.getElementById('globalWrapper').removeChild(document.getElementById(n));
}
function toggle_note(n)
{
var c=document.getElementById(n+"_content");
c.style.display = c.style.display=='none' ? 'block' : 'none';
}
function pick_note(e)
{
if (typeof(active_note)!='object') return;
active_note.style.zIndex = note_top++;
mouse_x = e.clientX; mouse_y = e.clientY;
active_note_top = parseInt(active_note.style.top); active_note_left = parseInt(active_note.style.left);
document.addEventListener("mousemove", drag_note, false);
document.addEventListener("mouseup", drop_note, false);
e.preventDefault();
}
function drag_note(e)
{
var x = e.clientX;
var y = e.clientY;
active_note.style.top = (y - mouse_y + active_note_top) + "px"; active_note.style.left = (x - mouse_x + active_note_left) + "px";
}
function drop_note(e)
{
document.removeEventListener("mousemove", drag_note, false);
document.removeEventListener("mouseup", drop_note, false);
active_note=false;
}
///Get user name
function get_user_name()
{
return document.getElementById("pt-userpage").firstChild.innerHTML;
}
///Make a new option
function new_option(n,v)
{
if (window[n]== undefined) { window[n] = v }
}
//XML searcher by ID
function findDescendantById(node, id)
{
if (node.id == id)
{
return node;
}
var i, c;
for (i = node.firstChild; i != null; i=i.nextSibling) {
c = findDescendantById(i,id);
if (c != null)
return c;
}
return null;
}
//XML searcher by class
function findDescendantByClass(node, cl)
{
if (node.nodeType==1 && node.getAttribute('class') == cl)
{
return node;
}
var i, c;
for (i = node.firstChild; i != null; i=i.nextSibling) {
c = findDescendantByClass(i,cl);
if (c != null)
return c;
}
return null;
}
///////////////
//Easy XML insertion
////////////////
//// el (tagname, arg1,val1,arg2,val2,...,child1,child2...)
function el ()
{
var res;
var i;
if (el.arguments.length>0)
{
res = document.createElement(el.arguments[0]);
i=1;
while (i < el.arguments.length)
{
if ( typeof(el.arguments[i]) == "string" )
{
i++;
if ( typeof(el.arguments[i])== "string" || typeof(el.arguments[i])=="number")
{
res.setAttribute(el.arguments[i-1],el.arguments[i]);
i++;
}
else
{
alert ("el: Argument "+ el.arguments[i-1] +" specified, but no value provided.")
return undef;
}
}
else break;
}
while (i < el.arguments.length)
{
if (typeof(el.arguments[i])=='object')
{
res.appendChild(el.arguments[i]);
i++
}
else
{
alert ("el: Useless argument "+ el.arguments[i-1] +" provided.")
return undef;
}
}
}
else
{
alert ("el: Missing element name.")
return undef;
}
return res;
}
function tx(s)
{
return document.createTextNode(s);
}
function add_stuff(p,e)
{
document.getElementById(p).appendChild(e);
}
/////////////////
// DOWNLOADING //
/////////////////
//////////////
//
// downloader
//
//
function downloader(url) {
// Source: http://jibbering.com/2002/4/httprequest.html
this.http = false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation,
// we can cope with old IE versions.
// and security blocked creation of the objects.
try {
this.http = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
this.http = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
// this.http = false;
}
}
@end @*/
if (! this.http && typeof XMLHttpRequest!='undefined') this.http = new XMLHttpRequest();
this.url = url; this.id=null;
this.lastModified = null;
this.callbackFunction = null;
};
new downloader();
downloader.prototype.send = function (x) {if (!this.http) return null; return this.http.send(x);};
downloader.prototype.abort = function () {if (!this.http) return null; return this.http.abort();};
downloader.prototype.runCallback = function () {this.callbackFunction(this);};
downloader.prototype.getData = function () {if(!this.http) return null; return this.http.responseText;};
downloader.prototype.setTarget = function () {if(!this.http) return null; this.http.open("GET", this.url, true);this.setRequestHeader('Accept','text/html');};
downloader.prototype.start=function () {if(!this.http) return null; return this.http.send(null);};
downloader.prototype.getReadyState=function () {if(!this.http) return null; return this.http.readyState;};
downloader.prototype.setRequestHeader=function (a,b) {if(!this.http) return null; return };
downloader.prototype.getLastModifiedDate=function () {
if(!this.http) return null;
var lastmod=null;
try {
lastmod=this.http.getResponseHeader('Last-Modified');
} catch (err) {}
if (lastmod) return new Date(lastmod);
return null;
}
downloader.prototype.setCallback = function (f) {
if(!this.http) return;
this.http.onreadystatechange = f;
this.callbackFunction = f;
};
function newDownload(url, id, callback) {
var d=new downloader(url);
if (!d.http) return 'ohdear';
d.id=id;
d.setTarget();
var f = function () {
var stctl=1;
if (stctl && d.getReadyState() == 4) {
stctl=0;
d.data=d.getData();
d.lastModified=d.getLastModifiedDate();
callback(d);
}
};
d.setCallback(f);
return d;//d.start();
};
function fakeDownload(url, id, callback, data, lastModified) {
var d=newDownload(url,callback);
d.id=id; d.data=data;
d.lastModified=lastModified;
return callback(d);
};
function startDownload(url, id, callback) {
var d=newDownload(url, id, callback);
if (typeof d == typeof '' ) return d;
return d.start();
};
//
//
// downloader
//
//////////////
//Download a part of a page by ID or class (what) into an element by ID (where)
function downloadChunkById(url, where, what)
{
var callback=function(d)
{
var ch;
var dldoc = document.createElement("div");
var dlwhere= document.getElementById(where);
dldoc.innerHTML=d.data;
try
{
var dlwhat=findDescendantById(dldoc, what);
while (ch=dlwhere.firstChild) {dlwhere.removeChild(ch);}
dlwhere.appendChild(dlwhat);
} catch(e){};
};
return startDownload(url, where, callback);
delete dldoc;
};
function downloadChunkByClass(url, where, what)
{
var callback=function(d)
{
var ch;
var dldoc = document.createElement("div");
var dlwhere= document.getElementById(where);
dldoc.innerHTML=d.data;
while (ch=dlwhere.firstChild) {dlwhere.removeChild(ch);}
document.getElementById(where).appendChild(findDescendantByClass(dldoc, what))
};
return startDownload(url, where, callback);
};