Jump to content

User:Mathwizard1232/monobook.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// This code will change ISBN links to point to the url of your choice,
// instead of Special:Booksources, when you are logged in. 
//
// How to use it:
//
// 1.) Make sure you are logged in, so you can edit your user javascript 
//     page.
//
// 2.) Copy the contents of this page to your user javascript page. This page is 
//     at User:<your username>/monobook.js , where <your username> is replaced by 
//     your user name. For example, my user name is Lunchboxhero so my javascript 
//     page is User:Lunchboxhero/monobook.js .
// 
//     (Every line that starts with "//" is a comment is not necessary for the code 
//     to work and can be discarded.)
//
// 3.) If you want to change the destination URL from amazon.com to another book
//     source, you can do so by changing the value of the book_source_URL.  Go to 
//     [[Wikipedia:Book sources]] and copy the URL of the book source you want to
//     use (right-click or ctrl-click the link, then click "Copy Link Location", 
//     "Copy Target", or similar).  Put the new URL between the quotes next 
//     to book_source_URL, in place of the URL now there.
//
// 4.) Once satisfied with the destination url, and your javascript page saved, you 
//     may have to refresh your browser cache in order for the code to be loaded. 
//     
//     For Mozilla/Safari/Konqueror: hold down Shift while clicking Reload (or 
//     press Ctrl-Shift-R), IE: press Ctrl-F5, Opera: press F5.
//
// The code should now work, as long as you are logged in.
//
// The current magicURL includes Wikimedia's Amazon Referral reference. That means if you click on 
// the ISBN link, go to Amazon and end up buying the book, Wikimedia will earn some small amount
// of money from that sale.  If you don't want to support Wikimedia in this way, but do want ISBNs
// to link to Amazon, replace the current magicURL with "http://www.amazon.com/gp/search?ie=UTF8&field-isbn=MAGICNUMBER"
//
// Good luck, and if you have any questions, leave a comment on the discussion
// page of User:Lunchboxhero/monobook.js . Thanks to Superm401 and drrngravy for their
// improvements.
 
var book_source_URL = "http://www.amazon.com/gp/search?ie=UTF8&field-isbn=MAGICNUMBER&tag=wikipedia08-20";
importScript('User:Lunchboxhero/externISBN.js');

//<pre><nowiki>

// Popups
// [[User:Lupin/popups.js]]

mw.loader.load(
             'https://en.wikipedia.org/w/index.php?title=User:Lupin/popups.js'
             + '&action=raw&ctype=text/javascript&dontcountme=s');

//Functions to manage strings
function Insert(str, pos, newstr)
{
	return (str.substr(0,pos) + newstr + str.substr(pos));
}

function Delete(str, pos, length)
{
	if (pos + length < str.length)
	{
		return (str.substr(0,pos) + str.substr(pos+length));
	}
	else
	{
		return str.substr(0,pos);
	}
}

function DeleteOne(str, pos)
{
	return Delete(str, pos, 1)
}

function replace(str,txt,replace)
{
	while(str.indexOf(txt)!=-1)
	{
		str = str.replace(txt,replace);
	}
	return str;
}

function FindPrev(str, txt, pos)
{
	pos--;
	while (pos > -1)
	{
		var Char = str.substr(pos,1);
		if (Char == txt)
		{
			return pos;
		}
		pos--;
	}
	return -1;
}

//Functions for finding page information
function title()
{
	return document.title.substr(0, document.title.lastIndexOf(' - Wikipedia, the free'));
}

function article()
{
	return document.title.substring(8, document.title.lastIndexOf(' - Wikipedia, the free'));
}

function reversetitle()
{
	var str = article();
	var pos = str.lastIndexOf(' ');
	var last = str.substr(pos+1);
	var first = str.substring(0,pos);
	return last + "," + first;
}

function ImageTitle()
{
	var Title = article();
	for (var i = 0; i < Title.length; i++)
	{
		if ((Title.substr(i,1) == '.') || (Title.substr(i,1) == ' '))
		{
			Title = DeleteOne(Title, i)
			i--
		}
	}
	return Title + ".jpg"
}

//Senator functions
function party()
{
	var endpos = document.editform.wpTextbox1.value.indexOf(" to the United States Senate");
	var startpos = FindPrev(document.editform.wpTextbox1.value, " ", endpos);
	return document.editform.wpTextbox1.value.substring(startpos+1,endpos);
}

function state()
{
	var pos = document.editform.wpTextbox1.value.indexOf('a Senator from ');
	var end = document.editform.wpTextbox1.value.indexOf(';');
	var state = document.editform.wpTextbox1.value.substring(pos+15,end);
	return state;
}

//Functions for adding buttons
function addlilink(tabs, url, name, id, title, key)
{
    var na = document.createElement('a');
    na.href = url;
    na.appendChild(document.createTextNode(name));
    var li = document.createElement('li');
    if(id) li.id = id;
    li.appendChild(na);
    tabs.appendChild(li);
    na.accesskey = key;
    var pref = 'alt-';
    if(((clientPC.indexOf('AppleWebKit')!=-1) && (clientPC.indexOf('spoofer')==-1)) || navigator.userAgent.toLowerCase().indexOf( 'mac' ) != -1 ) pref = 'control-';
    if(clientPC.indexOf('opera')!=-1) pref = 'shift-esc-';
    if(key && title) na.title = title + ' [' + pref + key + ']';
    else if(title) na.title = title;
    else if(key) na.title = '[' + pref + key + ']';
    return li;
}

function addTab(url, name, id, title, key)
{
    var tabs = document.getElementById('p-cactions').getElementsByTagName('ul')[0];
    addlilink(tabs, url, name, id, title, key);
}

//Functions for editing
function addText(text)
{
	document.editform.wpTextbox1.value = document.editform.wpTextbox1.value + text;
}

function addTextTop(text)
{
	document.editform.wpTextbox1.value = text + document.editform.wpTextbox1.value;
}

function addSummary(text)
{
	document.editform.wpSummary.value = text
}

//Functions for modifying page for Senator Conversion
function CongressConvert()
{
	var str = document.editform.wpTextbox1.value;
	var pos = -1;
	while (str.indexOf("Congress", pos) != -1)
	{
		var midpos = str.indexOf("Congress", pos) - 2;
		var startpos1 = str.lastIndexOf("(",midpos);
		var startpos2 = str.lastIndexOf(" ",midpos);
		var startpos;
		if (startpos1 < startpos2)
		{
			startpos = startpos2;
		}
		else
		{
			startpos = startpos1;
		}
		startpos++;
		var currpos = midpos + 10;
		//window.alert("Up to found point: " + str.substring(0,currpos));
		//window.alert(str.substring(startpos,currpos));
		var link = "[[" + str.substring(startpos,midpos) + " United States Congress|" + str.substring(startpos,currpos) + "]]"
		str = Delete(str,startpos,currpos-startpos)
		str = Insert(str,startpos,link)
		pos = startpos + link.length
	}
	document.editform.wpTextbox1.value = str;
}

function Convert(text)
{
	//Add links, convert names
	text = replace(text,"Wis.","Wisconsin");
	text = replace(text,"Md.","Maryland");
	text = replace(text,"Mo.","Missouri");
	text = replace(text,"Wash.","Washington");
	text = replace(text,"Pa.","Pennsylvania");
	//CongressConvert();
	return text;
}

function doStart() //Main function that creates the template for Senators
{
	/*if (document.editform.wpTextbox1.value != "")
	{
		window.alert('Data found, template not added.');
	}
	else
	{*/
		var Bio = document.editform.wpTextbox1.value;
		document.editform.wpTextbox1.value = Convert(Bio);

		//window.alert(article());
		addText('\n[[Image:' +ImageTitle() + '|thumb|right|'+article()+']]' + "\n");
		addText("'''" + article() + "'''" + " was an [[American]] [[politician]]. A [[" + party() + " Party (United States)|" + party() + "]],");
		addText(" he served as a [[United States Senator]] from [[" + state() + "]].\n");
		addText("==Early Life==\n\n==Political Career==\n\n==Sources==\n* {{CongBio|}}\n");
		addText("{{start box}}\n{{U.S. Senator box\n\n")
		addText("|state = " + state() + "\n\n|class = \n|before=[[]]\n|after=[[]]\n|alongside=[[]]\n|years= &ndash; }}\n")
		addText("{{end box}}\n");
		addText("{{US-politician-stub}}\n");
		addText("[[Category: births|" + reversetitle() + "]]\n");
		addText("[[Category: deaths|" + reversetitle() + "]]\n");
		addText("[[Category:United States Senators from " +state() + "|" + reversetitle() + "]]");
		document.editform.wpSummary.value = 'Created for [[WP:USC]]';
		//window.alert('Adding template.');
	//}
}

//Quick proposed deletion
function doProd()
{
	var reason=prompt("Please enter reason for prod.")
	if (reason != null && reason != "")
	{
		addTextTop("{{subst:prod|" + reason + "}}\n\n")
		addSummary("[[WP:PROD|Proposed Deletion]]")
		document.editform.submit()
	}
}

//Functions for making buttons, loading, making flow go where I want at the start
function addStart() 
{
  addTab("Javascript:doStart()", "Start Senator Page", "ca-start", "Creates an outline for a page for a former Senator.", "");
  akeytt();
}

function GoEdit(tag)
{
	location.href = 'http://en.wikipedia.org/w/index.php?title=' + title() + '&action=edit&other=' + tag;
}

function doGoProd()
{
	GoEdit('StartProd');
}

function doGoStart() 
{
	location.href = 'http://en.wikipedia.org/w/index.php?title=' + title() + '&action=edit&other=StartSen';
}

function GoStart() 
{
	addTab("Javascript:doGoStart()", "Start Senator Page", "ca-start", "Creates an outline for a page for a former Senator.", "");
}

function GoProd()
{
	addTab("Javascript:doGoProd()", "Tag Prod", "ca-prod", "Tags article for proposed deletion (for non-controversial)", "");
}

function addProd()
{
	addTab("Javascript:doProd()", "Tag Prod", "ca-prod", "Tags article for proposed deletion (for non-controversial)", "");
}

function CheckStart() //Checks for parameters sent to start various tasks
{
	var end = location.href.substr(location.href.lastIndexOf('=edit'));
	if (end == '=edit')
		return;
	else if (end == '=edit&other=StartSen')
	{
		doStart();
	}
	else if (end == '=edit&other=StartProd')
	{
		doProd();
	}
	else
	{
		//window.alert('Unrecognized command: ' + end);
	}
}

//Loads initial boxes depending on current window
if (document.title.indexOf("Editing ") == 0)
{
	addOnloadHook(addStart);
	addOnloadHook(addProd);
}
else
{
	addOnloadHook(GoProd);
	//addOnloadHook(GoStart);
}

if (document.title.indexOf("Editing ") == 0)
{
	addOnloadHook(CheckStart);
}
//</nowiki></pre>