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
 
(61 intermediate revisions by the same user not shown)
Line 1: Line 1:
(function() {
(function () {
    // Create the menu container
  function constrainPosition(x, y, width, height) {
    const menu = document.createElement("div");
     const winWidth = window.innerWidth;
     menu.id = "mahitoMenu";
     const winHeight = window.innerHeight;
    menu.style.position = "fixed";
     if (x < 0) x = 0;
     menu.style.top = "20px";
     if (y < 0) y = 0;
    menu.style.right = "20px";
     if (x + width > winWidth) x = winWidth - width;
     menu.style.padding = "10px";
     if (y + height > winHeight) y = winHeight - height;
     menu.style.backgroundColor = "#111";
     return [x, y];
     menu.style.color = "#fff";
  }
     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";


    // Create header for drag and minimize
  const toggle = document.createElement("button");
    const header = document.createElement("div");
  toggle.id = "delta-toggle";
    header.style.cursor = "move";
  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",
  });


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


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


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


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


     // Drag functionality
  runBtn.onclick = () => {
     let isDragging = false, offsetX = 0, offsetY = 0;
     try {
      eval(textarea.value);
     } catch (e) {
      alert("Error: " + e.message);
    }
  };


     header.addEventListener("mousedown", function(e) {
  function makeDraggable(element, excludeElements = [], onClick) {
        isDragging = true;
    let isDragging = false;
         offsetX = e.clientX - menu.offsetLeft;
    let dragStartX = 0;
         offsetY = e.clientY - menu.offsetTop;
    let dragStartY = 0;
        e.preventDefault();
    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;
 
      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 onDragEnd() {
      if (!isDragging) return;
      isDragging = false;
      if (!moved && typeof onClick === "function") {
         onClick();
      }
    }
 
    element.addEventListener("mousedown", (e) => {
      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("mousemove", function(e) {
     element.addEventListener("touchstart", (e) => {
        if (isDragging) {
      if (!e.touches || !e.touches[0]) return;
            menu.style.left = (e.clientX - offsetX) + "px";
      if (!onDragStart(e.touches[0].clientX, e.touches[0].clientY, e.target)) return;
            menu.style.top = (e.clientY - offsetY) + "px";
      e.preventDefault();
            menu.style.right = "auto"; // stop auto-sticking to right side
        }
     });
     });
    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);
  }
  function toggleMenu() {
    menu.style.display = menu.style.display === "none" ? "block" : "none";
  }
  makeDraggable(toggle, [], toggleMenu);
  makeDraggable(menu, [textarea, runBtn]);


    document.addEventListener("mouseup", function() {
  menu.appendChild(title);
        isDragging = false;
  menu.appendChild(textarea);
    });
  menu.appendChild(runBtn);
  document.body.appendChild(toggle);
  document.body.appendChild(menu);
})();
})();