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
 
(41 intermediate revisions by the same user not shown)
Line 1: Line 1:
(function () {
(function () {
   const icon = "https://files.catbox.moe/wk78nl.jpg";
  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];
  }
 
  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",
  });
 
   const icon = document.createElement("img");
  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);


   const menu = document.createElement("div");
   const menu = document.createElement("div");
   menu.id = "mahitoMenu";
   menu.id = "delta-menu";
   Object.assign(menu.style, {
   Object.assign(menu.style, {
    display: "none",
     position: "fixed",
     position: "fixed",
     top: "20px",
     bottom: "80px",
     right: "20px",
     right: "20px",
     background: "#7889B2",
    width: "300px",
     background: "#2b2b2b",
     color: "#fff",
     color: "#fff",
    border: "2px solid #666",
     borderRadius: "10px",
     borderRadius: "10px",
     padding: "10px",
     padding: "15px",
    boxShadow: "0 0 10px rgba(0,0,0,0.5)",
    zIndex: 9999,
     fontFamily: "monospace",
     fontFamily: "monospace",
     zIndex: "99999",
     cursor: "move",
     boxShadow: "0 0 10px black",
     userSelect: "none",
     touchAction: "none",
     touchAction: "none",
   });
   });
  const header = document.createElement("div");
  header.style.display = "flex";
  header.style.alignItems = "center";
  header.style.gap = "6px";
  header.style.marginBottom = "8px";
  header.style.cursor = "grab";
  const img1 = document.createElement("img");
  img1.src = icon;
  img1.width = 15;
  img1.height = 15;


   const title = document.createElement("div");
   const title = document.createElement("div");
   title.textContent = "Mahito Menu";
   title.textContent = "Delta Online";
   title.style.fontWeight = "bold";
   Object.assign(title.style, {
  title.style.color = "#fff";
    textAlign: "center",
    fontSize: "18px",
    marginBottom: "10px",
    userSelect: "text",
    cursor: "default",
  });


   const img2 = document.createElement("img");
   const textarea = document.createElement("textarea");
   img2.src = icon;
   textarea.placeholder = "Enter JavaScript...";
  img2.width = 15;
   Object.assign(textarea.style, {
  img2.height = 15;
    width: "100%",
 
    height: "120px",
  header.appendChild(img1);
    background: "#1e1e1e",
  header.appendChild(title);
    color: "#fff",
  header.appendChild(img2);
    border: "none",
 
    borderRadius: "6px",
   function makeButton(text, onclick) {
    padding: "10px",
    const btn = document.createElement("button");
    resize: "none",
    btn.textContent = text;
     boxSizing: "border-box",
    btn.onclick = onclick;
     cursor: "auto",
    Object.assign(btn.style, {
     userSelect: "text",
      marginBottom: "6px",
      width: "100%",
      cursor: "pointer",
      background: "#45A8C5",
      color: "#fff",
      border: "none",
      padding: "5px 10px",
      borderRadius: "5px",
     });
     return btn;
  }
 
  const btn1 = makeButton("Mass undo", () => {
     prompt("Copy this into your common.js:", "mw.loader.load('//en.wikipedia.org/w/index.php?title=User:Alexis_Jazz/Kill-It-With-Fire.js&action=raw&ctype=text/javascript');");
   });
   });


   const btn2 = makeButton("Open Explanation", () => {
   const runBtn = document.createElement("button");
     alert("Open Explanation clicked");
  runBtn.textContent = "Run Code";
  Object.assign(runBtn.style, {
     marginTop: "10px",
    width: "100%",
    padding: "10px",
    background: "#4caf50",
    color: "#fff",
    border: "none",
    borderRadius: "6px",
    cursor: "pointer",
   });
   });


   menu.appendChild(header);
   runBtn.onclick = () => {
  menu.appendChild(btn1);
    try {
  menu.appendChild(btn2);
      eval(textarea.value);
   document.body.appendChild(menu);
    } catch (e) {
      alert("Error: " + e.message);
    }
   };


   // === DRAGGING ===
   function makeDraggable(element, excludeElements = [], onClick) {
  let isDragging = false;
    let isDragging = false;
  let offsetX = 0, offsetY = 0;
    let dragStartX = 0;
    let dragStartY = 0;
    let elemStartLeft = 0;
    let elemStartTop = 0;
    let moved = false;


  function startDrag(x, y) {
    function onDragStart(x, y, target) {
    isDragging = true;
      if (excludeElements.includes(target)) return false;
    const rect = menu.getBoundingClientRect();
      isDragging = true;
    offsetX = x - rect.left;
      moved = false;
    offsetY = y - rect.top;
      dragStartX = x;
    menu.style.left = rect.left + "px";
      dragStartY = y;
    menu.style.top = rect.top + "px";
      const rect = element.getBoundingClientRect();
    menu.style.right = "auto";
      elemStartLeft = rect.left;
  }
      elemStartTop = rect.top;
      return true;
    }


  function doDrag(x, y) {
    function onDragMove(x, y) {
    if (!isDragging) return;
      if (!isDragging) return;
    menu.style.left = x - offsetX + "px";
      const dx = x - dragStartX;
    menu.style.top = y - offsetY + "px";
      const dy = y - dragStartY;
  }
      if (Math.abs(dx) > 3 || Math.abs(dy) > 3) moved = true;


  function stopDrag() {
      let newLeft = elemStartLeft + dx;
    isDragging = false;
      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";
    }


  header.addEventListener("mousedown", (e) => {
    function onDragEnd() {
    startDrag(e.clientX, e.clientY);
      if (!isDragging) return;
    e.preventDefault();
      isDragging = false;
  });
      if (!moved && typeof onClick === "function") {
        onClick();
      }
    }


  document.addEventListener("mousemove", (e) => {
    element.addEventListener("mousedown", (e) => {
     doDrag(e.clientX, e.clientY);
      if (!onDragStart(e.clientX, e.clientY, e.target)) return;
  });
      e.preventDefault();
     });
    document.addEventListener("mousemove", (e) => onDragMove(e.clientX, e.clientY));
    document.addEventListener("mouseup", onDragEnd);


  document.addEventListener("mouseup", stopDrag);
    element.addEventListener("touchstart", (e) => {
      if (!e.touches || !e.touches[0]) return;
      if (!onDragStart(e.touches[0].clientX, e.touches[0].clientY, e.target)) return;
      e.preventDefault();
    });
    document.addEventListener(
      "touchmove",
      (e) => {
        if (!e.touches || !e.touches[0]) return;
        onDragMove(e.touches[0].clientX, e.touches[0].clientY);
      },
      { passive: false }
    );
    document.addEventListener("touchend", onDragEnd);
    document.addEventListener("touchcancel", onDragEnd);
  }


   header.addEventListener("touchstart", (e) => {
   function toggleMenu() {
     const touch = e.touches[0];
     menu.style.display = menu.style.display === "none" ? "block" : "none";
    startDrag(touch.clientX, touch.clientY);
   }
    e.preventDefault();
   });


   document.addEventListener("touchmove", (e) => {
   makeDraggable(toggle, [], toggleMenu);
    if (!isDragging) return;
   makeDraggable(menu, [textarea, runBtn]);
    const touch = e.touches[0];
    doDrag(touch.clientX, touch.clientY);
   });


   document.addEventListener("touchend", stopDrag);
  menu.appendChild(title);
  menu.appendChild(textarea);
  menu.appendChild(runBtn);
   document.body.appendChild(toggle);
  document.body.appendChild(menu);
})();
})();