Järjestelmäviesti:Gadget-util.js
Siirry navigaatioon
Siirry hakuun
Huomautus: Selaimen välimuisti pitää tyhjentää asetusten tallentamisen jälkeen, jotta muutokset tulisivat voimaan.
- Firefox ja Safari: Napsauta Shift-näppäin pohjassa Päivitä, tai paina Ctrl-F5 tai Ctrl-R (⌘-R Macilla)
- Google Chrome: Paina Ctrl-Shift-R (⌘-Shift-R Macilla)
- Internet Explorer ja Edge: Napsauta Ctrl-näppäin pohjassa Päivitä tai paina Ctrl-F5
- Opera: Paina Ctrl-F5.
/* Various random utility functions I find useful. If for some reason you want to
* use these in your code, the following line will import them for you:
importScript('User:Anomie/util.js'); // Linkback: [[:en:User:Anomie/util.js]]
* (Please keep the comment so I can see how many people use this).
Finnish wikipedia notes
* Installed & support: Zache
* Linkclassifier requires this
* Source: http://en.wikipedia.org/w/index.php?title=User:Anomie/util.js
*/
function jsondecode(v){
try {
return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u\n\r\t]/.test(v.replace(/"(\\.|[^\x22\\])*"/g,''))) && eval('('+v+')');
} catch(e){
return null;
}
}
if(!Number.prototype.toHex) Number.prototype.toHex = function(p){
var s=this.toString(16);
var i=s.indexOf('.');
if(i<0) i=s.length;
if(typeof(p)!='undefined') for(p-=i; p>0; p--){
s='0'+s;
}
return s;
}
if(!Number.toHex) Number.toHex = function(n,p){
return Number(n).toHex(p);
}
if(!RegExp.quote){
RegExp.quote = function(s){
return s.replace(/[^\t !\x22#%&\x27,:;<=>@_`~a-z0-9]/ig, RegExp.quote.$replacer);
}
RegExp.quote.$replacer = function(c){
c=c.charCodeAt(0);
return '\\u'+c.toHex(4);
}
}
function api(p, cb){
var uri=mw.config.get("wgServer")+mw.config.get("wgScriptPath")+'/api.php';
var data='format=json';
for(var k in p){
var v=p[k];
if(typeof(v)=='object' && (v instanceof Array))
v=v.map(encodeURIComponent).join('|');
else
v=encodeURIComponent(v);
data+='&'+encodeURIComponent(k)+'='+v;
}
var x = sajax_init_object();
if(!x) return false;
if(typeof(cb)=='function'){
x.open('POST', uri, true);
x.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
x.onreadystatechange=function(){
if(x.readyState!=4) return;
var r=jsondecode(x.responseText);
if(!r) throw new Error("Could not parse response");
cb(r, p);
};
x.send(data);
return true;
} else {
x.open('POST', uri, false);
x.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
x.send(data);
var r=jsondecode(x.responseText);
if(!r) throw new Error("Could not parse response");
return r;
}
}
function rawpage(pg, cb){
var uri=mw.config.get("wgServer")+mw.config.get("wgScriptPath")+'/index.php?action=raw&title='+encodeURIComponent(pg);
var x = sajax_init_object();
if(!x) return false;
if(typeof(cb)=='function'){
x.open('GET', uri, true);
x.onreadystatechange=function(){
if(x.readyState!=4) return;
cb(x.responseText);
};
x.send(null);
return true;
} else {
x.open('GET', uri, false);
x.send(null);
return x.responseText;
}
}
function isClass(n, c){
return n.className.match(new RegExp('(^|\\s)'+c+'(\\s|$)'));
}
function addClass(n, c){
if(!isClass(n,c)) n.className=(n.className=='')?c:(n.className+' '+c);
}
function delClass(n, c){
var cl=n.className;
cl=cl.replace(new RegExp('(^|\\s)'+c+'(\\s|$)', 'g'), ' ');
cl=cl.replace(/\s\s+/g, ' ').replace(/^\s+|\s+$/g, '');
if(n.className!=cl) n.className=cl;
}
if(!Array.prototype.map) Array.prototype.map=function(callback, thisObject){
var l=this.length;
var a=[];
if(typeof(thisObject)=='undefined' || thisObject===null){
for(var i=0;i<l;i++){
a[i]=callback(this[i],i,this);
}
} else {
for(var i=0;i<l;i++){
a[i]=callback.call(thisObject,this[i],i,this);
}
}
return a;
}
if(!Array.map) Array.map=function(obj, callback, thisObject){
return Array.prototype.map.call(obj, callback, thisObject);
}
if(!Array.prototype.forEach) Array.prototype.forEach=function(callback, thisObject){
var l=this.length;
if(typeof(thisObject)=='undefined' || thisObject===null){
for(var i=0;i<l;i++){
callback(this[i],i,this);
}
} else {
for(var i=0;i<l;i++){
callback.call(thisObject,this[i],i,this);
}
}
}
if(!Array.forEach) Array.forEach=function(obj, callback, thisObject){
return Array.prototype.forEach.call(obj, callback, thisObject);
}
if(!Array.prototype.filter) Array.prototype.filter=function(callback, thisObject){
var l=this.length;
var a=[];
if(typeof(thisObject)=='undefined' || thisObject===null){
for(var i=0;i<l;i++){
if(callback(this[i],i,this)) a.push(this[i]);
}
} else {
for(var i=0;i<l;i++){
if(callback.call(thisObject,this[i],i,this)) a.push(this[i]);
}
}
return a;
}
if(!Array.filter) Array.filter=function(obj, callback, thisObject){
return Array.prototype.filter.call(obj, callback, thisObject);
}