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
 
(55 intermediate revisions by the same user not shown)
Line 1: Line 1:
(function () {
(function () {
   const iconURL = "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",
     padding: "10px",
     width: "300px",
     backgroundColor: "#111",
     background: "#2b2b2b",
     color: "#fff",
     color: "#fff",
    border: "2px solid #666",
    zIndex: "9999",
    fontFamily: "monospace",
     borderRadius: "10px",
     borderRadius: "10px",
    padding: "15px",
     boxShadow: "0 0 10px rgba(0,0,0,0.5)",
     boxShadow: "0 0 10px rgba(0,0,0,0.5)",
    zIndex: 9999,
    fontFamily: "monospace",
    cursor: "move",
     userSelect: "none",
     userSelect: "none",
     touchAction: "none",
     touchAction: "none",
   });
   });


   const header = document.createElement("div");
   const title = document.createElement("div");
   Object.assign(header.style, {
  title.textContent = "Delta Online";
     cursor: "grab",
   Object.assign(title.style, {
     display: "flex",
     textAlign: "center",
     justifyContent: "center",
     fontSize: "18px",
     alignItems: "center",
     marginBottom: "10px",
     marginBottom: "8px",
     userSelect: "text",
    gap: "6px",
     cursor: "default",
   });
   });


  // Left icon
   const textarea = document.createElement("textarea");
   const leftIcon = document.createElement("img");
   textarea.placeholder = "Enter JavaScript...";
   leftIcon.src = iconURL;
   Object.assign(textarea.style, {
   leftIcon.width = 15;
    width: "100%",
  leftIcon.height = 15;
    height: "120px",
  leftIcon.alt = "Mahito icon";
    background: "#1e1e1e",
  leftIcon.style.borderRadius = "3px";
    color: "#fff",
    border: "none",
    borderRadius: "6px",
    padding: "10px",
    resize: "none",
    boxSizing: "border-box",
    cursor: "auto",
    userSelect: "text",
  });


  // Title text
   const runBtn = document.createElement("button");
   const title = document.createElement("div");
   runBtn.textContent = "Run Code";
   title.textContent = "Mahito Menu";
   Object.assign(runBtn.style, {
   title.style.fontWeight = "bold";
    marginTop: "10px",
 
    width: "100%",
  // Right icon
    padding: "10px",
  const rightIcon = document.createElement("img");
     background: "#4caf50",
  rightIcon.src = iconURL;
  rightIcon.width = 15;
  rightIcon.height = 15;
  rightIcon.alt = "Mahito icon";
  rightIcon.style.borderRadius = "3px";
 
  header.appendChild(leftIcon);
  header.appendChild(title);
  header.appendChild(rightIcon);
 
  menu.appendChild(header);
 
  const content = document.createElement("div");
  const openBtn = document.createElement("button");
  openBtn.textContent = "Open Explanation";
  Object.assign(openBtn.style, {
     background: "#444",
     color: "#fff",
     color: "#fff",
     border: "none",
     border: "none",
     padding: "5px 10px",
     borderRadius: "6px",
     cursor: "pointer",
     cursor: "pointer",
   });
   });
  openBtn.onclick = () => console.log("Open Explanation clicked");


   content.appendChild(openBtn);
   runBtn.onclick = () => {
  menu.appendChild(content);
    try {
      eval(textarea.value);
    } catch (e) {
      alert("Error: " + e.message);
    }
  };


   document.body.appendChild(menu);
   function makeDraggable(element, excludeElements = [], onClick) {
    let isDragging = false;
    let dragStartX = 0;
    let dragStartY = 0;
    let elemStartLeft = 0;
    let elemStartTop = 0;
    let moved = false;


  // Dragging logic
    function onDragStart(x, y, target) {
  let isDragging = false,
      if (excludeElements.includes(target)) return false;
    offsetX = 0,
      isDragging = true;
    offsetY = 0;
      moved = false;
      dragStartX = x;
      dragStartY = y;
      const rect = element.getBoundingClientRect();
      elemStartLeft = rect.left;
      elemStartTop = rect.top;
      return true;
    }


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


  function doDrag(x, y) {
      let newLeft = elemStartLeft + dx;
    if (isDragging) {
      let newTop = elemStartTop + dy;
       menu.style.left = x - offsetX + "px";
      [newLeft, newTop] = constrainPosition(
       menu.style.top = y - offsetY + "px";
        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", function (e) {
      if (!onDragStart(e.clientX, e.clientY, e.target)) return;
    startDrag(e.clientX, e.clientY);
      e.preventDefault();
    e.preventDefault();
    });
  });
    document.addEventListener("mousemove", (e) => onDragMove(e.clientX, e.clientY));
    document.addEventListener("mouseup", onDragEnd);


  document.addEventListener("mousemove", function (e) {
    element.addEventListener("touchstart", (e) => {
    doDrag(e.clientX, e.clientY);
      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);
  }


   document.addEventListener("mouseup", stopDrag);
   function toggleMenu() {
    menu.style.display = menu.style.display === "none" ? "block" : "none";
  }


   // Touch events
   makeDraggable(toggle, [], toggleMenu);
  header.addEventListener("touchstart", function (e) {
  makeDraggable(menu, [textarea, runBtn]);
    const touch = e.touches[0];
    startDrag(touch.clientX, touch.clientY);
    e.preventDefault();
  });


   document.addEventListener("touchmove", function (e) {
   menu.appendChild(title);
    if (!isDragging) return;
  menu.appendChild(textarea);
    const touch = e.touches[0];
  menu.appendChild(runBtn);
    doDrag(touch.clientX, touch.clientY);
   document.body.appendChild(toggle);
   });
   document.body.appendChild(menu);
 
   document.addEventListener("touchend", stopDrag);
})();
})();