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. |
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();