User:Evad37/duplinks-alt.js: Difference between revisions

Content deleted Content added
ce comments
selector change requested on talk
 
(9 intermediate revisions by 2 users not shown)
Line 2:
//See also https://en.wikipedia.org/wiki/User:Ucucha/duplinks for documentation of the original script
$( function($) {
var namespaceNumber if((wgNamespaceNumber != 0) && mw.config.get('wgNamespaceNumber != 2')) {;
// only check links in mainspace, and userspace (for userspace drafts), and draftspace
var isCorrectNamespace = namespaceNumber === 0 || namespaceNumber === 2 || namespaceNumber === 118;
return;
if (!isCorrectNamespace) {
}
return;
var portletlink = mw.util.addPortletLink('p-tb', '#', 'Highlight duplicate links', 'ca-findduplicatelinks');
}
$(portletlink).click( function(e) {
mw.loader.using('mediawiki.util').then(function(){
e.preventDefault();
var portletlink = mw.util.addPortletLink('p-tb', '#', 'Highlight duplicate links', 'ca-findduplicatelinks');
// create a separate div surrounding the lead
$(portletlink).click( function(e) {
// first get the element immediately surrounding the article text. Unfortunately, MW doesn't seem to provide a non-fragile way for that.
e.preventDefault();
var content = ".mw-content-ltr";
// Check if VisualEditor is being used - the element surrounding text is different in VE
$(content).prepend(document.createElement('div'));
var isVisualEditor = window.___location.href.search("veaction")>0;
var lead = $(content).children()[0];
// Get the element immediately surrounding the article text.
$(lead).attr('id', 'lead');
var $content = isVisualEditor ? $(".ve-ce-documentNode.ve-ce-branchNode") : $($(".mw-parser-output", "#mw-content-text")[0])
$(content).children().each( function() {
// Create a separate div to conatin the lead
if(this.nodeName.toLowerCase() == 'h2') {
$lead = $("<div id='lead'>").prependTo($content);
return false;
// Move the elements containing the lead content into this newly-created div
}
$lead.nextAll().each( function() {
if($(this).attr('id') != 'lead') {
if ( $(this).is('h2, .mw-heading2') ) {
$(lead).append(this);
// Reached the first heading after the lead.
}
// Returning false breaks out of the jQuery .each() loop early
return true;
return false;
});
}
$lead.append(this);
// detect duplicate links
return true;
mw.util.addCSS(".duplicate-link { border: 1px solid red !important; }");
});
mw.util.addCSS(".duplicated-link { border: 1px solid green; }");
 
var finddups = function() {
// Objects to keep track of whether we've seen a link before, and which links are duplicated
var href = $(this).attr('href');
var seen = {};
if(href != undefined && href.indexOf('#') != 0) {
var duplicated = {};
if(seen[href]) {
var hasDuplicatedLinks = false;
$(this).addClass("duplicate-link");
 
duplicated[href] = true;
// Styles
}
mw.util.addCSS(".duplicate-link { border: 1px solid red; }\n.duplicated-link { border: 1px dashed green; }");
else {
seen[href] = true;
// Detect and mark links which are duplicates
}
var finddups = function() {
}
var href = this.attributes.href && this.attributes.href.value;
return true;
if (href != undefined && href.indexOf('#') != 0) {
};
if (seen[href]) {
// mark duplicated links
this.classList.add("duplicate-link");
var markdups = function() {
duplicated[href] = true;
var href = $(this).attr('href');
hasDuplicatedLinks = true;
if(href != undefined && href.indexOf('#') != 0) {
}
if(duplicated[href]) {
else {
$(this).addClass("duplicated-link");
seen[href] = true;
}
}
}
return true;
return true;
};
};
// arrays to keep track of whether we've seen a link before, and which links are duplicated
// Detect and mark the first occurance of duplicated links
var seen = [];
var markdups = function() {
var duplicated = [];
var href = this.attributes.href && this.attributes.href.value;
mw.util.$content.find('p a').not('#lead *, .infobox *, .navbox *').each(finddups);
if(href != undefined && href.indexOf('#') != 0) {
mw.util.$content.find('p a').not('#lead *, .infobox *, .navbox *').each(markdups);
if(duplicated[href]) {
var seen = [];
var this.classList.add("duplicated = []-link");
duplicated[href] = '';
mw.util.$content.find('#lead p a').not('.infobox *, .navbox *').each(finddups);
}
mw.util.$content.find('#lead p a').not('.infobox *, .navbox *').each(markdups);
}
});
return true;
};
 
// Process sections after the lead
mw.util.$content.find('p a').not('#lead *, .infobox *, .navbox *').each(finddups);
mw.util.$content.find('p a').not('#lead *, .infobox *, .navbox *').each(markdups);
// Reset tracking objects, process lead section
seen = {};
duplicated = {};
mw.util.$content.find('#lead p a').not('.infobox *, .navbox *').each(finddups);
mw.util.$content.find('#lead p a').not('.infobox *, .navbox *').each(markdups);
 
// Show a notice if no duplicates were found
if (!hasDuplicatedLinks) {
mw.notify('No duplicated links were detected');
}
});
});
});