User:Macaw*/NBE.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | This user script seems to have a documentation page at User:Macaw*/NBE. |
// 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';
const portletText = 'NBE';
const navigation = '#p-cactions'; // Targeting the actions menu
// Create the portlet container
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>`;
// Insert the portlet after the actions menu
const actionsMenu = document.querySelector(navigation);
if (actionsMenu && !document.getElementById(portletId)) {
actionsMenu.parentNode.insertBefore(portlet, actionsMenu.nextSibling);
}
// Ensure the portlet is visible
portlet.style.display = 'block';
},
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 linkItem = document.createElement('li');
linkItem.innerHTML = `<a href="#" class="nbe-link" data-type="${type}">${linkText}</a>`;
linkItem.querySelector('a').addEventListener('click', (e) => {
e.preventDefault();
this.handleTemplate(type, prompt);
});
document.getElementById('nbe-links').appendChild(linkItem);
},
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);
}
};
// Ensure the script runs after the DOM is fully loaded
document.addEventListener('DOMContentLoaded', function() {
NBE.init();
});