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 13:05, 2 May 2025 (please work). 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'; 

NBE.addPortlet = function() {
	/** @type {string} id of the target navigation area (skin dependent, on vector either of "#left-navigation", "#right-navigation", or "#mw-panel") */
	let navigation;

	/** @type {string} id of the portlet menu to create, preferably start with "p-". */
	let id;

	/** @type {string} name of the portlet menu to create. Visibility depends on the class used. */
	let text;

	/** @type {Node} the id of the node before which the new item should be added, should be another item in the same list, or undefined to place it at the end. */
	let nextnodeid;

	switch (mw.config.get('skin')) {
		case 'vector':
		case 'vector-2022':
			navigation = '#right-navigation';
			id = 'p-nbe';
			text = 'NBE';
			// In order to get mw.util.addPortlet to generate a dropdown menu in vector and vector-2022, the nextnodeid must be p-cactions. Any other nextnodeid will generate a non-dropdown portlet instead.
			nextnodeid = 'p-cactions';
			break;
		case 'timeless':
			navigation = '#page-tools .sidebar-inner';
			id = 'p-twinkle';
			text = 'Twinkle';
			nextnodeid = 'p-userpagetools';
			break;
		default:
			navigation = null;
			id = 'p-cactions';
	}

	if (navigation === null) {
		return id;
	}

	// make sure navigation is a valid CSS selector
	const root = document.querySelector(navigation);
	if (!root) {
		return id;
	}

	// if we already created the portlet, return early. we don't want to create it again.
	const item = document.getElementById(id);
	if (item) {
		return id;
	}

	mw.util.addPortlet(id, text, '#' + nextnodeid);

	// The Twinkle dropdown menu has been added to the left of p-cactions, since that is the only spot that will create a dropdown menu. But we want it on the right. Move it to the right.
	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');
		$('#p-twinkle-dropdown').insertAfter($landmark);

if (enableMissingCoordinates === '1') {
    mw.loader.using('mediawiki.util', function () {
        mw.hook('wikipage.content').add(function () {
            const link = mw.util.addPortletLink('p-cactions', '#', 'Missing Coordinates', 'Missing Coordinates', 'Mark as missing coordinates.');
            $(link).click(function (event) {
                event.preventDefault();
                const userInput = prompt("What is the general location of the missing coordinates?");
                if (userInput !== null) {
                    document.editform.wpTextbox1.value = `{{coord missing|${userInput}}}\n` + document.editform.wpTextbox1.value; 
                    document.editform.wpSummary.value = "Added Missing Coordinates Template with [[User:Macaw*/NBE|Not Bad Edits]]"; 
                    document.editform.submit();
                }
            });
        });
    });
}

/**
 * Builds a portlet menu if it doesn't exist yet, and adds a portlet link. This function runs at the top of every Twinkle module, ensuring that the first module to be loaded adds the portlet, and that every module can add a link to itself to the portlet.
 *
 * @param task Either a URL for the portlet link or a function to execute.
 */
Twinkle.addPortletLink = function(task, text, id, tooltip) {
	// Create a portlet to hold all the portlet links (if not created already). And get the portletId.
	const portletId = Twinkle.addPortlet();

	// Create a portlet link and add it to the portlet.
	const link = mw.util.addPortletLink(portletId, typeof task === 'string' ? task : '#', text, id, tooltip);

	// Related to the hidden peer gadget that prevents jumpiness when the page first loads
	$('.client-js .skin-vector #p-cactions').css('margin-right', 'initial');

	// Add a click listener for the portlet link
	if (typeof task === 'function') {
		$(link).on('click', (ev) => {
			task();
			ev.preventDefault();
		});
	}



if (enableMissingInfo === '1') {
    mw.loader.using('mediawiki.util', function () {
        mw.hook('wikipage.content').add(function () {
            const link = mw.util.addPortletLink('p-cactions', '#', 'Missing Information', 'Missing Information', 'Mark as missing information.');
            $(link).click(function (event) {
                event.preventDefault();
                const userInput = prompt("What information is this page missing?");
                if (userInput !== null) {
                    document.editform.wpTextbox1.value = `{{Missing information|${userInput}}}\n` + document.editform.wpTextbox1.value; 
                    document.editform.wpSummary.value = "Added Missing Information Template with [[User:Macaw*/NBE|Not Bad Edits]]"; 
                    document.editform.submit();
                }
            });
        });
    });
}

if (enableUnreferenced === '1') {
    mw.loader.using('mediawiki.util', function () {
        mw.hook('wikipage.content').add(function () {
            const link = mw.util.addPortletLink('p-cactions', '#', 'Unreferenced', 'Unreferenced', 'Mark as unreferenced.');
            $(link).click(function (event) {
                event.preventDefault();
                const currentDate = new Date().toLocaleString('en-US', { year: 'numeric', month: 'long' });
                document.editform.wpTextbox1.value = `{{Unreferenced|date=${currentDate}}}\n` + document.editform.wpTextbox1.value; 
                document.editform.wpSummary.value = "Added Unreferenced Template with [[User:Macaw*/NBE|Not Bad Edits]]"; 
                document.editform.submit();
            });
        });
    });
}