User:Polygnotus/Scripts/SectionLinks.js

This is an old revision of this page, as edited by Polygnotus (talk | contribs) at 05:11, 15 October 2024 (Created page with '// Add link icons to h2 headers for current versions, old revisions, and diffs (function() { function addLinkIcons() { const headers = document.querySelectorAll('h2[id]'); headers.forEach(header => { const isCurrentVersion = !window.___location.search.match(/[?&]oldid=(\d+)/) && !document.querySelector('.diff-currentversion-title'); if (isCurrentVersion) { // Add two icons for cu...'). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
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.
// Add link icons to h2 headers for current versions, old revisions, and diffs
(function() {
    function addLinkIcons() {
        const headers = document.querySelectorAll('h2[id]');
        
        headers.forEach(header => {
            const isCurrentVersion = !window.___location.search.match(/[?&]oldid=(\d+)/) && !document.querySelector('.diff-currentversion-title');
            
            if (isCurrentVersion) {
                // Add two icons for current version
                addIcon(header, '🔗', false); // Chain link emoji for section link
                addIcon(header, '📌', true);  // Pushpin emoji for permalink
            } else {
                // Add single icon for old revision or diff
                addIcon(header, '🔗', true);
            }
        });
    }

    function addIcon(header, iconText, isPermalink) {
        const icon = document.createElement('span');
        icon.innerHTML = iconText;
        icon.style.cursor = 'pointer';
        icon.style.marginRight = '5px';
        icon.style.fontSize = '0.8em';
        
        icon.addEventListener('click', function(e) {
            e.preventDefault();
            let url;
            
            if (isPermalink) {
                const currentId = mw.config.get('wgRevisionId');
                url = `${window.___location.origin}${window.___location.pathname}?oldid=${currentId}#${header.id}`;
            } else {
                url = `${window.___location.origin}${window.___location.pathname}#${header.id}`;
            }
            
            navigator.clipboard.writeText(url).then(() => {
                mw.notify(`${isPermalink ? 'Permalink' : 'Section link'} copied to clipboard!`, {
                    type: 'success',
                    tag: 'urlCopy'
                });
            }).catch(() => {
                mw.notify('Failed to copy URL', {
                    type: 'error',
                    tag: 'urlCopy'
                });
            });
        });
        
        header.insertBefore(icon, header.firstChild);
    }

    // Run the function when the DOM is fully loaded
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', addLinkIcons);
    } else {
        addLinkIcons();
    }
})();