Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page.
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.
mw.loader.using(['mediawiki.util'],function(){$(document).ready(function(){console.log("Script started");if((mw.config.get('wgNamespaceNumber')!==0&&mw.config.get('wgPageName')!=='User:Polygnotus/dupreftest')||mw.config.get('wgAction')!=='view'){console.log("Not the correct page or action, script terminated");return;}console.log("Page title:",document.title);console.log("URL:",window.___location.href);functionfindNextReflistDiv(element){letnextElement=element.nextElementSibling;while(nextElement){if(nextElement.tagName.toLowerCase()==='div'&&(nextElement.classList.contains('reflist')||nextElement.classList.contains('mw-references-wrap'))){returnnextElement;}nextElement=nextElement.nextElementSibling;}returnnull;}constreferencesHeader=document.querySelector("h2#References");if(!referencesHeader){console.log("References heading not found, script terminated");return;}constcontainerDiv=referencesHeader.closest("div");if(!containerDiv){console.log("Container div not found, script terminated");return;}constreflistDiv=findNextReflistDiv(containerDiv);if(!reflistDiv){console.log("Reflist div not found, script terminated");return;}constreferencesList=reflistDiv.querySelector('ol.references');if(!referencesList){console.log("ol.references not found within reflist div");return;}conststyle=document.createElement('style');style.textContent=` li:target { border: 1px dotted red; padding: 2px; background-color: #ffcccc !important;} .duplicate-citation-highlight { background-color: #ffe6e6; } .duplicate-citation-hover { background-color: #ffcccc; } .duplicate-citation-clicked { border: 1px dotted red; padding: 2px; background-color: #ffe6e6; } `;document.head.appendChild(style);letnewParagraph=document.createElement("p");newParagraph.style.color="red";functionaddDuplicateCitationsTemplate(){console.log("Adding duplicate citations template");varapi=newmw.Api();varpageTitle=mw.config.get('wgPageName');letduplicateInfo=getDuplicateInfo();api.get({action:'query',prop:'revisions',titles:pageTitle,rvprop:'content',rvslots:'main',formatversion:2}).then(function(data){varpage=data.query.pages[0];varcontent=page.revisions[0].slots.main.content;varnewContent='{{Duplicate citations}}\n'+content;letsummary='+{{Duplicate citations}}';if(duplicateInfo.length>0){summary+=': ';duplicateInfo.forEach((info,index)=>{summary+=`${info.url} (refs: ${info.refs.join(', ')})`;if(index<duplicateInfo.length-1){summary+='; ';}});}returnapi.postWithToken('csrf',{action:'edit',title:pageTitle,text:newContent,summary:summary});}).then(function(){mw.notify('Successfully added the Duplicate citations template!');___location.reload();}).catch(function(error){console.error('Error:',error);mw.notify('Failed to add the template. See console for details.',{type:'error'});});}functiongetDuplicateInfo(){console.log("Getting duplicate info");constreferenceItems=referencesList.querySelectorAll('li');console.log("Number of reference items:",referenceItems.length);consturlMap=newMap();constduplicates=[];referenceItems.forEach((item,index)=>{constrefNumber=index+1;console.log(`Processing reference item ${refNumber}`);constspan=item.querySelector('span.reference-text');if(!span){console.log(` No reference-text span found in item ${refNumber}`);return;}constlinks=span.querySelectorAll('a');console.log(` Number of links in this span: ${links.length}`);letvalidLink=null;for(letlinkoflinks){consturl=link.href;constlinkText=link.textContent.trim();if(linkText!=="Archived"&&(!url.includes("wikipedia.org/wiki/")||url.includes("Special:BookSources"))&&!url.includes("_(identifier)")){validLink=link;console.log(` Valid link found: ${url}`);break;}}if(validLink){consturl=validLink.href;if(urlMap.has(url)){urlMap.get(url).push(refNumber.toString());console.log(` Duplicate found for URL: ${url}`);}else{urlMap.set(url,[refNumber.toString()]);console.log(` New URL added to map: ${url}`);}}else{console.log(` No valid link found in this item`);}});urlMap.forEach((refs,url)=>{if(refs.length>1){duplicates.push({url,refs});}});console.log("Number of duplicate sets found:",duplicates.length);console.log("Duplicate sets:",duplicates);returnduplicates;}functioncheckDuplicateReferenceLinks(){console.log("Checking for duplicate reference links");constduplicateInfo=getDuplicateInfo();if(duplicateInfo.length>0){console.log("Duplicates found, creating list");if(document.querySelector('table.box-Duplicated_citations')===null){consteditSections=containerDiv.querySelectorAll('span.mw-editsection');editSections.forEach(editSection=>{letspanBefore=document.createElement('span');spanBefore.className='mw-editsection-bracket';spanBefore.textContent='[';letaddTemplateLink=document.createElement('a');addTemplateLink.textContent=' add {{duplicated citations}} ';addTemplateLink.href='#';addTemplateLink.addEventListener('click',function(e){e.preventDefault();addDuplicateCitationsTemplate();});letspanAfter=document.createElement('span');spanAfter.className='mw-editsection-bracket';spanAfter.textContent=']';editSection.appendChild(spanBefore);editSection.appendChild(addTemplateLink);editSection.appendChild(spanAfter);});}duplicateInfo.forEach(({url,refs})=>{letparagraphInfo=document.createElement('span');leturlLink=document.createElement('a');urlLink.href=url;urlLink.textContent=url;urlLink.target="_blank";urlLink.rel="noopener noreferrer";paragraphInfo.appendChild(document.createTextNode('Duplicate URL: '));paragraphInfo.appendChild(urlLink);paragraphInfo.appendChild(document.createTextNode(' in refs: '));refs.forEach((refNumber,index)=>{letlink=document.createElement('a');link.href=`#cite_note-${ref}`;link.textContent=refNumber;paragraphInfo.appendChild(link);link.addEventListener('mouseover',()=>{refs.forEach(ref=>{constcitationElement=document.getElementById(`cite_note-${ref}`);if(citationElement){if(ref===refNumber){citationElement.classList.add('duplicate-citation-hover');}else{citationElement.classList.add('duplicate-citation-highlight');}}});});link.addEventListener('mouseout',()=>{refs.forEach(ref=>{constcitationElement=document.getElementById(`cite_note-${ref}`);if(citationElement){citationElement.classList.remove('duplicate-citation-hover');citationElement.classList.remove('duplicate-citation-highlight');}});});link.addEventListener('click',()=>{document.querySelectorAll('.duplicate-citation-clicked').forEach(el=>{el.classList.remove('duplicate-citation-clicked');});refs.forEach(ref=>{constcitationElement=document.getElementById(`cite_note-${ref}`);if(citationElement){citationElement.classList.add('duplicate-citation-clicked');}});});if(index<refs.length-1){paragraphInfo.appendChild(document.createTextNode(', '));}});paragraphInfo.appendChild(document.createElement('br'));newParagraph.appendChild(paragraphInfo);});console.log("Appending duplicate list to page");containerDiv.after(newParagraph);}else{console.log("No duplicates found");}}checkDuplicateReferenceLinks();console.log("Script execution completed");});});