User:DavidCane/common.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. |
![]() | The accompanying .css page for this skin is at User:DavidCane/common.css. |
importScript('User:Gary King/nominations viewer.js'); // [[Wikipedia:Nominations Viewer]]
importScript("User:Smith609/toolbox.js");
importScript('User:AndyZ/peerreviewer.js'); //[[User:AndyZ/peerreviewer.js]]
// [[User:Lupin/popups.js]]
document.write('<script type="text/javascript" src="'
+ 'http://en.wikipedia.org/w/index.php?title=User:Lupin/popups.js'
+ '&action=raw&ctype=text/javascript&dontcountme=s"></script>');
// This will add an [edit] link at the top of all pages except preview pages and the main page
// by User:Pile0nades
// Add an [edit] link to pages
addOnloadHook(function () {
// if this is preview page or generated page, stop
if(
document.getElementById("wikiPreview") ||
document.getElementById("histlegend") ||
document.getElementById("difference") ||
document.getElementById("watchdetails") ||
document.getElementById("ca-viewsource") ||
window.location.href.indexOf("/wiki/Special:") != -1
) {
if(window.location.href.indexOf("&action=edit§ion=0") != -1) {
document.getElementById("wpSummary").value = "/* Intro */ ";
}
return;
};
// get the page title
var pageTitle = wgPageName;
// create div and set innerHTML to link
var divContainer = document.createElement("div");
divContainer.innerHTML = '<div class="editsection">[<a href="/w/index.php?title='+pageTitle+'&action=edit§ion=0" title="Edit section: '+pageTitle+'">edit intro</a>]</div>';
// insert divContainer into the DOM below the h1
if(window.location.href.indexOf("&action=edit") == -1) {
document.getElementById("content").insertBefore(divContainer, document.getElementsByTagName("h1")[0]);
}
});
// [[User:Ais523/topcontrib.js]]
// This script color-codes lines according to who has the top contribution for a page.
//<pre><nowiki>
addOnloadHook(function () {
if((location.href.indexOf("Special:Contributions")!=-1||
location.href.indexOf("Special%3AContributions")!=-1)
&&location.href.indexOf("&ais523")==-1&&location.href.indexOf("?ais523")==-1)
// the ais523 in the line above is meant to be ais523, not your username; it's
// to avoid a clash with a couple of other scripts I've written. Feel free to
// expand it to avoid clashes with other contribs-manipulating scripts, though.
{
var i,li,a;
li=document.getElementById("bodyContent");
li=li.getElementsByTagName("li");
i=-1;
a=new Array();
while(++i<li.length)
{
var s,t;
var html = li[i].innerHTML;
var match = html.match(/"\/wiki\/([^"]*)"/);
if (!match)
match = html.match(/"\/w\/index.php\?title=([^"]*)&redirect=no"/);
t = match[1];
var spans = li[i].getElementsByTagName("span");
var topspanfound = false;
for (var j = 0; j < spans.length; j++)
{
if (spans[j].className == "mw-uctop")
{
topspanfound = true;
break;
}
}
s = topspanfound ? "#CCCCFF" : "#FFCCCC";
if(a[t]!=undefined) s=(a[t]=="#CCCCFF"?"#CCFFFF":"#FFFFCC"); else a[t]=s;
li[i].innerHTML="<SPAN STYLE='background-color:"+s+"'>"+li[i].innerHTML+"</SPAN>";
}
}
});
//</nowiki></pre>
//<pre>
//This function adds a tab which, when clicked, extracts data from an infobox and
//populates the fields of the Persondata template. See the talk page for more details.
//
//To use this function add {{subst:js|User:Dr pda/persondata.js}} to your monobook.js
//
function format_name(x){
NAME = x.substr(x.indexOf('=')+1);
NAME = NAME.replace(/'''?/g,'');
NAME = NAME.replace(/^\s*/g,'');
NAME = NAME.replace(/\s*$/g,'');
var comma = NAME.indexOf(',');
var start = (comma == -1) ? NAME.length : comma;
var lastSpace = NAME.lastIndexOf(' ',start);
if(lastSpace != -1){
NAME = NAME.substring(lastSpace,start)+', '+NAME.substring(0,lastSpace)+NAME.substring(start);
}
return NAME;
}
function suggestPersonData(){
var text = document.getElementById('wpTextbox1').value;
//do nothing if article already contains persondata
if(text.match(/persondata/i)) return;
var template = '';
var NAME = '';
var ALTERNATIVE_NAMES = '';
var SHORT_DESCRIPTION = '';
var PLACE_OF_BIRTH = '';
var DATE_OF_BIRTH = '';
var PLACE_OF_DEATH = '';
var DATE_OF_DEATH = '';
//handle cases where the template name doesn't contain infobox
text = text.replace(/{{NFL player/i,'{{NFL player infobox');
if(text.match(/Infobox/i)){
start = text.lastIndexOf('{{',text.indexOf('nfobox'));
stop = text.indexOf('}}',start);
next = text.indexOf('{{',start+1)
//Correctly handle other templates used within the infobox
while (next < stop && text.indexOf('{{',start+1)!= -1){
next = text.indexOf('{{',stop+1);
stop = text.indexOf('}}',stop+1);
}
template = text.substring(start,stop);
//Remove references, birth/death date and age templates
template = template.replace(/<ref.*(\/ref>|$)/gm,'');
template = template.replace(/{{(?:[Bb]irth|[Dd]eath) date(?: and age)?\s?(\|\s?[md]f\s?=\s?ye?s?)?\|\s?(\d{1,4})\|\s?(\d{1,2})\|\s?(\d{1,2}).*}}/g,"$2-$3-$4");
template = template.replace(/\[\[Image:Flag.*px\]\]/ig,'');
template = template.replace(/{{flagicon.*}}/ig,'');
template = template.replace(/<\/?small>/ig,'');
var firstpar = template.indexOf('|');
template = template.substr(firstpar+1);
var birthplace_in_born = false;
var array = template.split(/(\n\s*\||\|\s*\n)/);
for (x=0;x<array.length;x++){
if(array[x].match(/subject_name/i)||array[x].match(/^\s*\bname\b/i)||array[x].match(/fullname/i)){
NAME = format_name(array[x]);
}
if(array[x].match(/playername/i)||array[x].match(/birth_?name/i)){
ALTERNATIVE_NAMES = (ALTERNATIVE_NAMES == '') ? format_name(array[x]):ALTERNATIVE_NAMES + ';' + format_name(array[x]);
}
else if(array[x].match(/date_of_birth/i)||array[x].match(/dateofbirth/i)||array[x].match(/birthdate/i)||array[x].match(/birth_date/i)||array[x].match(/date of birth/i)||array[x].match(/datebirth/i)){
DATE_OF_BIRTH = array[x].substr(array[x].indexOf('=')+1);
}
else if(array[x].match(/place_of_birth/i)||(array[x].match(/origin/i)&&birthplace_in_born==false)||array[x].match(/birthplace/i)||array[x].match(/placebirth/i)||array[x].match(/location/i)||array[x].match(/placeofbirth/i)||array[x].match(/birth_place/i)||array[x].match(/place of birth/i)){
PLACE_OF_BIRTH = array[x].substr(array[x].indexOf('=')+1);
}
else if(array[x].match(/born/i)){
var temp = array[x].indexOf('<br');
if(temp != -1){
DATE_OF_BIRTH = array[x].substring(array[x].indexOf('=')+1,temp);
PLACE_OF_BIRTH = array[x].substr(array[x].indexOf('>',temp)+1);
birthplace_in_born = true;
}
else{
DATE_OF_BIRTH = array[x].substring(array[x].indexOf('=')+1);
}
}
else if(array[x].match(/lived/i)){
var temp = array[x].substr(array[x].indexOf('=')+1);
temp = temp.replace(/born\s*/i,'');
var dash = temp.search(/–\s*|-\s*/);
if(dash >=0){
DATE_OF_BIRTH = temp.substring(0,dash);
DATE_OF_DEATH = temp.substring(temp.indexOf(' ',dash)+1);
}
else{
DATE_OF_BIRTH = temp;
}
}
else if(array[x].match(/cityofbirth/i)){
var temp = array[x].substr(array[x].indexOf('=')+1);
temp = temp.match(/.*/);
PLACE_OF_BIRTH = (PLACE_OF_BIRTH == '') ? temp : temp + ' ,' + PLACE_OF_BIRTH;
}
else if(array[x].match(/countryofbirth/i)){
PLACE_OF_BIRTH = (PLACE_OF_BIRTH == '') ? array[x].substr(array[x].indexOf('=')+1) : PLACE_OF_BIRTH + ',' + array[x].substr(array[x].indexOf('=')+1);
}
else if(array[x].match(/date_of_death/i)||array[x].match(/dateofdeath/i)||array[x].match(/deathdate/i)||array[x].match(/death_date/i)||array[x].match(/date of death/i)||array[x].match(/datedeath/i)){
DATE_OF_DEATH = array[x].substr(array[x].indexOf('=')+1);
}
else if(array[x].match(/place_of_death/i)||array[x].match(/deathplace/i)||array[x].match(/placeofdeath/i)||array[x].match(/death_place/i)||array[x].match(/place of death/i)||array[x].match(/placedeath/i)){
PLACE_OF_DEATH = array[x].substr(array[x].indexOf('=')+1);
}
else if(array[x].match(/died/i)){
var temp = array[x].indexOf('<br');
if(temp != -1){
DATE_OF_DEATH = array[x].substring(array[x].indexOf('=')+1,temp);
PLACE_OF_DEATH = array[x].substr(array[x].indexOf('>',temp)+1);
}
else{
DATE_OF_DEATH = array[x].substring(array[x].indexOf('=')+1);
}
}
else if(array[x].match(/cityofdeath/i)){
var temp = array[x].substr(array[x].indexOf('=')+1);
temp = temp.match(/.*/);
PLACE_OF_DEATH = (PLACE_OF_DEATH == '') ? temp : temp + ' ,' + PLACE_OF_DEATH;
}
else if(array[x].match(/countryofdeath/i)){
PLACE_OF_DEATH = (PLACE_OF_DEATH == '') ? array[x].substr(array[x].indexOf('=')+1) : PLACE_OF_DEATH + ',' + array[x].substr(array[x].indexOf('=')+1);
}
else if(array[x].match(/occupation/i)||array[x].match(/\bfield\b\s*=/i)||array[x].match(/office/i)){
SHORT_DESCRIPTION = array[x].substr(array[x].indexOf('=')+1);
SHORT_DESCRIPTION = SHORT_DESCRIPTION.replace(/\s*<br\s*\/?>\s*/g,', ');
}
}
//more tidy up
DATE_OF_BIRTH = DATE_OF_BIRTH.replace(/\(age? \d*\)/i,'');
DATE_OF_DEATH = DATE_OF_DEATH.replace(/\(age? \d*\)/i,'');
}
if(NAME == ''){
var pagename = wgPageName.replace(/_/g,' ');
pagename = pagename.replace(/\(.*\)/g,' ');
NAME = format_name(pagename);
}
if(DATE_OF_BIRTH=='' && text.match(/Category:\d* births/)){
var catbirth = text.search(/Category:\d* births/);
DATE_OF_BIRTH = text.substring(catbirth+9,text.indexOf('births')-1);
}
if(DATE_OF_DEATH=='' && text.match(/Category:\d* deaths/)){
var catdeath = text.search(/Category:\d* deaths/);
DATE_OF_DEATH = text.substring(catdeath+9,text.indexOf('deaths')-1);
}
var persondata = "\n\n<!-- Metadata: see [[Wikipedia:Persondata]] -->\n{{Persondata\n|NAME="+NAME+"\n|ALTERNATIVE NAMES="+ALTERNATIVE_NAMES+"\n|SHORT DESCRIPTION="+SHORT_DESCRIPTION+"\n|DATE OF BIRTH="+DATE_OF_BIRTH+"\n|PLACE OF BIRTH="+PLACE_OF_BIRTH+"\n|DATE OF DEATH="+DATE_OF_DEATH+"\n|PLACE OF DEATH="+PLACE_OF_DEATH+"\n}}\n\n";
var insertPosition = text.indexOf('[[Category:')-1;
if(text.match('{{DEFAULTSORT')) insertPosition = text.indexOf('{{DEFAULTSORT')-1;
if(insertPosition != -2){
document.getElementById('wpTextbox1').value = text.substr(0,insertPosition)+persondata+text.substr(insertPosition+1);
}
else{
alert('This article does not belong to any categories! Consider adding some.');
document.getElementById('wpTextbox1').value = text+persondata;
}
document.getElementById('wpSummary').value += ' adding [[WP:PDATA|persondata]] using [[User talk:Dr pda/persondata.js|User:Dr pda/persondata.js]]';
document.getElementById('wpDiff').click();
}
function togglePersondata() {
var element = document.getElementById('persondata');
if (element.style.display != 'block'){
element.style.display = 'block';
}
else{
element.style.display = 'none';
}
}
addOnloadHook(function () {
if(!document.forms.editform){
if (document.getElementById('persondata') != null){
addPortletLink('p-cactions', 'javascript:togglePersondata()', 'show/hide persondata', 'ca-pdata', 'Show/hide persondata metadata', '', '');
}
}
else{
if (wgNamespaceNumber == 0 && document.getElementById('wpTextbox1').value.match(/persondata/i) == null){
addPortletLink('p-cactions', 'javascript:suggestPersonData()', 'add persondata', 'ca-pdata', 'add persondata metadata', '', '');
}
}
});
//</pre>
//<pre>
//This function adds a link to the toolbox which, when clicked, displays the size of the page
//and the size of the prose. See the talk page for more details.
//
//To use this function add {{subst:js|User:Dr pda/prosesize.js}} to your monobook.js
//
function loadXMLDocPassingTemplate(url,handler, page)
{
// branch for native XMLHttpRequest object
if (window.XMLHttpRequest) {
var req = new XMLHttpRequest();
}
// branch for IE/Windows ActiveX version
else if (window.ActiveXObject) {
var req = new ActiveXObject("Microsoft.XMLHTTP");
}
if (req) {
req.onreadystatechange = function () {handler(req, page)};
req.open("GET", url, true);
req.send("");
}
}
function getWikiText(req, page) {
// only if req shows "loaded"
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
// ...processing statements go here...
response = req.responseXML.documentElement;
var rev = response.getElementsByTagName('rev');
if(rev.length > 0){
result = rev[0].getAttribute('size');
if(result > 10240){
result = (result/1024).toFixed(0)+' kB';
}
else{
result = result+' B';
}
wiki_value = document.createElement("li");
wiki_value.id = "wiki-size";
wiki_value.innerHTML = '<b>Wiki text: </b>'+result;
var output = document.getElementById("document-size-stats");
prose_value = document.getElementById("prose-size");
output.insertBefore(wiki_value,prose_value);
}
else{
//alert("There was a problem using the Wikipedia Search to find the wiki text size\nEither the search is not working or the correct article did not appear on the first page of results");
wiki_value = document.createElement("li");
wiki_value.id = "wiki-size";
wiki_value.innerHTML = '<b>Wiki text: </b>Problem getting wiki text size';
var output = document.getElementById("document-size-stats");
prose_value = document.getElementById("prose-size");
output.insertBefore(wiki_value,prose_value);
}
} else {
alert("There was a problem retrieving the XML data:\n" +
req.statusText);
}
}
}
function getFileSize(req, page) {
// only if req shows "loaded"
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
// ...processing statements go here...
var fsize = req.responseText.length;
window.status = fsize;
var total_value = document.createElement("li");
total_value.id = "total-size";
total_value.innerHTML='<b>File size: </b>'+(fsize/1024).toFixed(0)+' kB';
var output = document.getElementById("document-size-stats");
var prose_html_value = document.getElementById("prose-size-html");
output.insertBefore(total_value,prose_html_value);
} else {
alert("There was a problem retrieving the XML data:\n" +
req.statusText + "\n(" + url + ")");
}
}
}
function getLength(id){
var textLength = 0;
for(var i=0;i<id.childNodes.length; i++){
if(id.childNodes[i].nodeName == '#text'){
textLength += id.childNodes[i].nodeValue.length;
}
else if(id.childNodes[i].id == 'coordinates'){
//special case for {{coord}} template
}
else{
textLength += getLength(id.childNodes[i]);
}
}
return textLength;
}
function getRefMarkLength(id,html){
var textLength = 0;
for(var i=0;i<id.childNodes.length; i++){
if(id.childNodes[i].className == 'reference'){
textLength += (html)? id.childNodes[i].innerHTML.length : getLength(id.childNodes[i]);
}
}
return textLength;
}
function getDocumentSize(){
contentDivName = '';
if(skin == 'monobook' || skin == 'chick' || skin == 'myskin' || skin == 'simple'){
contentDivName = 'bodyContent';
}
else if (skin == 'modern'){
contentDivName = 'mw_contentholder';
}
else if (skin == 'standard' || skin == 'cologneblue' || skin == 'nostalgia'){
contentDivName = 'article';
}
else{
//fallback case; the above covers all currently existing skins
contentDivName = 'bodyContent';
}
//Same for all skins if previewing page
if(wgAction == 'submit') contentDivName = 'wikiPreview';
var bodyContent = document.getElementById(contentDivName);
if(document.getElementById("document-size-stats")){
//if statistics already exist, turn them off and remove highlighting
var output = document.getElementById("document-size-stats");
var oldStyle = output.className;
var pList = bodyContent.getElementsByTagName("p");
for(var i=0;i<pList.length; i++){
if(pList[i].parentNode.id == contentDivName) pList[i].style.cssText = oldStyle;
}
output.parentNode.removeChild(output);
var header = document.getElementById("document-size-header");
header.parentNode.removeChild(header);
}
else{
var output = document.createElement("ul");
output.id = "document-size-stats";
var prose_html_value = document.createElement("li");
prose_html_value.id = "prose-size-html";
output.appendChild(prose_html_value);
var ref_html_value = document.createElement("li");
ref_html_value.id = "ref-size-html";
output.appendChild(ref_html_value);
var prose_value = document.createElement("li");
prose_value.id = "prose-size";
output.appendChild(prose_value);
output.className = bodyContent.getElementsByTagName("p").item(0).style.cssText;
var ref_value = document.createElement("li");
ref_value.id = "ref-size";
output.appendChild(ref_value);
var dummy = document.getElementById("siteSub");
dummy.parentNode.insertBefore(output, dummy.nextSibling);
var header = document.createElement("span");
header.id = "document-size-header";
header.innerHTML = '<br/>Document statistics: <small><i>(See <a href="http://en.wikipedia.org/wiki/User_talk:Dr_pda/prosesize.js">here</a> for details.)<i></small>';
dummy.parentNode.insertBefore(header,output);
//File size not well defined for preview mode or section edit
if(wgAction != 'submit'){
//If browser supports document.fileSize property (IE)
if(document.fileSize){
var total_value = document.createElement("li");
total_value.id = "total-size";
total_value.innerHTML='<b>File size: </b>'+(document.fileSize/1024).toFixed(0)+' kB';
output.insertBefore(total_value,prose_html_value);
}
else{
loadXMLDocPassingTemplate(location.pathname,getFileSize,'')
}
}
//Get size of images only if browser supports filesize property (IE)
var iList = bodyContent.getElementsByTagName("img");
if(iList.length >0 && iList[0].fileSize){
//Get size of images included in document
var image_size = 0;
var first_magnify = true;
for (var i=0;i<iList.length; i++){
var im = iList[i];
if(im.getAttribute("src").indexOf("magnify-clip.png") != -1){
if(first_magnify){
image_size += im.fileSize*1;
first_magnify = false;
}
}
else{
image_size += im.fileSize*1;
}
}
var image_value = document.createElement("li");
image_value.id = "image-size";
image_value.innerHTML='<b>Images: </b>'+(image_size/1024).toFixed(0)+' kB';
output.appendChild(image_value);
}
//Calculate prose size and size of reference markers ([1] etc)
var pList = bodyContent.getElementsByTagName("p");
prose_size = 0;
prose_size_html = 0;
refmark_size = 0;
refmark_size_html = 0;
word_count = 0;
for(var i=0;i<pList.length; i++){
var para = pList[i];
if(para.parentNode.id == contentDivName){
prose_size += getLength(para);
prose_size_html += para.innerHTML.length;
refmark_size += getRefMarkLength(para,false);
refmark_size_html += getRefMarkLength(para,true);
word_count += para.innerHTML.replace(/(<([^>]+)>)/ig,"").split(' ').length
para.style.cssText = "background-color:yellow";
}
}
if((prose_size-refmark_size)>10240){
prose_value.innerHTML='<b>Prose size (text only): </b>'+((prose_size-refmark_size)/1024).toFixed(0)+' kB ('+word_count+' words) "readable prose size"';
}
else{
prose_value.innerHTML='<b>Prose size (text only): </b>'+(prose_size-refmark_size)+' B ('+word_count+' words) "readable prose size"';
}
if((prose_size_html-refmark_size_html)>10240){
prose_html_value.innerHTML='<b>Prose size (including all HTML code): </b>'+((prose_size_html-refmark_size_html)/1024).toFixed(0)+' kB';
}
else{
prose_html_value.innerHTML='<b>Prose size (including all HTML code): </b>'+(prose_size_html-refmark_size_html)+' B';
}
//Calculate size of references (i.e. output of <references/>)
var rList = bodyContent.getElementsByTagName("ol");
var ref_size = 0;
var ref_size_html = 0;
for (var i=0; i<rList.length; i++){
if(rList[i].className == "references"){
ref_size = getLength(rList[i]);
ref_size_html = rList[i].innerHTML.length;
}
}
if((ref_size+refmark_size)>10240){
ref_value.innerHTML='<b>References (text only): </b>'+((ref_size+refmark_size)/1024).toFixed(0)+' kB';
}
else{
ref_value.innerHTML='<b>References (text only): </b>'+(ref_size+refmark_size)+' B';
}
if((ref_size_html+refmark_size_html)>10240){
ref_html_value.innerHTML='<b>References (including all HTML code): </b>'+((ref_size_html+refmark_size_html)/1024).toFixed(0)+' kB';
}
else{
ref_html_value.innerHTML='<b>References (including all HTML code): </b>'+(ref_size_html+refmark_size_html)+' B';
}
//get correct name of article from wikipedia-defined global variables
var pageNameUnderscores = wgPageName;
var pageNameSpaces = pageNameUnderscores.replace(/_/g,' ')
//if page is a permalink, diff, etc don't try to search
if(!location.pathname.match('/w/index.php')){
//Get revision size from API
var searchURL = wgScriptPath + '/api.php?action=query&prop=revisions&rvprop=size&format=xml&revids=' + wgCurRevisionId;
loadXMLDocPassingTemplate(searchURL,getWikiText,pageNameSpaces);
}
else if(wgAction == 'submit'){
//Get size of text in edit box
result = document.getElementById('wpTextbox1').value.length;
if(result > 10240){
result = (result/1024).toFixed(0)+' kB';
}
else{
result = result+' B';
}
wiki_value = document.createElement("li");
wiki_value.id = "wiki-size";
wiki_value.innerHTML = '<b>Wiki text: </b>'+result;
var output = document.getElementById("document-size-stats");
prose_value = document.getElementById("prose-size");
output.insertBefore(wiki_value,prose_value);
}
}
}
addOnloadHook(function () {
if(wgAction == 'edit' || (wgAction == 'submit' && document.getElementById('wikiDiff')) ){
addPortletLink('p-tb', 'javascript:alert("You need to preview the text for the prose size script to work in edit mode.")', 'Page size', 't-page-size', 'Calculate page and prose size', '', '');
document.getElementById("t-page-size").firstChild.style.cssText = "color:black;"
}
else if(wgAction == 'view' || wgAction == 'submit' || wgAction == 'purge'){
addPortletLink('p-tb', 'javascript:getDocumentSize()', 'Page size', 't-page-size', 'Calculate page and prose size', '', '');
}
});
//</pre>