User:DreamRimmer/test.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. |
![]() | Documentation for this user script can be added at User:DreamRimmer/test. |
"use strict";
mw.loader.using(["mediawiki.util"], () => {
if (mw.config.get("wgNamespaceNumber") < 0)
return;
if (!mw.config.get("wgIsProbablyEditable"))
return;
const link = mw.util.addPortletLink(
mw.config.get("skin") === "minerva" ? "p-tb" : "p-cactions",
"#",
"Null edit",
"null-edit"
);
link.addEventListener("click", async (event) => {
event.preventDefault();
const reason = prompt("Please enter a reason for the null edit:");
if (!reason) {
return; // User canceled or entered an empty reason, do nothing.
}
mw.notify("Null editing page...", { tag: "null-edit-notification" });
try {
// Get the current content of the page.
const currentPageContent = await new mw.Api().get({
action: "query",
titles: mw.config.get("wgPageName"),
prop: "revisions",
rvprop: "content",
});
// Add a space to the end of the content.
const newContent = currentPageContent.query.pages[Object.keys(currentPageContent.query.pages)[0]].revisions[0].content + " ";
// Perform the null edit with the updated content and edit summary.
await new mw.Api().edit(mw.config.get("wgPageName"), {
text: newContent,
summary: `Null edit: ${reason}`,
minor: true,
});
} catch (error) {
console.error(error);
return mw.notify("An error occurred when null editing this page!", {
type: "error",
tag: "null-edit-notification",
});
}
mw.notify("Successfully null edited page, reloading...", {
type: "success",
tag: "null-edit-notification",
});
window.location.reload();
});
});