User:BZPN/RfD.js

From Test Wiki
Revision as of 12:58, 1 November 2024 by BZPN (talk | contribs)
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
// Simple English Wikipedia Deletion Gadget with Popup
// This gadget adds a "Nominate for deletion" button to the toolbar, opening a popup for deletion nomination.

(function() {
    if (mw.config.get('wgNamespaceNumber') < 0 || mw.config.get('wgIsArticle') === false) return;

    // Add "Nominate for deletion" button to the toolbar
    mw.util.addPortletLink(
        'p-cactions',
        '#',
        'Nominate for deletion',
        'ca-nominate-deletion',
        'Nominate this page for deletion'
    );

    // Add click event for the deletion nomination button
    $('#ca-nominate-deletion').on('click', function(e) {
        e.preventDefault();
        openDeletionPopup();
    });

    // Function to create and open the deletion popup
    function openDeletionPopup() {
        // Create the popup overlay
        const overlay = $('<div>').attr('id', 'deletion-popup-overlay').css({
            'position': 'fixed',
            'top': '0',
            'left': '0',
            'width': '100%',
            'height': '100%',
            'background-color': 'rgba(0, 0, 0, 0.5)',
            'z-index': '1000',
        }).appendTo('body');

        // Create the popup container
        const popup = $('<div>').attr('id', 'deletion-popup').css({
            'width': '400px',
            'margin': '10% auto',
            'padding': '20px',
            'background-color': '#fff',
            'border-radius': '8px',
            'box-shadow': '0 4px 8px rgba(0, 0, 0, 0.2)',
            'text-align': 'left',
            'position': 'relative'
        }).appendTo(overlay);

        // Close button
        $('<span>').text('×').css({
            'position': 'absolute',
            'top': '10px',
            'right': '15px',
            'cursor': 'pointer',
            'font-size': '18px',
            'font-weight': 'bold'
        }).on('click', closeDeletionPopup).appendTo(popup);

        // Title
        $('<h3>').text('Nominate for deletion').appendTo(popup);

        // Short reason input
        $('<label>').attr('for', 'deletion-short-reason').text('Short reason:').appendTo(popup);
        const shortReasonInput = $('<input>').attr({
            'type': 'text',
            'id': 'deletion-short-reason',
            'placeholder': 'Enter short reason...'
        }).css({
            'width': '100%',
            'padding': '8px',
            'margin': '10px 0'
        }).appendTo(popup);

        // Detailed reason input
        $('<label>').attr('for', 'deletion-detailed-reason').text('Detailed reason for discussion:').appendTo(popup);
        const detailedReasonInput = $('<textarea>').attr({
            'id': 'deletion-detailed-reason',
            'placeholder': 'Enter detailed reason...'
        }).css({
            'width': '100%',
            'padding': '8px',
            'height': '80px',
            'margin': '10px 0'
        }).appendTo(popup);

        // Submit button
        $('<button>').text('Submit nomination').css({
            'width': '100%',
            'padding': '10px',
            'background-color': '#0073e6',
            'color': '#fff',
            'border': 'none',
            'border-radius': '5px',
            'cursor': 'pointer'
        }).on('click', function() {
            const shortReason = shortReasonInput.val().trim();
            const detailedReason = detailedReasonInput.val().trim();
            if (shortReason && detailedReason) {
                initiateDeletionProcess(shortReason, detailedReason);
            } else {
                alert('Please provide both a short and detailed reason.');
            }
        }).appendTo(popup);
    }

    // Function to close the deletion popup
    function closeDeletionPopup() {
        $('#deletion-popup-overlay').remove();
    }

    // Function to initiate deletion process
    function initiateDeletionProcess(shortReason, detailedReason) {
        // Step 1: Tagging the article with deletion notice
        tagPageForDeletion(shortReason);

        // Step 2: Creating the deletion discussion page
        createDiscussionPage(detailedReason);

        // Step 3: Adding the discussion link to the deletion list
        addToDeletionList();

        // Close the popup
        closeDeletionPopup();
    }

    // Function to tag page with deletion notice
    function tagPageForDeletion(reason) {
        const tag = `{{rfd|${reason}}}`;
        const editSummary = 'Nominating for deletion';

        new mw.Api().postWithToken('csrf', {
            action: 'edit',
            title: mw.config.get('wgPageName'),
            prependtext: tag + '\n',
            summary: editSummary,
            watchlist: 'watch',
        }).done(function() {
            alert('Page tagged for deletion.');
        }).fail(function() {
            alert('Failed to tag the page for deletion. Please try again.');
        });
    }

    // Function to create deletion discussion page
    function createDiscussionPage(reason) {
        const discussionPageTitle = `Wikipedia:Requests for deletion/Requests/${mw.config.get('wgPageName')}`;
        const discussionContent = `{{RfD/Preload/Template|reason=${reason}}}\n\n==Deletion discussion==\nMore detailed reason why this page should be deleted:\n* ${reason}`;

        new mw.Api().postWithToken('csrf', {
            action: 'edit',
            title: discussionPageTitle,
            text: discussionContent,
            summary: 'Starting deletion discussion',
            watchlist: 'watch',
        }).done(function() {
            alert('Deletion discussion page created.');
        }).fail(function() {
            alert('Failed to create deletion discussion page. Please try again.');
        });
    }

    // Function to add deletion discussion to the list
    function addToDeletionList() {
        const deletionListTitle = 'Wikipedia:Requests for deletion/Current discussions';
        const discussionLink = `{{Wikipedia:Requests for deletion/Requests/${mw.config.get('wgPageName')}}}`;

        new mw.Api().edit(deletionListTitle, function(revision) {
            return {
                text: discussionLink + '\n' + revision.content,
                summary: 'Adding new deletion discussion',
                watchlist: 'watch',
            };
        }).done(function() {
            alert('Deletion discussion added to the list.');
        }).fail(function() {
            alert('Failed to add the discussion to the deletion list. Please try again.');
        });
    }
})();