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:48, 2 May 2025 (fix display issues). 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 NBE = {
    init: function() {
        this.addPortlet();
        this.addLinks();
        this.checkForTemplateInsertion();
    },

    addPortlet: function() {
        const portletId = 'p-nbe', portletText = 'NBE';
        const navigation = '#p-cactions'; // Targeting the actions menu
        const root = document.querySelector(navigation);

        if (root && !document.getElementById(portletId)) {
            // Create a new portlet
            const portlet = document.createElement('div');
            portlet.id = portletId;
            portlet.className = 'portal'; // Add appropriate class for styling
            portlet.innerHTML = `<h3>${portletText}</h3><ul id="nbe-links"></ul>`;
            root.parentNode.insertBefore(portlet, root.nextSibling);
        }
    },

    addLinks: function() {
        const links = [
            { type: 'coord missing', prompt: "What is the general location of the missing coordinates?" },
            { type: 'Missing information', prompt: "What information is this page missing?" },
            { type: 'Unreferenced', prompt: null }
        ];
        links.forEach(link => this.addLink(link.type, link.prompt));
    },

    addLink: function(type, prompt) {
        const linkText = type.replace(/_/g, ' ');
        const link = document.createElement('li');
        link.innerHTML = `<a href="#" class="nbe-link" data-type="${type}">${linkText}</a>`;
        
        link.querySelector('a').addEventListener('click', (e) => {
            e.preventDefault();
            this.handleTemplate(type, prompt);
        });

        document.getElementById('nbe-links').appendChild(link);
    },

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

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

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

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

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

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

    checkForTemplateInsertion: function() {
        const urlParams = new URLSearchParams(window.location.search);
        const type = urlParams.get('templateType');
        const prompt = urlParams.get('promptMessage');
        if (this.isEditMode() && type) this.handleTemplate(type, prompt);
    }
};

NBE.init();