User:Susanarb/common.js
Jump to navigation
Jump to search
Note: After saving, changes may not occur immediately. Click here to learn how to bypass your browser's cache.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Cmd-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (Cmd-Shift-R on a Mac)
- Internet Explorer: Hold Ctrl while clicking Refresh, or press Ctrl-F5
- Opera: Clear the cache in Tools → Preferences
For details and instructions about other browsers, see Wikipedia:Bypass your cache.
Code that you insert on this page could contain malicious content capable of compromising your account. If you are unsure whether code you are adding to this page is safe, you can ask at the central discussion page, Scriptorium. The code will be executed when previewing this page under some skins, including Monobook. You can in the interim if you wish to refresh the content sooner under another skin. |
The accompanying .css page for this skin can be added at User:Susanarb/common.css. |
/* global $, mw, pathoschild */
/**
* Add several tools useful when proofreading pages in the Page: namespace.
* @see https://en.wikisource.org/wiki/Wikisource:TemplateScript
*/
mw.loader.load('//en.wikisource.org/w/index.php?title=MediaWiki:TemplateScript/proofreading.js&action=raw&ctype=text/javascript');
/**
* Add several tools for editing typography like diacritics and letter case.
* @see https://en.wikisource.org/wiki/Wikisource:TemplateScript
*/
mw.loader.load('//en.wikisource.org/w/index.php?title=MediaWiki:TemplateScript/typography.js&action=raw&ctype=text/javascript');
/**
* TemplateScript adds configurable templates and scripts to the sidebar, and adds an example regex editor.
* @see https://meta.wikimedia.org/wiki/TemplateScript
* @update-token [[File:pathoschild/templatescript.js]]
*/
// <nowiki>
$.ajax('//tools-static.wmflabs.org/meta/scripts/pathoschild.templatescript.js', { dataType:'script', cache:true }).then(function() {
/*********
** Register scripts
*********/
pathoschild.TemplateScript.add([
// stuff I routinely do at the start (Page ns only)
{ name: 'hws', script: hws, forNamespaces: 104 },
{ name: 'hwe', script: hwe, forNamespaces: 104 },
// stuff I only do via shortcuts; if I could create these shortcuts without adding them to the sidebar, I would.
{ name: 'center', script: makeCenter, accessKey: 'c' },
{ name: 'right', script: makeRight, accessKey: 'r' },
{ name: 'SIC [?]', script: sic, accessKey: '/' },
// stuff I do at the end (Page ns only)
{ name: 'uc', script: uc, forNamespaces: 104 },
// stuff I can do any time
{ name: 'author (surname first)', script: surnameFirst, accessKey: ',' }
]);
/*********
** Define scripts
*********/
/**
* Convert text to title case.
*/
function _titlecase(text) {
// split text into individual words and examine them one by one
var textArray = text.toLowerCase().split(" ");
for (var i in textArray) {
switch(textArray[i]) {
case "a":
case "an":
case "and":
case "as":
case "at":
case "but":
case "by":
case "etcetera":
case "etc.":
case "for":
case "from":
case "in":
case "nor":
case "of":
case "o'":
case "on":
case "or":
case "the":
case "to":
case "with":
case "versus":
case "vs.":
case "v.":
case "yet":
break; // don't capitalise articles, "to" as part of an infinitive, prepositions or short conjunctions
default: // capitalise everything else
textArray[i] = textArray[i].substring(0,1).toUpperCase() + textArray[i].substring(1,textArray[i].length);
break;
}
}
// capitalise first word regardless
textArray[0] = textArray[0].substring(0,1).toUpperCase() + textArray[0].substring(1,textArray[0].length);
// capitalise last word regardless
var last = textArray.length-1;
textArray[last] = textArray[last].substring(0,1).toUpperCase() + textArray[last].substring(1,textArray[last].length);
// reconstruct title
var titleCase="";
for (i in textArray) {
titleCase += textArray[i];
if (i < last) titleCase += " ";
}
return titleCase;
}
function surnameFirst() {
var editbox = document.getElementsByName('wpTextbox1')[0];
editbox.focus();
var selStart = editbox.selectionStart;
var selEnd = editbox.selectionEnd;
var name = editbox.value.substring(selStart, selEnd);
// If name is all in capitals, convert target to title case.
var target = name;
if (target == target.toUpperCase()) {
target = _titlecase(target);
}
// split text into individual words
var nameArray = name.split(" ");
// put last first, followed by a comma, then all the rest preceded by spaces
name = nameArray[nameArray.length-1]+",";
for (var i=0; i<nameArray.length-1; i++)
name = name+" "+nameArray[i];
var linked = '[[Author:'+target+'|'+name+']]';
editbox.value = editbox.value.substring(0, selStart) + linked + editbox.value.substring(selEnd);
editbox.setSelectionRange(selStart+linked.length, selStart+linked.length);
}
function makeCenter() {
var editbox = document.getElementsByName('wpTextbox1')[0];
editbox.focus();
var selStart = editbox.selectionStart;
var selEnd = editbox.selectionEnd;
editbox.value = editbox.value.substring(0, selStart) + '{{center|' + editbox.value.substring(selStart, selEnd) + '}}' + editbox.value.substring(selEnd);
editbox.setSelectionRange(selStart, selStart+11);
}
function makeRight() {
var editbox = document.getElementsByName('wpTextbox1')[0];
editbox.focus();
var selStart = editbox.selectionStart;
var selEnd = editbox.selectionEnd;
editbox.value = editbox.value.substring(0, selStart) + '{{right|' + editbox.value.substring(selStart, selEnd) + '}}' + editbox.value.substring(selEnd);
editbox.setSelectionRange(selStart, selStart+10);
}
function sic() {
var editbox = document.getElementsByName('wpTextbox1')[0];
editbox.focus();
var selStart = editbox.selectionStart;
var selEnd = editbox.selectionEnd;
var selText = editbox.value.substring(selStart, selEnd);
editbox.value = editbox.value.substring(0, selStart) + '{{SIC|' + selText + '|[' + selText + ']}}' + editbox.value.substring(selEnd);
editbox.setSelectionRange(selEnd+8, selEnd+8);
}
//wrap first word in hwe template
function hwe() {
var editbox = document.getElementsByName('wpTextbox1')[0];
var to = editbox.value.search(/\W/);
if (to == -1) to = editbox.value.length;
var pre = editbox.value.substring(0, to);
var post = '{{hwe|'+pre+'|…'+pre+'}}';
editbox.value = post + editbox.value.substring(to);
}
//wrap last word in hws template
function hws() {
var editbox = document.getElementsByName('wpTextbox1')[0];
var from = editbox.value.lastIndexOf(" ");
if (from == -1) from = 0;
else from = from + 1;
var pre = editbox.value.substring(from);
if (pre.slice(-1) == "-") pre = pre.slice(0, -1);
var post = '{{hws|'+pre+'|'+pre+'…}}';
editbox.value = editbox.value.substring(0, from) + post;
}
function uc() {
var editbox = document.getElementsByName('wpTextbox1')[0];
var matches = /\{\{[Uu]c\s*\|\s*([^\}]+)\}\}/g.exec(editbox.value);
while (matches) {
editbox.value = editbox.value.replace(matches[0], matches[1].toUpperCase());
matches = /\{\{[Uu]c\s*\|\s*([^\}]+)\}\}/g.exec(editbox.value);
}
}
});
// </nowiki>