Jump to content

User:Macaw*/NBE.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Macaw* (talk | contribs) at 17:06, 2 May 2025 (revert). 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.
// NBE or Not Bad Edits was created by Macaw*

const enableMissingCoordinates = '1'; 
const enableUnreferenced = '1'; 
const enableMissingInfo = '1'; 

const NBE = {
    init: function() {
        this.addPortlet();
        this.addLinks();
        this.checkForTemplateInsertion();
    },

    addPortlet: function() {
        const portletId = 'p-nbe', portletText = 'NBE';
        const navigation = mw.config.get('skin') === 'timeless' ? '#page-tools .sidebar-inner' : '#right-navigation';
        const nextNodeId = mw.config.get('skin') === 'timeless' ? 'p-userpagetools' : 'p-cactions';

        const root = document.querySelector(navigation);
        if (root && !document.getElementById(portletId)) {
            mw.util.addPortlet(portletId, portletText, '#' + nextNodeId);
        }
    },

    addLinks: function() {
        if (enableMissingCoordinates === '1') this.addLink('coord missing', "What is the general location of the missing coordinates?");
        if (enableMissingInfo === '1') this.addLink('Missing information', "What information is this page missing?");
        if (enableUnreferenced === '1') this.addLink('Unreferenced', null);
    },

    addLink: function(templateType, promptMessage) {
        const link = mw.util.addPortletLink('p-nbe', '#', templateType === 'coord missing' ? 'Missing Coordinates' : templateType === 'Missing information' ? 'Missing Information' : 'Unreferenced', templateType);
        
        link.addEventListener('click', (event) => {
            event.preventDefault();
            this.handleTemplate(templateType, promptMessage);
        });
    },

    handleTemplate: function(templateType, promptMessage) {
        if (!this.isEditMode()) {
            this.redirectToEditMode(templateType, promptMessage);
            return;
        }

        const userInput = promptMessage ? prompt(promptMessage) : null;
        let templateString = '';

        if (templateType === 'coord missing' && userInput) {
            templateString = `{{coord missing|${userInput}}}\n`;
        } else if (templateType === 'Missing information' && userInput) {
            templateString = `{{Missing information|${userInput}}}\n`;
        } else if (templateType === 'Unreferenced') {
            const currentDate = new Date().toLocaleString('en-US', { year: 'numeric', month: 'long' });
            templateString = `{{Unreferenced|date=${currentDate}}}\n`;
        }

        if (templateString) {
            document.editform.wpTextbox1.value = templateString + document.editform.wpTextbox1.value; 
            document.editform.wpSummary.value = `Added ${templateType} Template with [[User:Macaw*/NBE|Not Bad Edits]]`; 
            document.editform.submit();
        }
    },

    isEditMode: function() {
        return document.getElementById('editform') !== null;
    },

    redirectToEditMode: function(templateType, promptMessage) {
        const editUrl = mw.util.getUrl(mw.config.get('wgPageName'), { action: 'edit', templateType, promptMessage });
        window.location.href = editUrl;
    },

    checkForTemplateInsertion: function() {
        const urlParams = new URLSearchParams(window.location.search);
        const templateType = urlParams.get('templateType');
        const promptMessage = urlParams.get('promptMessage');

        if (this.isEditMode() && templateType) {
            const userInput = promptMessage ? prompt(promptMessage) : null;
            let templateString = '';

            if (templateType === 'coord missing' && userInput) {
                templateString = `{{coord missing|${userInput}}}\n`;
            } else if (templateType === 'Missing information' && userInput) {
                templateString = `{{Missing information|${userInput}}}\n`;
            } else if (templateType === 'Unreferenced') {
                const currentDate = new Date().toLocaleString('en-US', { year: 'numeric', month: 'long' });
                templateString = `{{Unreferenced|date=${currentDate}}}\n`;
            }

            if (templateString) {
                document.editform.wpTextbox1.value = templateString + document.editform.wpTextbox1.value; 
                document.editform.wpSummary.value = `Added ${templateType} Template with [[User:Macaw*/NBE|Not Bad Edits]]`; 
                document.editform.submit();
            }
        }
    }
};

NBE.init();