Jump to content

User:Macaw*/NBE.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.
const enableMissingCoordinates = '1'; 
const enableUnreferenced = '1'; 
const enableMissingInfo = '1'; 

const NBE = {
    init() {
        this.addPortlet();
        this.addLinks();
        this.checkForTemplateInsertion();
    },
    addPortlet() {
        const portletId = 'p-nbe', portletText = 'NBE', nav = mw.config.get('skin') === 'timeless' ? '#page-tools .sidebar-inner' : '#right-navigation';
        if (document.querySelector(nav) && !document.getElementById(portletId)) {
            mw.util.addPortlet(portletId, portletText, '#' + (mw.config.get('skin') === 'timeless' ? 'p-userpagetools' : 'p-cactions'));
        }
        if (mw.config.get('skin') === 'vector') $('#p-nbe').insertAfter('#p-cactions');
        else if (mw.config.get('skin') === 'vector-2022') {
            const $landmark = $('#right-navigation > .vector-page-tools-landmark');
            if ($landmark.length) $('#p-nbe-dropdown').insertAfter($landmark);
        }
    },
    addLinks() {
        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(type, promptMsg) {
        const linkText = type === 'coord missing' ? 'Missing Coordinates' : type === 'Missing information' ? 'Missing Information' : 'Unreferenced';
        const link = mw.util.addPortletLink('p-nbe', '#', linkText, type);
        link.addEventListener('click', (e) => {
            e.preventDefault();
            this.handleTemplate(type, promptMsg);
        });
    },
    handleTemplate(type, promptMsg) {
        if (!this.isEditMode()) {
            this.redirectToEditMode(type, promptMsg);
            return;
        }
        let templateString = '';
        if (type === 'coord missing') {
            const userInput = prompt(promptMsg);
            if (userInput) templateString = `{{coord missing|${userInput}}}\n`;
        } else if (type === 'Missing information') {
            const userInput = prompt(promptMsg);
            if (userInput) templateString = `{{Missing information|${userInput}}}\n`;
        } else if (type === 'Unreferenced') {
            templateString = `{{Unreferenced|date=${new Date().toLocaleString('en-US', { year: 'numeric', month: 'long' })}}}\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() {
        return document.getElementById('editform') !== null;
    },
    redirectToEditMode(type, promptMsg) {
        window.location.href = mw.util.getUrl(mw.config.get('wgPageName'), { action: 'edit', templateType: type, promptMessage: promptMsg });
    },
    checkForTemplateInsertion() {
        const urlParams = new URLSearchParams(window.location.search);
        const type = urlParams.get('templateType');
        if (this.isEditMode() && type) {
            let templateString = '';
            if (type === 'coord missing') {
                const userInput = prompt("What is the general location of the missing coordinates?");
                if (userInput) templateString = `{{coord missing|${userInput}}}\n`;
            } else if (type === 'Missing information') {
                const userInput = prompt("What information is this page missing?");
                if (userInput) templateString = `{{Missing information|${userInput}}}\n`;
            } else if (type === 'Unreferenced') {
                templateString = `{{Unreferenced|date=${new Date().toLocaleString('en-US', { year: 'numeric', month: 'long' })}}}\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();
            }
        }
    }
};

NBE.init();