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
 
(56 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 menu = document.createElement("div");
  const toggle = document.createElement("button");
    menu.id = "mahitoMenu";
  toggle.id = "delta-toggle";
    Object.assign(menu.style, {
  Object.assign(toggle.style, {
        position: "fixed",
    position: "fixed",
        top: "20px",
    bottom: "20px",
        right: "20px",
    right: "20px",
        padding: "10px",
    padding: "0",
        backgroundColor: "#111",
    background: "#2b2b2b",
        color: "#fff",
    border: "none",
        border: "2px solid #666",
    borderRadius: "12px",
        zIndex: "9999",
    zIndex: 9998,
        fontFamily: "monospace",
    cursor: "move",
        borderRadius: "10px",
    userSelect: "none",
        boxShadow: "0 0 10px rgba(0,0,0,0.5)",
    touchAction: "none",
        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 header = document.createElement("div");
  const menu = document.createElement("div");
    Object.assign(header.style, {
  menu.id = "delta-menu";
        cursor: "grab",
  Object.assign(menu.style, {
        display: "flex",
    display: "none",
        alignItems: "center",
    position: "fixed",
        marginBottom: "8px",
    bottom: "80px",
        gap: "6px"
    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",
  });


    // Replace title with catbox image
  const title = document.createElement("div");
    const icon = document.createElement("img");
  title.textContent = "Delta Online";
    icon.src = iconURL;
  Object.assign(title.style, {
     icon.width = 10;
     textAlign: "center",
     icon.height = 10;
     fontSize: "18px",
     icon.alt = "Mahito";
     marginBottom: "10px",
     icon.style.borderRadius = "2px";
     userSelect: "text",
     header.appendChild(icon);
     cursor: "default",
  });


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


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


    // Dragging logic (mouse + touch)
  function makeDraggable(element, excludeElements = [], onClick) {
     let isDragging = false, offsetX = 0, offsetY = 0;
     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
     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);
})();
})();