Jump to content

User:WikiWikiWayne/common.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.
/* Disables auto-launch of diff */

autoEdClick = false;

/* Add-on for wikEd */

wikEdRegExTypoFix = true;

/* Unify ref parameter spacing */

page: importScript( 'User:Waldyrious/formatcitations.js' ); // Backlink: [[User:Waldyrious/formatcitations.js]]

/* Add AutoFormatter */

mw.loader.load( '//de.wikipedia.org/w/index.php?title=Benutzer:TMg/autoFormatter.js&action=raw&ctype=text/javascript' );

/* Add-on JavaScript Wiki Browser */

mw.loader.load('//en.wikipedia.org/w/index.php?title=User:Joeytje50/JWB.js/load.js&action=raw&ctype=text/javascript');

/* Find duplicate arguments */

findargdupsresultsbox = 'yes';
importScript('User:Frietjes/findargdups.js'); // [[User:Frietjes/findargdups]]

/* Reference segregator that can also migrate to LDR style */

var SegregateRefsJsAllowConversion = true;
importScript("User:PleaseStand/segregate-refs.js");

/* Adds template wizard */
mw.loader.load( 'https://en.wikipedia.org/w/index.php?title=User:Samwilson/TemplateWizard.js&action=raw&ctype=text/javascript' );

/* Adds Sources script links to lefthand Toolbar */
importScript('User:Ohconfucius/script/Sources.js'); // [[User:Ohconfucius/script/Sources.js]]

/* Adds AutoFormatter to Toolbar */

mw.loader.load( '//de.wikipedia.org/w/index.php?title=Benutzer:TMg/autoFormatter.js&action=raw&ctype=text/javascript' );

/* Adds Teahouse Badges to Wikilove */

importScript("User:Ocaasi/WikiLoveinstallscript.js");

/* Adds a TB link next to every signature at The Teahouse to send a Talkback message to their Talk page */

importScript("User:Writ Keeper/Scripts/teahouseTalkbackLink.js"); // Adds a talkback link to signatures on the Teahouse

/* Puts Go to top link at bottom of each page */

importScript('User:Numbermaniac/goToTop.js'); // [[User:Numbermaniac/goToTop.js]]

/* Puts a Highlight duplicate links link on left side of each page */

importScript('User:Ucucha/duplinks.js'); // [[User:Ucucha/duplinks]]

/* Puts a reFILL link on the left side of each page */

mw.loader.load( "https://meta.wikimedia.org/w/index.php?title=User:Zhaofeng_Li/Reflinks.js&action=raw&ctype=text/javascript" );

/* Puts a Pending changes link at top right of each page */

var head = document.getElementById('mw-head').getElementsByTagName('ul')[0];
var li = document.createElement("li");
var a = document.createElement("a");

a.appendChild(document.createTextNode("Pending changes"));
a.href = "https://en.wikipedia.org/wiki/Special:PendingChanges";

li.appendChild(a);
head.insertBefore(li, head.childNodes[9]);

/* Adding SmartLinks */

if ( $.inArray( mw.config.get( 'wgAction' ), ['edit', 'submit'] ) > -1 ) {
	importScript('User:V111P/js/smartLinkingLoader.js'); // [[User:V111P/js/smartLinkingLoader.js]]
}

/* Adds parent and child sort to HotCat */

window.hotcat_use_category_links = true;

/* Adds AutoEd to Tools */

importScript('Wikipedia:AutoEd/complete.js');

/* Adds FurMe for tagging images with licensing */

importScript('User:AWeenieMan/furme.js');

/* Adds links in lefthand Tools links section to unify dates per MOS:DATES */

importScript('User:Ohconfucius/script/MOSNUM dates.js'); // [[User:Ohconfucius/script/MOSNUM dates.js]]
/* Puts a link in the Tools tab to easily submit Diffs to EditQuest */

importScript('User:Samtar/dev/editquest/editquest.js'); //Edit Quest

/* Adds DYK Check link to lefthand Tools links */

importScript('User:Shubinator/DYKcheck.js'); //DYKcheck tool

/* Adds Mass Roolback with checkboxes portlet to user contribs page */
importScript('User:Kangaroopower/MRollback.js'); //Mass Rollback Script [[User:Kangaroopower/MRollback.js]]

//This function adds a link to the toolbox which, when clicked, displays the size of the page
//and the size of the prose in bytes. See the talk page for more details.

mw.loader.load('//en.wikipedia.org/w/index.php?title=User%3ADr+pda%2Fprosesizebytes.js&action=raw&ctype=text/javascript'); // [[User:Dr pda/prosesizebytes.js]]

/* Adding one click archiver */

importScript('User:Technical_13/Scripts/OneClickArchiver.js'); 
/* Backlink: [[User:Technical_13/Scripts/OneClickArchiver]] */

/* Normalizes spaces in citation templates */

function formatCitations(vertical) {
    var txt = document.editform.wpTextbox1;
 
    // Fill an array with one entry per each recognized citation template
    // Note: the regex should be changed to allow parameter values that are themselves templates,
    // e.g. {{cite book | author = John Doe | title = The Book | year = 1234 | ref = {{sfnRef|Doe (1234)}} | isbn = 0-12345-678-9 }}
    // I got as far as this: /(\{\{[Cc]it(?:ation|(?:ar|e) [a-z ]+)\s*)(\|\s*[^=]+=\s*(?:\{\{)?.+(?:\}\})?\s*)+(\}\})/g
    var originalTemplates = txt.value.match(/\{\{[Cc]it(ation|(ar|e) [a-z ]+) *\n? *\|[^{}]+\}\}/g);
    // Duplicate the array, for editing. We need to keep the original strings for the replacement step
    var tweakedTemplates  = originalTemplates.slice();

    for(var i = 0; i < originalTemplates.length; i++) {
        if(vertical) {
            // Fill an array with one entry per each parameter for this citation template
            var originalParams = originalTemplates[i].match(/ *\n? *\| *\n? *[^ =]+ *= */g);
            // Create array to be filled with the cleaned-up parameters.
            // We need to keep the original strings for the replacement step.
            var tweakedParams = [];

            var maxWidth = 0;
            for(var j = 0; j < originalParams.length; j++){
                // Get rid of the delimiters and spaces, keep only the parameter string
                tweakedParams[j] = originalParams[j].match(/[a-z1-9_-]+/)[0];
                // Calculate the length of the longest parameter
                maxWidth = (tweakedParams[j].length>maxWidth) ? tweakedParams[j].length : maxWidth;
            }
            maxWidth++; // We need an extra one because Array(n).join(' ') will produce a string with n-1 chars

            // Generate the aligned versions of the parameters (with padding before the equal signs)
            for(var k = 0; k < originalParams.length; k++) {
                var numSpaces = maxWidth - tweakedParams[k].length;
                var alignedParam = "\n  | " + tweakedParams[k] + new Array(numSpaces).join(" ") + " = ";
                // Replace the original parameters with the tweaked ones
                tweakedTemplates[i] = tweakedTemplates[i].replace (originalParams[k], alignedParam);
            }

            // Also align the }}
            tweakedTemplates[i] = tweakedTemplates[i].replace(/ *\n? *\}\}/g,"\n  }}"); 
            // Replace the original templates with the tweaked versions
            txt.value = txt.value.replace(originalTemplates[i], tweakedTemplates[i]); 
        } else {
            // Remove newlines
            tweakedTemplates[i] = tweakedTemplates[i].replace(/\n/g, "");
            // Normalize spaces around the pipes and equal signs
            tweakedTemplates[i] = tweakedTemplates[i].replace(/ *\| *([a-z1-9_-]+) *= */g," |$1 = ");
            // Remove potencial extra spaces before template ends
            tweakedTemplates[i] = tweakedTemplates[i].replace(/ *\}\}$/," }}");
            txt.value = txt.value.replace(originalTemplates[i], tweakedTemplates[i]);                                      
        }
    }

    // Update the edit summary
    var sum = document.editform.wpSummary;
    var summary = vertical ? "convert citation templates to vertical format" : "harmonize whitespace" ;
    summary += " (using [[User:Waldir/formatcitations.js]])";
    if (sum.value.indexOf(summary) == -1) {
        if (sum.value.match(/[^\*\/\s][^\/\s]?\s*$/)) {
            sum.value += " | ";
        }
        sum.value += summary;
    }
    if(!vertical) { document.editform.wpMinoredit.checked = true; }
}

$(function () {
    if(document.forms.editform) {
        mw.util.addPortletLink('p-cactions', 'javascript:formatCitations(false)', '{{}}', 'ca-formatcitations', 'Format citations: add whitespace', '-', '');
        mw.util.addPortletLink('p-cactions', 'javascript:formatCitations(true)', '{{}}+', 'ca-formatcitations-vertical', 'Formats citations vertically', '-', '');
    }
});

/* Opens edit summary in a new empty  box, then reinserts section name */

importScript('User:Svick/SectionInput.js');

importScript('User:Ohconfucius/script/MOSNUM_dates.js'); // Backlink: [[User:Ohconfucius/script/MOSNUM_dates.js]]

/* Adds refrenamer to tools */

importScript('User:Nardog/RefRenamer.js'); // Backlink: [[User:Nardog/RefRenamer.js]]