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

Content deleted Content added
mw.config.get
selector change requested on talk
 
(6 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((mw.config.get('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();
// also check if VisualEditor is being used - the element surrounding text is different in VE
// Check if VisualEditor is being used - the element surrounding text is different in VE
var href = window.___location.href;
var isVisualEditor = if(window.___location.href.search("veaction")>0) { var content = ".ve-ce-documentNode.ve-ce-branchNode"; }
// Get the element immediately surrounding the article text.
else { var content = ".mw-content-ltr"; }
var $content = isVisualEditor ? $(".ve-ce-documentNode.ve-ce-branchNode") : $($(".mw-parser-output", "#mw-content-text")[0])
$(content).prepend(document.createElement('div'));
// Create a separate div to conatin the lead
var lead = $(content).children()[0];
$lead = $("<div id='lead'>").attrprependTo('id', 'lead'$content);
// Move the elements containing the lead content into this newly-created div
$(content).children().each( function() {
$lead.nextAll().each( function() {
if(this.nodeName.toLowerCase() == 'h2') {
if ( $(this).is('h2, .mw-heading2') ) {
return false;
// Reached the first heading after the lead.
}
// Returning false breaks out of the jQuery .each() loop early
if($(this).attr('id') != 'lead') {
return false;
$(lead).append(this);
}
}
$lead.append(this);
return true;
return true;
});
});
 
// detect duplicate links
// Objects to keep track of whether we've seen a link before, and which links are duplicated
mw.util.addCSS(".duplicate-link { border: 1px solid red; }");
var seen = {};
mw.util.addCSS(".duplicated-link { border: 1px solid green; }");
var finddupsduplicated = function() {};
var hasDuplicatedLinks = false;
var href = $(this).attr('href');
 
if(href != undefined && href.indexOf('#') != 0) {
// Styles
if(seen[href]) {
mw.util.addCSS(".duplicate-link { border: 1px solid red; }\n.duplicated-link { border: 1px dashed green; }");
$(this).addClass("duplicate-link");
duplicated[href] = true;
// Detect and mark links which are duplicates
}
var finddups = function() {
else {
var href = this.attributes.href && this.attributes.href.value;
seen[href] = true;
if (href != undefined && href.indexOf('#') != 0) {
}
if (seen[href]) {
}
this.classList.add("duplicate-link");
return true;
duplicated[href] = true;
};
hasDuplicatedLinks = true;
// mark duplicated links
}
var markdups = function() {
else {
var href = $(this).attr('href');
seen[href] = true;
if(href != undefined && href.indexOf('#') != 0) {
}
if(duplicated[href]) {
}
$(this).addClass("duplicated-link");
return true;
duplicated[href] = '';
};
}
// Detect and mark the first occurance of duplicated links
}
var markdups = function() {
return true;
var href = this.attributes.href && this.attributes.href.value;
};
if(href != undefined && href.indexOf('#') != 0) {
// arrays to keep track of whether we've seen a link before, and which links are duplicated
if(duplicated[href]) {
var seen = [];
var this.classList.add("duplicated = []-link");
duplicated[href] = '';
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 = [];
return true;
var 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);
// 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');
}
});
});
});