Jump to content

User:Parent5446/MediaWiki/RemoveDuplicateLinks.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.
/*jslint white: true, browser: true, forin: true, undef: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, newcap: true, immed: true, strict: true */
"use strict";
function duplicate_links() {
    // Get information from the document.
    var textbox = document.getElementById("wpTextbox1").value;
    var messages = "";
    
    // Get an array of the links.
    var links_raw = textbox.match(/\[\[([^\|\[\]]+)(\|[^\|\[\]]+)?\]\]/gm);
    var links_done = [];
    
    // Run through each link.
    for (var link_num in links_raw) {
        // Process the target and display text of the link, even if they are the same.
        var has_title = (links_raw[link_num].indexOf("|") !== -1);
        var link_target, link_title;
        if (has_title) {
            link_target = links_raw[link_num].slice(2, links_raw[link_num].indexOf("|"));
            link_title = links_raw[link_num].slice(links_raw[link_num].indexOf("|") + 1, links_raw[link_num].indexOf("]"));
        } else {
            link_target = links_raw[link_num].slice(2, links_raw[link_num].indexOf("]"));
            link_title = link_target;
        }
        // Check if the link already exists in the article.
        if (links_done.toString().indexOf(link_title) !== -1) {
            // Link is a duplicate, remove it and all other duplicates.
            var start = 0, index = 0;
            var occurrences = 0;
            var link_length = links_raw[link_num].length;
            // Remove all duplicate links.
            while (index !== -1) {
                // Take everything before the match and everything after.
                var index = textbox.indexOf(links_raw[link_num], start);
                var beginning = textbox.slice(0, index);
                var end = textbox.slice(index + link_length);
                
                if (occurrences !== 0) {
                    textbox = beginning + link_title + end;
                }

                // Reset variables to find next duplicate.
                start = index + link_length;
                occurrences++;
            }
 
            // Alert the user of how many links were removed.
            messages += link_title + " | " + link_target + ": " + (occurrences - 1) + " links removed.\n";
        } else {
            // Link is not a duplicate.
            links_done.push(link_title);
        }
    }
    }
    
    // Alert use of what happened and make a diff.
    alert(messages);
    document.getElementById("wpTextbox1").value = textbox;
    document.getElementById("wpDiff").push();
}
 
addOnloadHook(function() {
    if (document.forms.editform) {
        mw.util.addPortletLink('p-tb', 'javascript:duplicate_links();', 'Remove duplicate links', 't-duplicatelink', 'Removes duplicate links', '', '');
    } else {
        mw.util.addPortletLink('p-tb', document.getElementById("ca-edit").getElementsByTagName("a")[0].href + "&duplicatelinks=1", 'Remove duplicate links', 't-duplicatelink', 'Removes duplicate links', '', '');
    }
    
    if (location.search.indexOf("duplicatelinks=1") !== -1) {
        duplicate_links(true);
    }
});