User:GregU/monobook.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 can be added at User:GregU/monobook.css. |
importScript("User:GregU/familytree.js");
importScript("User:GregU/hotkeys.js");
importScript("User:GregU/randomlink.js");
importScript("Wikipedia:WikiProject User scripts/Scripts/Replace");
hotkeys = {
'b' : 'Brooks was here',
'l' : '',
's' : '§',
'w' : 'Now is the time for all good men to come to the aid of their country. Now is the time for all good men to come to the aid of their country. Now is the time for all good men.',
't' : '{| class="wikitable"\n! header\n! header\n|-\n| data\n| data\n|}\n'
}
//===================== HYPHENS-TO-DASHES MODULE ======================
// Turn some hyphens into dashes, -> into arrows, and other piddly things.
// The user can disable these conversions by putting "nodashes" somewhere
// in the text -- either temporarily or permanently. You can similarly add
// "scores" if the score-detection heuristic doesn't trigger automatically.
// by [[User:GregU]]
//
function dashes_etc (str)
{
if (str.search(/nodashes/i) >= 0)
return str;
var scpat = /\bscores?\b|\[\[Category:.*\b(sport|athlet|players|teams|games|league|champion|tournament|competit|cup\b|\w+ball\b|hockey|lacrosse|cricket|rugby|tennis|golf|polo|boxing|boxers|martial.art|chess)/i;
var scoresAreLikely = (str.search(scpat) >= 0);
// Ensure the replacement isn't a link such as [[FOO - BAR]] before
// replacing it, so that we don't break the link. But we do want to
// replace dashes used in the right-side label part of links. Also,
// don't break templates, URLs, DOIs, {{#expr:}}, <math> equations,
// source code, or <ref name="13-70">.
//
function ifNotLink (str,pos,string)
{
var pat = /\[\[[^|\]]*$|\{\{[^|}]*$|[:\/%][^\s|>]+$|<[^>]*$|#\w*expr:.*$/i;
if (string.substring(pos-260,pos+1).search(pat) >= 0)
return str; // it's a link, so don't change it
var pat2 = /\{\{(main|see|detail|about|for\b|other|redir)[^}]*$/i;
if (string.substring(pos-260,pos+1).search(pat2) >= 0)
return str; // likely templates with page-name parameters
var m = string.slice(pos).search(/<\/?(math|source|syntaxhighlight|pre|code)\b/i);
if (m >= 0 && string.charAt(pos+m+1) == '/')
return str; // don't break a <math> equation, or source code
if (string.slice(pos).search(/^[^|{}[\]<>\n]*\.[a-z]{3,4}\s*[|}]|^.*hyphen/i) >= 0)
return str; // it's a file name parameter, or <!--sic hyphen-->
return str.replace(/--+\b/g, "—").replace(/-+/g, "–");
}
str = str.replace(/\s--?\s/g, ifNotLink); // en dash looks better
str = str.replace(/[a-z\d]---?[a-z\d]/ig, ifNotLink); // em dash
str = str.replace(/\d\d\d]*}*-(?:present|current)\b/ig, ifNotLink); // 1973-present
str = str.replace(/[^\w-](?:18|19|20)\d\d]*}*-[^\w-]/g, ifNotLink); // (1973-)
str = str.replace(/\d(?:s|%|''')-\d/g, ifNotLink); // 1950s-1960s, 40%-50%
str = str.replace(/\d-(?:\$|'+)\d/g, ifNotLink); // $40-$50, 7-'''4''', '49-'53
str = str.replace(/[½⅓⅔¼¾⅛⅜⅝⅞]%?-[\d½⅓⅔¼¾⅛⅜⅝⅞]/g, ifNotLink); // 3½-6
str = str.replace(/\d(?:st|nd|rd|th)?-\d+(?:st|nd|rd|th)\b/g, ifNotLink); // 2nd-3rd
str = str.replace(/([\s\w])->([\s\w])/g, "$1→$2"); // better arrow
str = str.replace(/([\s\w])<-([\s\w])/g, "$1←$2"); // better arrow
// November 15, 2005-March 7, 2006; [[March 18]]-[[April 4]]
str = str.replace(/(\d\]*)[-–—](\[*(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[a-z]* +\d)/g, "$1 – $2");
// May-June 2007
str = str.replace(/\b(?:(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[a-z]*-?\b){2,}/g, ifNotLink);
// [[266]]-[[283]]
str = str.replace(/(\d(?: BC)?\]\])-((ca?\.|AD ?)?\[\[\d)/g, "$1–$2");
// iv-xii
str = str.replace(/[ ;(=](?=\w+-)(?:m*(?:cm|cd|d?c*)(?:xc|xl|l?x*)(?:ix|iv|v?i*)-?\b){2}[^\w-]/g, ifNotLink);
if (scoresAreLikely) // W-L-D or 73–70–67–70=280, but not castling
str = str.replace(/[^\w–-](?!0-0-0)(?:\d\d?\d?-){2,}\d\d?[^\w\/–-]/g, ifNotLink);
// Number ranges and scores should use en dashes, per [[WP:DASH]].
// This has been well-tested and false positives (e.g., ID nos.) are rare.
//
function range (str, before,from,to,after, pos,string)
{
var dash = true;
var except = /\b(fig(ure)?|table|example|circular|section|part|number|no|nr|id|model|pub|std|report|rpt|law|P?L|p|page|date|IS\wN\b[ a-z]*)[^\w(,;]*$/i;
var rpat = /^([^A-Za-z]|nbsp)*(AD|BC|B?CE|BP|[kMG]a|km|mi|kg|lb|\w?Hz|vote|decision|record|odds|scor\w*|win|loss|tie|draw|lead|victory|defeat|upset|run|deficit|start|finish|season|game)\b/;
var lpat = /\b(pages|pp|nos|\d+\)?'*[:,]|(w[io]n|lost?|tie|dr.w|lea?d|f.ll|vot|rul|decid|pass|fail|defeat|scor|gam|match|trail|finish|end)e?[ds]?|\w\w+ing|ahead|behind|up|down|from|to|is|are|was|were|of|out|by|an?|at|it|went|go|gone|beaten|between)([^a-z]|nbsp)*$/i;
var inorder = (to-0 > from.slice(-to.length)); // pp 362-5
var precision = Math.max( from.search(/0*$/), to.search(/0*$/) );
if (string.substring(pos-20,pos+1).search(except) >= 0) {
return str; // based on preceding word, looks like a ref number
}
if (from == 9 && to == 11) {
dash = false; // 9-11 is a common special case
}
if (from-0 >= to) {
dash = false; // values don't look like a range
}
if (to-from > 120 && from * (precision > 2 ? 5 : 50) < to && from > 1) {
dash = false; // values don't look like a range
}
if (scoresAreLikely && from <= 900 && to <= 900) {
dash = true; // likely a score or wins-losses
}
if (from < 2-to && string.search(/Category:.*\bChess\b/i) >= 0) {
dash = false; // chess notations 0-0, 0-1, 1-0
}
if (before == '(' && after == ')') {
dash = true; // scores often seen as (8-4)
}
if (from.search(/^0./) >= 0 || to.search(/^0./) >= 0) {
dash = false; // 3-07 and 0123-4567 look like ref numbers
}
if (string.substr(pos-1,15).search(/^\d([:,.])\d+-\d+\1\d/) >= 0) {
dash = true; // 10:30-11:30, 35,000-40,000, 2.5-4.0
}
if (string.substr(pos,30).search(rpat) >= 0) {
dash = true; // 12-5 BC, 5-5000 km, 6-4 win, 73-50 vote
}
if (string.substring(pos-80,pos).search(lpat) >= 0) {
dash = true; // pp. 8, 25, 270-74, 313-7; won 6-4, 6-2
}
if (from > 1000 && from < 2100 && to.length == 2 && inorder) {
dash = true; // 1994-95 year range
}
return dash ? ifNotLink(str,pos,string) : str;
}
str = str.replace(/([^\w\/–-])(\d{1,4})-(\d{1,4})([^\w\/–-])/g, range);
return str;
}
//================ END OF CORE HYPHENS-TO-DASHES CODE =================
// Hyphens-to-dashes test harness
// Test on random articles or on [[User:GregU/Sandbox]]
//
function dashes_test()
{
var box = document.editform.wpTextbox1;
box.value = dashes_etc( box.value );
document.editform.wpDiff.click(); // faster testing
}
function false_negatives() // show possible false negatives
{
var box = document.editform.wpTextbox1;
box.value = box.value.replace(/(\d)-/g, "$1♥").replace(/-(\d)/g, "♥$1")
.replace(/♥(\w+)♥(\w+)♥(\w+)♥/g, "-$1-$2-$3-")
.replace(/♥(\w+)♥(\w+)♥/g, "-$1-$2-")
.replace(/♥(\w+)♥/g, "-$1-");
box.value = box.value.replace(/(https?:\/\/[^\s|[\]<>{}]*)♥/g, "$1-")
.replace(/(https?:\/\/[^\s|[\]<>{}]*)♥/g, "$1-");
document.editform.wpDiff.click();
}
addOnloadHook (function()
{
if (wgIsArticle && wgAction == "view") {
var url = wgServer + wgScript + '?title=' + encodeURIComponent(wgPageName) + '&action=edit';
addPortletLink('p-navigation', url + '&dashes=true', 'Dashes test');
}
else if (document.editform && wgAction == "edit") {
addPortletLink('p-navigation', 'javascript:dashes_test()', 'Dashes test');
if (document.URL.indexOf("&dashes=true") > 0) dashes_test();
}
else if (wgAction == "submit") {
addPortletLink('p-navigation', 'javascript:false_negatives()', 'False negatives');
}
// Show a 4-column table at top of articles of possible range snippets
//
if (wgIsArticle && wgAction == "view") {
var con = document.getElementById("bodyContent");
var text = con.textContent;
var pat = /[^\w\/–-](\d{1,4}[–-]\d{1,4})[^\w\/–-]/g;
var res, snip;
var html = "<table width='95%' style='margin: auto auto'>";
var count = 0;
while ((res = pat.exec(text)) != null) {
if (count++ % 4 == 0)
html += "\n<tr>";
snip = text.substring( res.index - 20, res.index + res[0].length + 20 );
snip = snip.replace(/&/g, "&")
.replace(/"/g, """)
.replace(/\n/g, " ");
html += '<td title="' + snip + '">'
+ text.substring( res.index-8, res.index+1 )
+ "<b>" + res[1] + "</b>"
+ text.substr( res.index + res[0].length - 1, 9 );
}
html += "\n</table>";
var div = document.createElement("div");
div.innerHTML = html;
con.insertBefore( div, con.firstChild );
}
});
// Testing of randomlink.js script
addOnloadHook (function()
{
randomlink_exclude = /^Wikipedia:|^Portal:|^(Lists?|Outline|Library) of|^(Deaths in )?20\d\d$/;
addPortletLink('p-navigation', 'javascript:randomLink("Wikipedia:Featured articles")',
'Featured article', 'n-randomfa', 'Pick a random featured article');
bblist = "Special:WhatLinksHere/Template:WikiProject_Baseball?namespace=1&hidelinks=1&limit=250";
addPortletLink('p-navigation', 'javascript:randomlink_paged=1;randomlink_maxfrom=24000000;randomLink(bblist)',
'Baseball article', 'n-randombb', 'Random article in WikiProject Baseball');
addPortletLink('p-navigation', 'javascript:randomLink("Special:WhatLinksHere")',
'Random back-link', 'n-randomback', 'Random page that links here');
addPortletLink('p-navigation', 'javascript:randomLink("Special:Recentchangeslinked")',
'Random related', 'n-randomrel', "Random link that's been updated recently");
addPortletLink('p-navigation', 'javascript:randomLink("Special:Contributions")',
'Random contrib', 'n-randomcontrib', 'Random user-page-user-page...');
});