User:Polygnotus/Scripts/ReferenceHighlighter.js

This is an old revision of this page, as edited by Polygnotus (talk | contribs) at 02:40, 5 September 2024 (Created page with '// Wikipedia Reference Highlighter $(function() { // Function to highlight all references with the same base ID function highlightReferences(baseId) { // Remove previous highlights $('.reference-highlight').removeClass('reference-highlight'); // Highlight all references with the same base ID $('sup.reference a[href^="#cite_note-' + baseId + '"]').each(function() { $(this).closest('sup').addClass('...'). 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.
// Wikipedia Reference Highlighter
$(function() {
    // Function to highlight all references with the same base ID
    function highlightReferences(baseId) {
        // Remove previous highlights
        $('.reference-highlight').removeClass('reference-highlight');
        
        // Highlight all references with the same base ID
        $('sup.reference a[href^="#cite_note-' + baseId + '"]').each(function() {
            $(this).closest('sup').addClass('reference-highlight');
        });
        
        // Highlight the reference in the list at the bottom
        $('#cite_note-' + baseId).addClass('reference-highlight');
    }
    
    // Add click event listener to all reference links
    $('sup.reference a').on('click', function(e) {
        e.preventDefault();
        var href = $(this).attr('href');
        var baseId = href.split('-')[1].split('#')[0];
        highlightReferences(baseId);
        
        // Scroll to the reference list item
        var target = $(href);
        $('html, body').animate({
            scrollTop: target.offset().top
        }, 500);
    });
    
    // Add necessary CSS
    $('<style>')
        .prop('type', 'text/css')
        .html(`
            .reference-highlight {
                background-color: purple !important;
                color: white !important;
            }
            .reference-highlight a {
                color: white !important;
            }
        `)
        .appendTo('head');
});