Vés al contingut

Usuari:Joancreus/topaz.js

De la Viquipèdia, l'enciclopèdia lliure

Nota: Després de desar, heu de netejar la memòria cau del navegador per veure els canvis. En la majoria de navegadors amb Windows o Linux, premeu Ctrl+F5 o bé premeu Shift i cliqueu el botó "Actualitza" (Ctrl i "Actualitza" amb Internet Explorer). Vegeu més informació i instruccions per a cada navegador a Viquipèdia:Neteja de la memòria cau.

var topaz = new Object();
 
/****************************************  topaz.util  ****************************************/
 
topaz.util = {
getobj:function(id) {
  return document.getElementById ?
             document.getElementById(id) :
             document.all[id];
},
time:function() {
  return((new Date()).getTime()/1000);
},
add:function(parent, tag, attr) {
  var el = document.createElement(tag);
  if (attr) {
    for (key in attr) {
      el[key] = attr[key];
    }
  }
  return parent.appendChild(el);
},
add2:function(parent, el0, tag, attr) {
  var el = document.createElement(tag);
  if (attr) {
    for (key in attr) {
      el[key] = attr[key];
    }
  }
  return parent.insertBefore(el, el0);
},
fullescape:function(text) {
  return escape(text).replace(/\+/g,"%2B");
},
mousebtnmap:{
  ns:[null,1,3,2],
  ie:[null,1,2,null,3]
},
xmlhttpreq:function() {
  if (window.XMLHttpRequest) {
    xmlhttpobj = new XMLHttpRequest()
  } else {
    try {
      xmlhttpobj = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttpobj = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
        xmlhttpobj = null;
      }
    }
  }
  return xmlhttpobj;
},
cookie:{
  noexpire:(function(){
    var d = new Date();
    d.setTime(d.getTime()+(365*24*60*60*1000));
    return d.toGMTString();
  })(),
 
  expire:(function(){
    var d = new Date();
    d.setTime(d.getTime()-1);
    return d.toGMTString();
  })(),
 
  get:function(name) {
    name += "-"+wgUserName;
    var cl = document.cookie.split(/;\s*/);
    for (var i=0; i<cl.length; i++) {
      var curc = cl[i].split(/\=/);
      if (curc[0] == name) {
        return(curc[1]);
      }
    }
    return null;
  },
 
  set:function(name, value) {
    name += "-"+wgUserName;
    document.cookie = name + "=" + escape(value) +
        "; expires=" + topaz.util.cookie.noexpire +
        "; path=/";
  },
 
  del:function(name) {
    name += "-"+wgUserName
    document.cookie = name+"=; expires="+topaz.util.coookie.expire+"; path=/";
  }
}
};
 
 
/*****************************************  topaz.comm  *****************************************/
 
topaz.comm = {
doreq:function(url,postdata,headers) {
  var req = topaz.util.xmlhttpreq();
  req.open(postdata?"POST":"GET", url, false);
  for(header in headers) {
    req.setRequestHeader(header,headers[header]);
  }
  req.send(postdata||null);
  return req;
}
};
 
 
/****************************************  topaz.wputil  ****************************************/
 
topaz.wputil = {
username:function() {
  return wgUserName
  //abans: topaz.util.getobj("pt-userpage").childNodes[0].childNodes[0].nodeValue;
},
iseditpage:function() {
  return document.title.indexOf("Editing ")==0;
},
pagename:function() {
  var iseditpage = topaz.wputil.iseditpage();
  return document.title.substr(iseditpage?8:0,document.title.indexOf(" - ")-(iseditpage?8:0));
},
addsidepanel:function(id,label) {
  var div = topaz.util.add(topaz.util.getobj("column-one"), "div", {className:"portlet",id:id, title: "panell afegit amb els Viquiestris"});
  topaz.util.add(div, "H5").innerHTML = label;
  topaz.util.add(topaz.util.add(div, "div", {className:"pBody"}), "ul");
},
addsidepanelbutton:function(id,label,targhref) {
  var li = topaz.util.add((topaz.util.getobj(id).getElementsByTagName("ul"))[0],"li");
  with(topaz.util.add(li,"a")) {
    innerHTML = label;
    href = targhref;
    title = label+": enllaç afegit amb els Viquiestris"
  }
  return li;
},
addsidepanelbutton2:function(id,label,targhref) {
  var li = topaz.util.add2((topaz.util.getobj(id).getElementsByTagName("ul"))[0],((topaz.util.getobj(id).getElementsByTagName("ul"))[0].getElementsByTagName("li"))[0],"li");
  with(topaz.util.add(li,"a")) {
    innerHTML = label;
    href = targhref;
  }
  return li;
},
setpagecontent:function(pagename,content,summary,watch) {
  var prelimresp = topaz.comm.doreq("/w/index.php?title="+pagename+"&action=submit");
  var edittime = prelimresp.responseText.match(/\<input type='hidden' value="(\d+)" name="wpEdittime" \/\>/)[1];
  var edittoken = prelimresp.responseText.match(/\<input type="hidden" value="([^"]+)" name="wpEditToken" \/\>/)[1];
  content = content.replace(/\&lt\;/gi, "<");
  content = content.replace(/\&gt\;/gi, ">");
  content = content.replace(/\&quot\;/gi, "\"");
  content = content.replace(/\&amp\;/gi, "%26");
  content = content.replace(/\&mdash\;/gi, "—");
  content = content.replace(/\&ndash\;/gi, "-");
  content = content.replace(/\&/gi, "%26");
  content = content.replace(/\%20\;/gi, "%20");
  content = content.replace(/\+/gi, "%2B")
  topaz.comm.doreq("/w/index.php?title="+pagename+"&action=submit",
      "wpEdittime="+edittime+
      "&wpEditToken="+topaz.util.fullescape(edittoken)+
      "&wpTextbox1="+content+
      "&wpSummary="+summary+
      (watch?"&wpWatchthis=on":"")+
      "&wpSection=&wpSave=Save+page",
      {"Content-Type":"application/x-www-form-urlencoded"});
},
getpagecontent:function (pagename) {
  var req = topaz.comm.doreq("/w/index.php?title="+pagename+"&action=edit");
  var content = req.responseText.match(/\<textarea[\w\W]*?\>([\w\W]*?)\<\/textarea\>/)[1];
  content = content.replace(/\&lt\;/gi, "<");
  content = content.replace(/\&gt\;/gi, ">");
  content = content.replace(/\&quot\;/gi, "\"");
  content = content.replace(/\&amp\;/gi, "%26");
  content = content.replace(/\&mdash\;/gi, "—");
  content = content.replace(/\&ndash\;/gi, "-");
  content = content.replace(/\&/gi, "%26");
  content = content.replace(/\%20\;/gi, "%20");
  content = content.replace(/\+/gi, "%2B")
  return content;
},
getpagecontentserver:function (server, pagename) {
  var req = topaz.comm.doreq(server + "/w/index.php?title="+pagename+"&action=edit");
  var content = req.responseText.match(/\<textarea[\w\W]*?\>([\w\W]*?)\<\/textarea\>/)[1];
  content = content.replace(/\&lt\;/gi, "<");
  content = content.replace(/\&gt\;/gi, ">");
  content = content.replace(/\&quot\;/gi, "\"");
  content = content.replace(/\&amp\;/gi, "%26");
  content = content.replace(/\&mdash\;/gi, "—");
  content = content.replace(/\&ndash\;/gi, "-");
  content = content.replace(/\&/gi, "%26");
  content = content.replace(/\%20\;/gi, "%20");
  content = content.replace(/\+/gi, "%2B")
  return content;
},
addpagecontent:function(pagename,addContent,summary,watch) {
  var newContent = '';
  var prelimresp = topaz.comm.doreq("/w/index.php?title="+pagename+"&action=submit");
  var edittime = prelimresp.responseText.match(/\<input type='hidden' value="(\d+)" name="wpEdittime" \/\>/)[1];
  var edittoken = prelimresp.responseText.match(/\<input type="hidden" value="([^"]+)" name="wpEditToken" \/\>/)[1];
  var req = topaz.comm.doreq("/w/index.php?title="+pagename+"&action=edit");
  var oldContent = req.responseText.match(/\<textarea[\w\W]*?\>([\w\W]*?)\<\/textarea\>/)[1];
  oldContent = oldContent.replace(/\&lt\;/gi, "<");
  oldContent = oldContent.replace(/\&gt\;/gi, ">");
  oldContent = oldContent.replace(/\&quot\;/gi, "\"");
  oldContent = oldContent.replace(/\&amp\;/gi, "%26");
  oldContent = oldContent.replace(/\&mdash\;/gi, "—");
  oldContent = oldContent.replace(/\&ndash\;/gi, "-");
  oldContent = oldContent.replace(/\&/gi, "%26");
  oldContent = oldContent.replace(/\%20\;/gi, "%20");
  oldContent = oldContent.replace(/\+/gi, "%2B")
 
  addContent = addContent.replace(/\&lt\;/gi, "<");
  addContent = addContent.replace(/\&gt\;/gi, ">");
  addContent = addContent.replace(/\&quot\;/gi, "\"");
  addContent = addContent.replace(/\&amp\;/gi, "%26");
  addContent = addContent.replace(/\&mdash\;/gi, "—");
  addContent = addContent.replace(/\&ndash\;/gi, "-");
  addContent = addContent.replace(/\&/gi, "%26");
  addContent = addContent.replace(/\%20\;/gi, "%20");
  addContent = addContent.replace(/\+/gi, "%2B")
 
  newContent = oldContent + addContent;
 
  topaz.comm.doreq("/w/index.php?title="+pagename+"&action=submit",
      "wpEdittime="+edittime+
      "&wpEditToken="+topaz.util.fullescape(edittoken)+
      "&wpTextbox1="+ newContent +
      "&wpSummary="+ summary +
      (watch?"&wpWatchthis=on":"")+
      "&wpSection=&wpSave=Save+page",
      {"Content-Type":"application/x-www-form-urlencoded"});
},
readpagecontent:function (pagename) {
  var req = topaz.comm.doreq("/w/index.php?title="+pagename);
  var content = req.responseText.match(/\<textarea[\w\W]*?\>([\w\W]*?)\<\/textarea\>/)[1];
  content = content.replace(/\&lt\;/gi, "<");
  content = content.replace(/\&gt\;/gi, ">");
  content = content.replace(/\&quot\;/gi, "\"");
  content = content.replace(/\&amp\;/gi, "%26");
  content = content.replace(/\&mdash\;/gi, "—");
  content = content.replace(/\&ndash\;/gi, "-");
  content = content.replace(/\&/gi, "%26");
  content = content.replace(/\%20\;/gi, "%20");
  content = content.replace(/\+/gi, "%2B")
  return content;
},
getspecialpagecontent:function (pagename) {
  var req = topaz.comm.doreq("/w/index.php?title="+pagename+"&action=raw");
  var content = req.responseText.match(/\<ol start=\'\d\d?\' class=\'special\'\>([\w\W]*?)\<\/ol\>/)[1];
  content = content.replace(/\&lt\;/gi, "<");
  content = content.replace(/\&gt\;/gi, ">");
  content = content.replace(/\&quot\;/gi, "\"");
  content = content.replace(/\&amp\;/gi, "%26");
  content = content.replace(/\&mdash\;/gi, "—");
  content = content.replace(/\&ndash\;/gi, "-");
  content = content.replace(/\&/gi, "%26");
  content = content.replace(/\%20\;/gi, "%20");
  content = content.replace(/\+/gi, "%2B")
  return content;
}
};