User:Polygnotus/DuplicateReferences.js

This is an old revision of this page, as edited by Polygnotus (talk | contribs) at 00:41, 16 July 2024. 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.
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;
        }
        
        let referencesHeading = document.getElementById("References");
        if (!referencesHeading) {
            console.log("References heading not found, script terminated");
            return;
        }
        
        const style = 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);
        
        let parentDiv = referencesHeading.closest("div");
        let newParagraph = document.createElement("p");
        newParagraph.style.color = "red";
        
        function getDuplicateInfo() {
            console.log("Getting duplicate info");
            const referenceItems = document.querySelectorAll('.references cite');
            console.log("Number of reference items:", referenceItems.length);
            
            const urlMap = new Map();
            const duplicates = [];

            referenceItems.forEach((item, index) => {
                const refNumber = index + 1;
                const links = item.querySelectorAll('a');

                let validLink = null;
                for (let link of links) {
                    const url = link.href;
                    const linkText = link.textContent.trim();
                    
                    if (
                        linkText !== "Archived" &&
                        (!url.includes("wikipedia.org/wiki/") || url.includes("Special:BookSources")) &&
                        !url.includes("_(identifier)")
                    ) {
                        validLink = link;
                        break;
                    }
                }

                if (validLink) {
                    const url = validLink.href;
                    if (urlMap.has(url)) {
                        urlMap.get(url).push(refNumber.toString());
                    } else {
                        urlMap.set(url, [refNumber.toString()]);
                    }
                }
            });

            urlMap.forEach((refs, url) => {
                if (refs.length > 1) {
                    duplicates.push({ url, refs });
                }
            });

            console.log("Number of duplicate sets found:", duplicates.length);
            return duplicates;
        }

        function checkDuplicateReferenceLinks() {
            console.log("Checking for duplicate reference links");
            const duplicateInfo = getDuplicateInfo();
            
            if (duplicateInfo.length > 0) {
                console.log("Duplicates found, creating list");
                
                duplicateInfo.forEach(({ url, refs }) => {
                    let paragraphInfo = document.createElement('span');
                    
                    let urlLink = 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) => {
                        let link = document.createElement('a');
                        link.href = `#cite_note-${refNumber}`;
                        link.textContent = refNumber;
                        paragraphInfo.appendChild(link);

                        if (index < refs.length - 1) {
                            paragraphInfo.appendChild(document.createTextNode(', '));
                        }
                    });
                    
                    paragraphInfo.appendChild(document.createElement('br'));
                    newParagraph.appendChild(paragraphInfo);
                });
                
                console.log("Appending duplicate list to page");
                parentDiv.after(newParagraph);
            } else {
                console.log("No duplicates found");
            }
        }
        
        checkDuplicateReferenceLinks();
        console.log("Script execution completed");
    });
});