Jump to content

User:Evad37/duplinks-alt.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Evad37 (talk | contribs) at 00:47, 24 January 2019 (make sure dependency mw.util is loaded). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
//This script uses material from Wikipedia user Ucucha's script "duplinks" ( https://en.wikipedia.org/wiki/User:Ucucha/duplinks.js ), which is released under the Creative Commons Attribution-Share-Alike License 3.0 ( http://creativecommons.org/licenses/by-sa/3.0/ )
//See also https://en.wikipedia.org/wiki/User:Ucucha/duplinks for documentation of the original script
$( function($) {
    if((mw.config.get('wgNamespaceNumber') != 0) && (mw.config.get('wgNamespaceNumber') != 2)) {
        // only check links in mainspace and userspace (for userspace drafts)
        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();
	        // create a separate div surrounding the lead
	        // first get the element immediately surrounding the article text. Unfortunately, MW doesn't seem to provide a non-fragile way for that.
	        // also check if VisualEditor is being used - the element surrounding text is different in VE
	        var href = window.location.href;
	        if(href.search("veaction")>0) { var content = ".ve-ce-documentNode.ve-ce-branchNode"; }
	        else { var content = ".mw-parser-output"; }
	        $(content).prepend(document.createElement('div'));
	        var lead = $(content).children()[0];
	        $(lead).attr('id', 'lead');
	        $(content).children().each( function() {
	            if(this.nodeName.toLowerCase() == 'h2') {
	                return false;
	            }
	            if($(this).attr('id') != 'lead') {
	                $(lead).append(this);
	            }
	            return true;
	        });
	 
	        // detect duplicate links
	        mw.util.addCSS(".duplicate-link { border: 1px solid red; }");
	        mw.util.addCSS(".duplicated-link { border: 1px dashed green; }");
	        var finddups = function() {
	            var href = $(this).attr('href');
	            if(href != undefined && href.indexOf('#') != 0) {
	                if(seen[href]) {
	                    $(this).addClass("duplicate-link");
	                    duplicated[href] = true;
	                }
	                else {
	                    seen[href] = true;
	                  }
	            }
	            return true;
	        };
	        // mark duplicated links
	        var markdups = function() {
	            var href = $(this).attr('href');
	            if(href != undefined && href.indexOf('#') != 0) {
	                if(duplicated[href]) {
	                    $(this).addClass("duplicated-link");
	                    duplicated[href] = '';
	                }
	            }
	            return true;
	        };
	        // arrays to keep track of whether we've seen a link before, and which links are duplicated
	        var seen = [];
	        var duplicated = [];
	        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 = [];
	        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);
        });
    });
});