User:L10nM4st3r/maintenance tools.js

From Uncyclopedia, the content-free encyclopedia
Jump to navigation Jump to search

Note: After saving, you have to bypass your browser's cache to see the changes.

  • Internet Explorer: hold down the Ctrl key and click the Refresh or Reload button, or press Ctrl+F5.
  • Firefox: hold down the Shift key while clicking Reload; alternatively press Ctrl+F5 or Ctrl-Shift-R.
  • Opera, Konqueror and Safari users can just click the Reload button.
  • Chrome: press Ctrl+F5 or Shift+F5
//<nowiki>
{
const pagePath = "https://" + mw.config.get("wgServerName") + "/wiki/{path}";
var mwAPI = new mw.Api();
var noticeUi = document.getElementById("lionmaster-wikiEditingTools-notice");

if (noticeUi) {
  noticeUi.remove()
  var mainUi = document.getElementById("lionmaster-wikiEditingTools-UI");
  mainUi.style = "display:block"
  updateData()
}

function updateData() {
  var scripterrors_content = document.getElementById("lionmaster-wikiEditingTools-scripterrs-content");

  function doLoad_scripterrs(continueId, cmcontinue) {
    getPagesInCategory("Pages_with_script_errors", (title) => {
        return `<br/><a href="${pagePath.replace("{path}", title)}">${title}</a>`
      }, scripterrors_content, (data) => {
        if ("continue" in data) var button = createButton("Load More", "Load more results", () => {
          doLoad_scripterrs(data.continue.continue, data.continue.cmcontinue);
          button.remove();
        }, scripterrors_content)
      }, continueId, cmcontinue)
  };

  if (scripterrors_content) doLoad_scripterrs()


  var temploop_content = document.getElementById("lionmaster-wikiEditingTools-temploop-content");

  function doLoad_temploop(continueId, cmcontinue) {
    getPagesInCategory("Pages_with_template_loops", (title) => {
        return `<br/><a href="${pagePath.replace("{path}", title)}">${title}</a>`
      }, temploop_content, (data) => {
        if ("continue" in data) var button = createButton("Load More", "Load more results", () => {
          doLoad_scripterrs(data.continue.continue, data.continue.cmcontinue);
          button.remove();
        }, temploop_content)
      }, continueId, cmcontinue)
  };

  if (temploop_content) doLoad_temploop()
}


function getPagesInCategory(category, htmlPerItem, object, then, continueId, cmcontinue) {
  var request = {
	"action": "query",
	"list": "categorymembers",
	"cmtitle": "Category:" + category
  }
  if (continueId) {
    request.continue = continueId;
    request.cmcontinue = cmcontinue;
  }

  mwAPI.get(request).then((data) => {
    var text = "";
    for (page of data.query.categorymembers) {
      text += htmlPerItem(page.title)
    }
    if (text.length > 0) {
      if (!object.innerHTML.trim().length > 0) object.innerHTML = text
      else object.innerHTML += text;
    }
    else if (!object.innerHTML.trim().length > 0) object.innerHTML = "<i>No pages in this category.</i>"

    if (then) then(data)
  })
}


// Creates a button with correct styling and functionality.
function createButton(text, tooltipText, onClick, addTo, style) {
    const button = document.createElement("input");

    button.setAttribute('title', tooltipText);
    button.setAttribute('type', "button");
    button.value = text;
    button.style = "cursor:pointer;" + (style != undefined ? style : "display:block; width:100%;");

    if (onClick != null) button.addEventListener('click', onClick);

    if (addTo != null) addTo.appendChild(button);
    return button;
}


}
//</nowiki>