User:Polygnotus/Scripts/PreviousAfDs.js

This is an old revision of this page, as edited by Polygnotus (talk | contribs) at 20:14, 8 June 2025. 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.
// Add AfD search functionality to Wikipedia
$(document).ready(function() {
    // Only run on article pages (namespace 0)
    if (mw.config.get('wgNamespaceNumber') === 0) {
        addAfDSearchButton();
    }
});

function addAfDSearchButton() {
    // Create the search button
    var $button = $('<a>')
        .attr('href', '#')
        .attr('id', 'afd-search-btn')
        .text('Search AfD')
        .css({
            'margin-left': '10px',
            'font-size': '0.9em',
            'color': '#0645ad',
            'text-decoration': 'none'
        })
        .on('click', function(e) {
            e.preventDefault();
            searchAfDDiscussions();
        });

    // Add button to the page - you can modify this selector based on where you want it
    // This adds it after the page title
    $('#firstHeading').after($('<div>').append($button));
    
    // Alternative locations (uncomment one if preferred):
    // $('.mw-indicators').append($button); // Near other page indicators
    // $('#p-cactions ul').append($('<li>').append($button)); // In the page actions menu
}

function searchAfDDiscussions() {
    var articleTitle = mw.config.get('wgPageName').replace(/_/g, ' ');
    var searchPrefix = 'Wikipedia:Articles for deletion/' + articleTitle;
    
    // Show loading indicator
    $('#afd-search-btn').text('Searching...').css('color', '#888');
    
    var api = new mw.Api();
    
    // First try exact prefix match
    api.get({
        action: 'query',
        list: 'allpages',
        apprefix: searchPrefix,
        apnamespace: 4, // Wikipedia namespace
        aplimit: 50,
        format: 'json'
    }).done(function(data) {
        var exactMatches = data.query.allpages || [];
        
        // Then do a broader search using the search API
        api.get({
            action: 'query',
            list: 'search',
            srsearch: 'prefix:"Wikipedia:Articles for deletion/' + articleTitle + '"',
            srnamespace: 4,
            srlimit: 50,
            format: 'json'
        }).done(function(searchData) {
            var searchMatches = searchData.query.search || [];
            
            // Combine and deduplicate results
            var allMatches = [];
            var seenTitles = new Set();
            
            // Add exact matches first
            exactMatches.forEach(function(page) {
                if (!seenTitles.has(page.title)) {
                    allMatches.push({title: page.title});
                    seenTitles.add(page.title);
                }
            });
            
            // Add search matches
            searchMatches.forEach(function(page) {
                if (!seenTitles.has(page.title)) {
                    allMatches.push({title: page.title});
                    seenTitles.add(page.title);
                }
            });
            
            displayAfDResults(allMatches, articleTitle);
        }).fail(function() {
            // If search fails, just show exact matches
            displayAfDResults(exactMatches, articleTitle);
        });
    }).fail(function() {
        $('#afd-search-btn').text('Search AfD').css('color', '#0645ad');
        mw.notify('Error searching for AfD discussions', { type: 'error' });
    });
}

function displayAfDResults(pages, articleTitle) {
    // Reset button
    $('#afd-search-btn').text('Search AfD').css('color', '#0645ad');
    
    // Remove any existing results
    $('#afd-results').remove();
    
    var $resultsDiv = $('<div>')
        .attr('id', 'afd-results')
        .css({
            'margin': '10px 0',
            'padding': '10px',
            'border': '1px solid #ccc',
            'background-color': '#f9f9f9',
            'border-radius': '3px'
        });
    
    if (pages.length === 0) {
        $resultsDiv.html('<strong>No previous AfD discussions found for "' + articleTitle + '"</strong>');
    } else {
        var $title = $('<strong>').text('Previous AfD discussions for "' + articleTitle + '":');
        var $list = $('<ul>').css('margin', '10px 0');
        
        pages.forEach(function(page) {
            var $link = $('<a>')
                .attr('href', mw.util.getUrl(page.title))
                .text(page.title);
            
            $list.append($('<li>').append($link));
        });
        
        $resultsDiv.append($title).append($list);
    }
    
    // Add close button
    var $closeBtn = $('<span>')
        .text(' [close]')
        .css({
            'cursor': 'pointer',
            'color': '#0645ad',
            'font-size': '0.9em'
        })
        .on('click', function() {
            $('#afd-results').remove();
        });
    
    $resultsDiv.append($closeBtn);
    
    // Insert results after the button
    $('#afd-search-btn').parent().after($resultsDiv);
}

// Optional: Add keyboard shortcut (Alt+A)
$(document).on('keydown', function(e) {
    if (e.altKey && e.which === 65 && mw.config.get('wgNamespaceNumber') === 0) { // Alt+A
        e.preventDefault();
        searchAfDDiscussions();
    }
});