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
 
(59 intermediate revisions by the same user not shown)
Line 1: Line 1:
(function () {
(function () {
     const minimizedImgURL = "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 menu = document.createElement("div");
  const toggle = document.createElement("button");
    menu.id = "mahitoMenu";
  toggle.id = "delta-toggle";
    menu.style.position = "fixed";
  Object.assign(toggle.style, {
     menu.style.top = "20px";
    position: "fixed",
     menu.style.right = "20px";
     bottom: "20px",
     menu.style.padding = "10px";
     right: "20px",
     menu.style.backgroundColor = "#111";
     padding: "0",
     menu.style.color = "#fff";
     background: "#2b2b2b",
     menu.style.border = "2px solid #666";
     border: "none",
     menu.style.zIndex = "9999";
     borderRadius: "12px",
     menu.style.fontFamily = "monospace";
     zIndex: 9998,
    menu.style.borderRadius = "10px";
     cursor: "move",
    menu.style.boxShadow = "0 0 10px rgba(0,0,0,0.5)";
     userSelect: "none",
     menu.style.userSelect = "none";
     touchAction: "none",
     menu.style.touchAction = "none";
  });
    menu.style.width = "auto";


    // Header
  const icon = document.createElement("img");
    const header = document.createElement("div");
  icon.src = "https://files.catbox.moe/uodscw.png";
    header.style.cursor = "grab";
  icon.alt = "Delta";
    header.style.display = "flex";
  Object.assign(icon.style, {
    header.style.justifyContent = "space-between";
    width: "48px",
     header.style.alignItems = "center";
    height: "48px",
     header.style.marginBottom = "8px";
    borderRadius: "12px",
     display: "block",
     pointerEvents: "none",
  });
  toggle.appendChild(icon);


    const title = document.createElement("div");
  const menu = document.createElement("div");
    title.textContent = "💀 Mahito Menu 💀";
  menu.id = "delta-menu";
    title.style.fontWeight = "bold";
  Object.assign(menu.style, {
     header.appendChild(title);
    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",
  });


    const minimizeBtn = document.createElement("button");
  const title = document.createElement("div");
    minimizeBtn.textContent = "";
  title.textContent = "Delta Online";
    minimizeBtn.style.background = "#333";
  Object.assign(title.style, {
     minimizeBtn.style.color = "#fff";
    textAlign: "center",
     minimizeBtn.style.border = "none";
     fontSize: "18px",
     minimizeBtn.style.cursor = "pointer";
     marginBottom: "10px",
     minimizeBtn.style.marginLeft = "10px";
     userSelect: "text",
    header.appendChild(minimizeBtn);
     cursor: "default",
    menu.appendChild(header);
  });


    // Content
  const textarea = document.createElement("textarea");
    const content = document.createElement("div");
  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",
  });


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


    // Minimized image element (hidden at first)
  runBtn.onclick = () => {
    const minimizedIcon = document.createElement("img");
     try {
    minimizedIcon.src = minimizedImgURL;
      eval(textarea.value);
     minimizedIcon.style.position = "fixed";
     } catch (e) {
    minimizedIcon.style.top = "20px";
      alert("Error: " + e.message);
     minimizedIcon.style.right = "20px";
     }
    minimizedIcon.style.width = "30px";
  };
    minimizedIcon.style.height = "30px";
     minimizedIcon.style.borderRadius = "5px";
    minimizedIcon.style.cursor = "pointer";
    minimizedIcon.style.display = "none";
    minimizedIcon.style.zIndex = "9999";
    document.body.appendChild(minimizedIcon);


     // Minimize logic
  function makeDraggable(element, excludeElements = [], onClick) {
     minimizeBtn.onclick = function () {
    let isDragging = false;
        menu.style.display = "none";
     let dragStartX = 0;
        minimizedIcon.style.display = "block";
     let dragStartY = 0;
     };
    let elemStartLeft = 0;
    let elemStartTop = 0;
     let moved = false;


     // Restore logic
     function onDragStart(x, y, target) {
    minimizedIcon.onclick = function () {
      if (excludeElements.includes(target)) return false;
        menu.style.display = "block";
      isDragging = true;
        minimizedIcon.style.display = "none";
      moved = false;
     };
      dragStartX = x;
      dragStartY = y;
      const rect = element.getBoundingClientRect();
      elemStartLeft = rect.left;
      elemStartTop = rect.top;
      return true;
     }


     // Dragging (mouse + touch)
     function onDragMove(x, y) {
    let isDragging = false,
      if (!isDragging) return;
        offsetX = 0,
      const dx = x - dragStartX;
        offsetY = 0;
      const dy = y - dragStartY;
      if (Math.abs(dx) > 3 || Math.abs(dy) > 3) moved = true;


    function startDrag(x, y) {
      let newLeft = elemStartLeft + dx;
         isDragging = true;
      let newTop = elemStartTop + dy;
         const rect = menu.getBoundingClientRect();
      [newLeft, newTop] = constrainPosition(
        offsetX = x - rect.left;
        newLeft,
        offsetY = y - rect.top;
        newTop,
        menu.style.right = "auto";
         element.offsetWidth,
         element.offsetHeight
      );
      element.style.left = newLeft + "px";
      element.style.top = newTop + "px";
      element.style.bottom = "auto";
      element.style.right = "auto";
     }
     }


     function doDrag(x, y) {
     function onDragEnd() {
        if (isDragging) {
      if (!isDragging) return;
            menu.style.left = (x - offsetX) + "px";
      isDragging = false;
            menu.style.top = (y - offsetY) + "px";
      if (!moved && typeof onClick === "function") {
        }
        onClick();
      }
     }
     }


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


     // Mouse events
     element.addEventListener("touchstart", (e) => {
    header.addEventListener("mousedown", e => {
      if (!e.touches || !e.touches[0]) return;
        startDrag(e.clientX, e.clientY);
      if (!onDragStart(e.touches[0].clientX, e.touches[0].clientY, e.target)) return;
        e.preventDefault();
      e.preventDefault();
     });
     });
     document.addEventListener("mousemove", e => doDrag(e.clientX, e.clientY));
     document.addEventListener(
     document.addEventListener("mouseup", stopDrag);
      "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);
  }
 
  function toggleMenu() {
    menu.style.display = menu.style.display === "none" ? "block" : "none";
  }
 
  makeDraggable(toggle, [], toggleMenu);
  makeDraggable(menu, [textarea, runBtn]);


    // Touch events
  menu.appendChild(title);
    header.addEventListener("touchstart", e => {
  menu.appendChild(textarea);
        const touch = e.touches[0];
  menu.appendChild(runBtn);
        startDrag(touch.clientX, touch.clientY);
  document.body.appendChild(toggle);
        e.preventDefault();
  document.body.appendChild(menu);
    });
    document.addEventListener("touchmove", e => {
        if (!isDragging) return;
        const touch = e.touches[0];
        doDrag(touch.clientX, touch.clientY);
    });
    document.addEventListener("touchend", stopDrag);
})();
})();