User:Acagastya/double.js

$.when(mw.loader.using(['mediawiki.util']), $.ready).done(function { if ( mw.config.get('wgNamespaceNumber') == 0 || mw.config.get('wgNamespaceNumber' == 2) ) {   // only check links in mainspace and userspace (for userspace drafts)    return;  }  const portletlink = mw.util.addPortletLink( 'p-article-tools', '#',   'Highlight duplicate links', 'ca-findduplicatelinks', 'Highlight duplicate links', 'd' );  $(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. const content = '.mw-content-ltr'; $(content).prepend(document.createElement('div')); const 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 { background: gold; color:#000; border: 1px solid red !important}'    ); const finddups = function { const href = $(this).attr('href'); if (href !== undefined && href.indexOf('#') !== 0) { if (seen[href]) { $(this).addClass('duplicate-link'); count++; console.log(this.title); } else { seen[href] = true; }     }      return true; };   // array to keep track of whether we've seen a link before const seen = []; let count = 0; mw.util.$content .find('p a') .not('#lead *, .infobox *, .navbox *, .mobile-only *') .each(finddups); seen.length = 0; mw.util.$content .find('#lead p a') .not('.infobox *, .navbox *, .sourceTemplate *, .mobile-only *') .each(finddups); if (count) alert(`${count} dublicate links found.`); count = 0; }); });