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
 
(60 intermediate revisions by the same user not shown)
Line 1: Line 1:
(function() {
(function () {
    const menu = document.createElement("div");
  function constrainPosition(x, y, width, height) {
     menu.id = "mahitoMenu";
     const winWidth = window.innerWidth;
    menu.style.position = "fixed";
     const winHeight = window.innerHeight;
     menu.style.top = "20px";
     if (x < 0) x = 0;
    menu.style.right = "20px";
     if (y < 0) y = 0;
     menu.style.padding = "10px";
     if (x + width > winWidth) x = winWidth - width;
     menu.style.backgroundColor = "#111";
     if (y + height > winHeight) y = winHeight - height;
     menu.style.color = "#fff";
     return [x, y];
     menu.style.border = "2px solid #666";
  }
    menu.style.zIndex = "9999";
    menu.style.fontFamily = "monospace";
     menu.style.borderRadius = "10px";
    menu.style.boxShadow = "0 0 10px rgba(0,0,0,0.5)";
    menu.style.userSelect = "none";
    menu.style.touchAction = "none"; // prevents weird scrolling glitches


    // Header for dragging and minimizing
  const toggle = document.createElement("button");
    const header = document.createElement("div");
  toggle.id = "delta-toggle";
    header.style.cursor = "grab";
  Object.assign(toggle.style, {
    header.style.display = "flex";
    position: "fixed",
     header.style.justifyContent = "space-between";
    bottom: "20px",
     header.style.alignItems = "center";
    right: "20px",
     header.style.marginBottom = "8px";
    padding: "0",
    background: "#2b2b2b",
    border: "none",
    borderRadius: "12px",
    zIndex: 9998,
     cursor: "move",
     userSelect: "none",
     touchAction: "none",
  });


    const title = document.createElement("div");
  const icon = document.createElement("img");
    title.textContent = "💀 Mahito Menu 💀";
  icon.src = "https://files.catbox.moe/uodscw.png";
    title.style.fontWeight = "bold";
  icon.alt = "Delta";
     header.appendChild(title);
  Object.assign(icon.style, {
     width: "48px",
    height: "48px",
    borderRadius: "12px",
    display: "block",
    pointerEvents: "none",
  });
  toggle.appendChild(icon);


    const minimizeBtn = document.createElement("button");
  const menu = document.createElement("div");
    minimizeBtn.textContent = "";
  menu.id = "delta-menu";
    minimizeBtn.style.background = "#333";
  Object.assign(menu.style, {
     minimizeBtn.style.color = "#fff";
    display: "none",
     minimizeBtn.style.border = "none";
    position: "fixed",
     minimizeBtn.style.cursor = "pointer";
    bottom: "80px",
     minimizeBtn.style.marginLeft = "10px";
    right: "20px",
     minimizeBtn.onclick = function() {
    width: "300px",
        content.style.display = content.style.display === "none" ? "block" : "none";
    background: "#2b2b2b",
     };
     color: "#fff",
    header.appendChild(minimizeBtn);
     borderRadius: "10px",
    menu.appendChild(header);
     padding: "15px",
     boxShadow: "0 0 10px rgba(0,0,0,0.5)",
     zIndex: 9999,
    fontFamily: "monospace",
    cursor: "move",
    userSelect: "none",
     touchAction: "none",
  });


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


    const openBtn = document.createElement("button");
  const textarea = document.createElement("textarea");
    openBtn.textContent = "Open Explanation";
  textarea.placeholder = "Enter JavaScript...";
    openBtn.style.background = "#444";
  Object.assign(textarea.style, {
     openBtn.style.color = "#fff";
    width: "100%",
     openBtn.style.border = "none";
    height: "120px",
     openBtn.style.padding = "5px 10px";
    background: "#1e1e1e",
     openBtn.style.cursor = "pointer";
     color: "#fff",
     openBtn.onclick = function() {
     border: "none",
        console.log("Open Explanation clicked");
    borderRadius: "6px",
     };
     padding: "10px",
    content.appendChild(openBtn);
     resize: "none",
     boxSizing: "border-box",
    cursor: "auto",
     userSelect: "text",
  });


    menu.appendChild(content);
  const runBtn = document.createElement("button");
    document.body.appendChild(menu);
  runBtn.textContent = "Run Code";
  Object.assign(runBtn.style, {
    marginTop: "10px",
    width: "100%",
    padding: "10px",
    background: "#4caf50",
    color: "#fff",
    border: "none",
    borderRadius: "6px",
    cursor: "pointer",
  });


     // Dragging support for mouse + touch
  runBtn.onclick = () => {
     let isDragging = false, offsetX = 0, offsetY = 0;
    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 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.right = "auto";
      dragStartY = y;
      const rect = element.getBoundingClientRect();
      elemStartLeft = rect.left;
      elemStartTop = rect.top;
      return true;
     }
     }


     function doDrag(x, y) {
     function onDragMove(x, y) {
        if (isDragging) {
      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;
 
      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 stopDrag() {
     function onDragEnd() {
        isDragging = false;
      if (!isDragging) return;
      isDragging = false;
      if (!moved && typeof onClick === "function") {
        onClick();
      }
     }
     }


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


     // Touch events (for mobile)
     element.addEventListener("touchstart", (e) => {
    header.addEventListener("touchstart", e => {
      if (!e.touches || !e.touches[0]) return;
        const touch = e.touches[0];
      if (!onDragStart(e.touches[0].clientX, e.touches[0].clientY, e.target)) return;
        startDrag(touch.clientX, touch.clientY);
      e.preventDefault();
        e.preventDefault();
     });
     });
     document.addEventListener("touchmove", e => {
     document.addEventListener(
         if (!isDragging) return;
      "touchmove",
         const touch = e.touches[0];
      (e) => {
        doDrag(touch.clientX, touch.clientY);
         if (!e.touches || !e.touches[0]) return;
     });
         onDragMove(e.touches[0].clientX, e.touches[0].clientY);
     document.addEventListener("touchend", stopDrag);
      },
      { passive: false }
     );
     document.addEventListener("touchend", onDragEnd);
    document.addEventListener("touchcancel", onDragEnd);
  }
 
  function toggleMenu() {
    menu.style.display = menu.style.display === "none" ? "block" : "none";
  }
 
  makeDraggable(toggle, [], toggleMenu);
  makeDraggable(menu, [textarea, runBtn]);
 
  menu.appendChild(title);
  menu.appendChild(textarea);
  menu.appendChild(runBtn);
  document.body.appendChild(toggle);
  document.body.appendChild(menu);
})();
})();