User:Parent5446/MediaWiki/RemoveDuplicateLinks.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. |
Documentation for this user script can be added at User:Parent5446/MediaWiki/RemoveDuplicateLinks. |
/*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);
}
});