User:DVRTed/dummy-edit.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 code will be executed when previewing this page.
Documentation for this user script can be added at User:DVRTed/dummy-edit.
/*
Adds an option to the toolbar to make a dummy edit on the current page (see [[WP:DUMMY]]).
It also prompts you for a custom edit summary.
*/
/* global mw, $ */
mw.loader.using(["mediawiki.util", "mediawiki.api"], function () {
const node = mw.util.addPortletLink(
"p-tb",
"#",
"Dummy edit",
"tb-dummyedit",
"Make a dummy edit to this page",
);
$(node).on("click", function (e) {
e.preventDefault();
const api = new mw.Api();
const page_title = mw.config.get("wgPageName");
api
.get({
action: "query",
titles: page_title,
prop: "revisions",
rvprop: "content|ids",
rvslots: "main",
formatversion: 2,
})
.then(function (data) {
const page = data.query.pages[0];
if (page.missing || !page.revisions?.length) {
mw.notify("Could not retrieve page content.", { type: "error" });
return;
}
const rev = page.revisions[0];
const old_text = rev.slots.main.content;
const base_rev_id = rev.revid; // to silently fail on edit conflicts
const lines = old_text.split("\n");
lines[0] = lines[0].endsWith(" ")
? lines[0].slice(0, -1)
: lines[0] + " ";
const new_text = lines.join("\n");
const edit_summary = prompt(
"Edit summary for the dummy edit (leave blank to cancel):",
"[[WP:DUMMY|Dummy edit]]",
);
if (edit_summary === null || edit_summary.trim() === "") return;
api
.postWithEditToken({
action: "edit",
title: page_title,
text: new_text,
summary: edit_summary,
baserevid: base_rev_id,
})
.then(() => {
mw.notify("Dummy edit successful!", { type: "success" });
})
.catch(function (err) {
mw.notify("Could not save page: " + err, { type: "error" });
});
})
.catch(function (err) {
mw.notify("Could not fetch page content: " + err, { type: "error" });
});
});
});