Jump to content

User:DreamRimmer/test.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by DreamRimmer (talk | contribs) at 07:14, 4 September 2023 (Created page with '"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 t...'). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
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.
"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();
  });
});