Jump to content

User:DreamRimmer/OneClickRemover.js

From Wikipedia, the free encyclopedia
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
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.
// <nowiki>
mw.loader.using(['mediawiki.util', 'mediawiki.api'], function() {
    'use strict';

    $(document).ready(function() {
        var config = mw.config.get([
            'wgArticleId',
            'wgNamespaceNumber',
            'wgPageName',
            'wgAction',
            'wgIsMainPage'
        ]);

        if (config.wgIsMainPage) return;
        if (config.wgNamespaceNumber === -1) return;

        var allowedNamespaces = [1, 3, 4, 5, 7, 9, 11, 15, 119];
        if (allowedNamespaces.indexOf(config.wgNamespaceNumber) === -1) return;
        if (config.wgAction !== 'view') return;

        function addRemoveButtons() {
            $('.section-remove-btn').remove();
            $('.ext-discussiontools-init-section-bar').each(function() {
                var bar = $(this);
                if (bar.find('.section-remove-btn').length > 0) return;
                var editLink = bar.closest('.mw-heading2, .mw-heading').find('.mw-editsection a').not('.mw-editsection-visualeditor').first();
                if (editLink.length === 0) return;
                var sectionNumber = getSectionNumber(editLink.attr('href'));
                if (!sectionNumber) return;
                var sectionTitle = bar.closest('.mw-heading2, .mw-heading').find('h2,h3').text().trim();
                if (!sectionTitle) {
                    sectionTitle = bar.closest('.mw-heading2, .mw-heading').text().trim();
                }
                var removeBtn = $('<span>')
                    .addClass('ext-discussiontools-init-section-metaitem section-remove-btn')
                    .html(
                        '<a href="#" title="Click to remove this section" style="vertical-align: middle; text-decoration: none;">' +
                        '<img src="/media/wikipedia/commons/d/de/OOjs_UI_icon_trash-destructive.svg" ' +
                        'alt="Remove" style="width:18px; height:18px; filter: drop-shadow(0 0 0 #d33);"/>' +
                        '</a>'
                    )
                    .data('section-number', sectionNumber)
                    .data('section-title', sectionTitle);
                removeBtn.find('a').on('click', function(e) {
                    e.preventDefault();
                    showRemoveInterface(bar, sectionNumber, sectionTitle);
                });
                var meta = bar.find('.ext-discussiontools-init-section-metadata');
                if (meta.length > 0) {
                    meta.append(removeBtn);
                } else {
                    bar.append(removeBtn);
                }
            });
        }

        function getSectionNumber(editUrl) {
            var match = editUrl && editUrl.match(/[?&]section=(\d+)/);
            return match ? parseInt(match[1]) : null;
        }

        function showRemoveInterface(parentElem, sectionNumber, sectionTitle) {
            $('.remove-interface').remove();
            var interfaceDiv = $('<div>')
                .addClass('remove-interface')
                .css({
                    'background': '#fefffe',
                    'border': '1px solid #000',
                    'padding': '10px',
                    'margin': '10px 0',
                    'border-radius': '4px'
                });

            var sectionInfo = $('<div>')
                .css({
                    'font-size': '0.9em',
                    'color': '#666',
                    'margin-bottom': '8px'
                })
                .text('Section ' + sectionNumber + ': ' + sectionTitle);

            var reasonInput = $('<input>')
                .attr('type', 'text')
                .attr('placeholder', 'e.g., duplicate, malformed, spam')
                .css({
                    'width': '300px',
                    'padding': '5px',
                    'margin-right': '10px',
                    'border': '1px solid #ccc',
                    'border-radius': '3px'
                });

            var saveBtn = $('<button>')
                .text('Remove Section')
                .css({
                    'background': '#d33',
                    'color': 'white',
                    'border': 'none',
                    'padding': '6px 12px',
                    'margin-right': '5px',
                    'border-radius': '3px',
                    'cursor': 'pointer'
                });

            var cancelBtn = $('<button>')
                .text('Cancel')
                .css({
                    'background': '#878787',
                    'color': 'white',
                    'border': 'none',
                    'padding': '6px 12px',
                    'border-radius': '3px',
                    'cursor': 'pointer'
                });

            saveBtn.on('click', function() {
                var reason = reasonInput.val().trim();
                if (!reason) {
                    alert('Please enter a reason.');
                    return;
                }
                removeSection(sectionNumber, sectionTitle, reason);
            });
            cancelBtn.on('click', function() {
                interfaceDiv.remove();
            });

            reasonInput.on('keypress', function(e) {
                if (e.which === 13) {
                    saveBtn.click();
                }
            });

            interfaceDiv.append(sectionInfo);
            interfaceDiv.append('Reason: ');
            interfaceDiv.append(reasonInput);
            interfaceDiv.append(saveBtn);
            interfaceDiv.append(cancelBtn);
            parentElem.after(interfaceDiv);
        }

        function removeSection(sectionNumber, sectionTitle, reason) {
            var editSummary = 'Removed section - ' + reason + ' ([[:en:User:DreamRimmer/OneClickRemover.js|OneClickRemover.js]])';
            mw.notify('Removing section...', { type: 'info' });

            var api = new mw.Api();
            var pageid = config.wgArticleId;

            api.postWithToken('csrf', {
                action: 'edit',
                pageid: pageid,
                section: sectionNumber,
                text: '',
                summary: editSummary,
                minor: true
            }).done(function(result) {
                if (result.edit && result.edit.result === 'Success') {
                    mw.notify('Section removed. Loading diff...', { type: 'success' });
                    setTimeout(function() {
                        var newRevisionId = result.edit.newrevid;
                        location.href = mw.util.getUrl(config.wgPageName) + '?diff=' + newRevisionId;
                    }, 2000);
                } else {
                    alert('could not remove section');
                }
            }).fail(function(error) {
                alert('Failed: ' + error);
            });
        }

        mw.util.addCSS(`
            .section-remove-btn a img {
                filter: drop-shadow(0 0 0 #d33);
                opacity: 0.85;
                transition: opacity 0.15s, filter 0.15s;
            }
            .section-remove-btn a:hover img {
                opacity: 1;
                filter: drop-shadow(0 0 0 #b32);
            }
            .remove-interface button:hover {
                opacity: 0.8;
                transform: translateY(-1px);
            }
            .remove-interface input:focus {
                outline: 2px solid #007cba;
                outline-offset: 1px;
                border-color: #007cba;
            }
        `);

        addRemoveButtons();
    });
});
// </nowiki>