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

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