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 16:28, 2 May 2025 (more bugs). 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*

// CONFIGURATION: Enable features by setting to '1' or '0'
const enableMissingCoordinates = '1'; 
const enableUnreferenced = '1'; 
const enableMissingInfo = '1'; 

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

    addPortlet: function() {
        let navigation, portletId = 'p-nbe', portletText = 'NBE', nextNodeId;

        switch (mw.config.get('skin')) {
            case 'vector':
            case 'vector-2022':
                navigation = '#right-navigation';
                nextNodeId = 'p-cactions';
                break;
            case 'timeless':
                navigation = '#page-tools .sidebar-inner';
                nextNodeId = 'p-userpagetools';
                break;
            default:
                return; // Exit if skin is not supported
        }

        const root = document.querySelector(navigation);
        if (!root) return;

        // Add the portlet if it doesn't already exist
        if (!document.getElementById(portletId)) {
            mw.util.addPortlet(portletId, portletText, '#' + nextNodeId);
        }
    },

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

    addLink: function(task, text, id, tooltip, templateType, promptMessage) {
        const portletId = 'p-nbe';
        const link = mw.util.addPortletLink(portletId, task, text, id, tooltip);
        
        // Attach the click event handler
        link.addEventListener('click', (event) => {
            event.preventDefault(); // Prevent default link behavior
            this.handleTemplate(templateType, promptMessage); // Call the consolidated handler function
        });
    },

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

        let userInput = null;
        if (promptMessage) {
            userInput = prompt(promptMessage);
        }

        let templateString = '';
        if (templateType === 'coord missing' && userInput !== null) {
            templateString = `{{coord missing|${userInput}}}\n`;
        } else if (templateType === 'Missing information' && userInput !== null) {
            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() {
        // Check if the page is in edit mode by looking for the edit form
        return document.getElementById('editform') !== null;
    },

    redirectToEditMode: function(templateType, promptMessage) {
        // Redirect to the edit page with parameters
        const editUrl = mw.util.getUrl(mw.config.get('wgPageName'), { action: 'edit', templateType: templateType, prompt