User:Stumblean/common.js: Difference between revisions

Stumblean (talk | contribs)
No edit summary
Tags: Mobile edit Mobile web edit Advanced mobile edit
Stumblean (talk | contribs)
No edit summary
Tags: Mobile edit Mobile web edit Advanced mobile edit
 
(64 intermediate revisions by the same user not shown)
Line 1: Line 1:
/// Twinkle is here because it's really good for vandalism. BTW DELETE THIS LINE!!!!
(function () {
  function constrainPosition(x, y, width, height) {
    const winWidth = window.innerWidth;
    const winHeight = window.innerHeight;
    if (x < 0) x = 0;
    if (y < 0) y = 0;
    if (x + width > winWidth) x = winWidth - width;
    if (y + height > winHeight) y = winHeight - height;
    return [x, y];
  }


/// Twinkle
  const toggle = document.createElement("button");
  toggle.id = "delta-toggle";
  Object.assign(toggle.style, {
    position: "fixed",
    bottom: "20px",
    right: "20px",
    padding: "0",
    background: "#2b2b2b",
    border: "none",
    borderRadius: "12px",
    zIndex: 9998,
    cursor: "move",
    userSelect: "none",
    touchAction: "none",
  });


mw.loader.load('//vi.wikipedia.org/w/index.php?title=User:Plantaest/TwinkleMobile.js&action=raw&ctype=text/javascript');
  const icon = document.createElement("img");
// [[User:Plantaest/TwinkleMobile]] by [[:vi:User:Plantaest]]
  icon.src = "https://files.catbox.moe/uodscw.png";
  icon.alt = "Delta";
  Object.assign(icon.style, {
    width: "48px",
    height: "48px",
    borderRadius: "12px",
    display: "block",
    pointerEvents: "none",
  });
  toggle.appendChild(icon);


/// Kill it with fire (mass undo)
  const menu = document.createElement("div");
  menu.id = "delta-menu";
  Object.assign(menu.style, {
    display: "none",
    position: "fixed",
    bottom: "80px",
    right: "20px",
    width: "300px",
    background: "#2b2b2b",
    color: "#fff",
    borderRadius: "10px",
    padding: "15px",
    boxShadow: "0 0 10px rgba(0,0,0,0.5)",
    zIndex: 9999,
    fontFamily: "monospace",
    cursor: "move",
    userSelect: "none",
    touchAction: "none",
  });


mw.loader.load('//en.wikipedia.org/w/index.php?title=User:Alexis_Jazz/Kill-It-With-Fire.js&action=raw&ctype=text/javascript');
  const title = document.createElement("div");
  title.textContent = "Delta Online";
  Object.assign(title.style, {
    textAlign: "center",
    fontSize: "18px",
    marginBottom: "10px",
    userSelect: "text",
    cursor: "default",
  });


/// null edit stuff
  const textarea = document.createElement("textarea");
  textarea.placeholder = "Enter JavaScript...";
  Object.assign(textarea.style, {
    width: "100%",
    height: "120px",
    background: "#1e1e1e",
    color: "#fff",
    border: "none",
    borderRadius: "6px",
    padding: "10px",
    resize: "none",
    boxSizing: "border-box",
    cursor: "auto",
    userSelect: "text",
  });


{{subst:Iusc|User:Ahecht/Scripts/refresh.js|User:Ahecht/Scripts/refresh}}
  const runBtn = document.createElement("button");
  runBtn.textContent = "Run Code";
  Object.assign(runBtn.style, {
    marginTop: "10px",
    width: "100%",
    padding: "10px",
    background: "#4caf50",
    color: "#fff",
    border: "none",
    borderRadius: "6px",
    cursor: "pointer",
  });


mw.loader.load('/w/index.php?title=User:BrandonXLF/NullEdit.js&action=raw&ctype=text/javascript'); // [[User:BrandonXLF/NullEdit.js]]
  runBtn.onclick = () => {
    try {
      eval(textarea.value);
    } catch (e) {
      alert("Error: " + e.message);
    }
  };
 
  function makeDraggable(element, excludeElements = [], onClick) {
    let isDragging = false;
    let dragStartX = 0;
    let dragStartY = 0;
    let elemStartLeft = 0;
    let elemStartTop = 0;
    let moved = false;
 
    function onDragStart(x, y, target) {
      if (excludeElements.includes(target)) return false;
      isDragging = true;
      moved = false;
      dragStartX = x;
      dragStartY = y;
      const rect = element.getBoundingClientRect();
      elemStartLeft = rect.left;
      elemStartTop = rect.top;
      return true;
    }
 
    function onDragMove(x, y) {
      if (!isDragging) return;
      const dx = x - dragStartX;
      const dy = y - dragStartY;
      if (Math.abs(dx) > 3 || Math.abs(dy) > 3) moved = true;


/// Mass edit (really useful, also had to put full script in bc I couldn't find a mw loader load thing)
      let newLeft = elemStartLeft + dx;
      let newTop = elemStartTop + dy;
      [newLeft, newTop] = constrainPosition(
        newLeft,
        newTop,
        element.offsetWidth,
        element.offsetHeight
      );
      element.style.left = newLeft + "px";
      element.style.top = newTop + "px";
      element.style.bottom = "auto";
      element.style.right = "auto";
    }


function doMassEdit() {
    function onDragEnd() {
    document.getElementById("wpMassEditSubmit").disabled = true;
      if (!isDragging) return;
    var articles = document.getElementById("wpMassEditPages").value.split("\n");
      isDragging = false;
    if(articles.length == 0) return;
      if (!moved && typeof onClick === "function") {
    var newtext = document.getElementById("wpMassEditText1").value;
         onClick();
    var text2 = document.getElementById("wpMassEditText2").value;
      }
    var wpMassEditType = document.getElementById("wpMassEditType").value, wpMassEditSummary = document.getElementById("wpMassEditSummary").value, edited = 0, failed = new Array(), error = new Array();
    var wpMassEditMinor = document.getElementById("wpMassEditMinor").checked;
    for(i=0;i<articles.length;i++) {
        var article = articles[i];
        if(article.length > 0) {
            var req = new XMLHttpRequest();
            req.open("GET", mw.config.get('wgScriptPath') + "/api.php?format=json&action=query&prop=info&meta=tokens&type=csrf&titles=" + encodeURIComponent(article), false);
            req.send(null);
            var query = eval("(" + req.responseText + ")").query;
            var edittoken = query.tokens.csrftoken;
            var response = query.pages;
            for(var index in response) {
                var info = response[index];
var postdata = "format=json"
                            + "&action=edit&watchlist=nochange"
                            + "&title=" + encodeURIComponent(article)
                            + "&summary=" + encodeURIComponent(wpMassEditSummary)
                            + "&token=" + encodeURIComponent(edittoken);
                if(wpMassEditMinor)
                postdata += "&minor=1";
                else
                postdata += "&notminor=1";
if(wpMassEditType == "prepend"){
postdata += "&prependtext=" + encodeURIComponent(newtext + "\n");
}
else if(wpMassEditType == "append"){
postdata += "&appendtext=" + encodeURIComponent("\n" + newtext);
}
else if(wpMassEditType == "replacepage"){
postdata += "&text=" + encodeURIComponent(newtext);
}
else if(wpMassEditType == "replacetext" || wpMassEditType == "replacetextg" || wpMassEditType == "bothpend"){
var pagetext = gettext(article);
if(wpMassEditType == "replacetextg"){
newtext = newtext.replace(/([.*+?|(){}\[\]\\])/g, "\\$1");
pagetext = pagetext.replace(new RegExp(newtext, "g"), text2);
}
else if(wpMassEditType == "replacetext"){
pagetext = pagetext.replace(newtext, text2);
}
else
pagetext = newtext + pagetext + text2;
postdata += "&text=" + encodeURIComponent(pagetext);
}
                var req = new XMLHttpRequest();
                req.open("POST", mw.config.get('wgScriptPath') + "/api.php", false);
                req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                req.setRequestHeader("Content-length", postdata.length);
                req.send(postdata);
                if(eval("(" + req.responseText + ")").edit ) { //If edited, update the count and the button.
                    edited++;
                    document.getElementById("wpMassEditSubmit").value = "(" + edited + ")";
                } else { //If not edited, add the title to the "failed" array and a description of the error to the "error" array.
                    failed.push(article);
                    error.push(eval("(" + req.responseText + ")").error.info);
                }
            }
         }
        if(!articles[i+1]) {
            document.getElementById("wpMassEditSubmit").value = "Done (" + edited + ")";
            if(failed.length > 0) {
                var linkedList = "";
                for(x=0; x<failed.length; x++) {
                    linkedList += "<li><a href=\"" + mw.config.get('wgScript') + "?title=" + encodeURIComponent(failed[x]) + "\">" + failed[x] + "</a>: " + error[x] + "</li>"; //Links the titles in the "failed" array
                }
                document.getElementById("wpMassEditFailedContainer").innerHTML += '<br /><b>Failed edits:</b><ul>' + linkedList + '</ul>';
            }
           
        }
     }
     }
}


function gettext(title){
    element.addEventListener("mousedown", (e) => {
var req = new XMLHttpRequest();
      if (!onDragStart(e.clientX, e.clientY, e.target)) return;
req.open("GET", mw.config.get('wgScriptPath') + "/api.php?action=query&prop=revisions&rvprop=content&format=json&indexpageids=1&titles="+encodeURIComponent(title), false);
      e.preventDefault();
req.send(null);
    });
var response = eval('(' + req.responseText + ')');
    document.addEventListener("mousemove", (e) => onDragMove(e.clientX, e.clientY));
pageid = response['query']['pageids'][0];
    document.addEventListener("mouseup", onDragEnd);
if (pageid == "-1") {
 
delete req;
    element.addEventListener("touchstart", (e) => {
return '';
      if (!e.touches || !e.touches[0]) return;
}
      if (!onDragStart(e.touches[0].clientX, e.touches[0].clientY, e.target)) return;
var pagetext = response['query']['pages'][pageid]['revisions'][0]['*'];
      e.preventDefault();
return pagetext;
     });
}
    document.addEventListener(
function masseditform() {
      "touchmove",
     var bodyContent;
      (e) => {
switch (mw.config.get('skin')) {
        if (!e.touches || !e.touches[0]) return;
case 'modern':
        onDragMove(e.touches[0].clientX, e.touches[0].clientY);
bodyContent = 'mw_contentholder';
      },
break;
      { passive: false }
case 'cologneblue':
     );
bodyContent = 'article';
     document.addEventListener("touchend", onDragEnd);
break;
     document.addEventListener("touchcancel", onDragEnd);
case 'vector':
  }
case 'monobook':
 
case 'vector':
  function toggleMenu() {
default:
    menu.style.display = menu.style.display === "none" ? "block" : "none";
bodyContent = 'bodyContent';
  }
break;
 
}
  makeDraggable(toggle, [], toggleMenu);
     document.getElementsByTagName("h1")[0].textContent = "Tim's mass edit tool";
  makeDraggable(menu, [textarea, runBtn]);
     document.title = "Tim's mass-editing tool - Wikipedia, the free encyclopedia";
 
     document.getElementById(bodyContent).innerHTML = '<h3 id="siteSub">From Wikipedia, the free encyclopedia</h3><br /><br />'
  menu.appendChild(title);
        + '<form id="wpMassEdit" name="wpMassEdit">'
  menu.appendChild(textarea);
        + '<b>If you abuse this tool, it\'s <i>your</i> fault, not mine.</b>'
  menu.appendChild(runBtn);
        + '<div id="wpMassEditFailedContainer"></div>'
  document.body.appendChild(toggle);
        + '<br /><br />'
  document.body.appendChild(menu);
            + 'Pages to edit (one on each line, please):<br />'
})();
                + '<textarea tabindex="1" name="wpMassEditPages" id="wpMassEditPages" rows="10" cols="80"></textarea>'
            + 'Text 1 (see below):<br />'
                + '<textarea tabindex="2" name="wpMassEditText1" id="wpMassEditText1" rows="10" cols="80"></textarea>'
            + 'Text 2 (see below):<br />'
                + '<textarea tabindex="3" name="wpMassEditText2" id="wpMassEditText2" rows="10" cols="80"></textarea>'
            + '<br /><br /><table style="background-color:transparent">'
                + '<tr><td>Edit type:</td>'
                    + '<td><select id="wpMassEditType">'
                        + '<option value="prepend">Prepend text 1</option>'
                        + '<option value="append">Append text 1</option>'
                        + '<option value="bothpend">Prepend text 1 and append text 2</option>'
                        + '<option value="replacetext">Replace first instance of text 1 with text 2</option>'
                        + '<option value="replacetextg">Replace all instances of text 1 with text 2</option>'
                        + '<option value="replacepage">Replace page with text 1</option>'
                    + '</select></td></tr>'
            + '<tr><td>Edit summary:</td>'
                + '<td><input type="text" id="wpMassEditSummary" name="wpMassEditSummary" maxlength="255" /></td></tr>'
            + '<tr><td>Mark edit as minor:</td>'
                + '<td><input type="checkbox" id="wpMassEditMinor" name="wpMassEditMinor"/></td></tr>'
                + '<tr><td><input type="button" id="wpMassEditSubmit" name="wpMassEditSubmit" value="Edit" onclick="doMassEdit()" /></td>'
        + '</form>';
}
var massedit_whitelist = ["Sven Manguard", "DeltaQuad.alt", "T. Canens"];
if(mw.config.get("wgNamespaceNumber") == -1 && (mw.config.get("wgTitle").toLowerCase() == "massedit") &&
(/sysop/.test(mw.config.get("wgUserGroups")) || massedit_whitelist.indexOf(mw.config.get("wgUserName")) != -1 )) $(masseditform);