Jump to content

User:Stumblean/common.js: Difference between revisions

From Domination Earth
Stumblean (talk | contribs)
No edit summary
Tags: Reverted Mobile edit Mobile web edit Advanced mobile edit
Stumblean (talk | contribs)
No edit summary
Tags: Mobile edit Mobile web edit Advanced mobile edit
 
(11 intermediate revisions by the same user not shown)
Line 1: Line 1:
(function () {
(function () {
   const defaultSettings = {
   function constrainPosition(x, y, width, height) {
     menuTitle: "Mahito Menu",
     const winWidth = window.innerWidth;
     iconURL: "https://files.catbox.moe/wk78nl.jpg",
     const winHeight = window.innerHeight;
     backgroundColor: "#7889B2",
     if (x < 0) x = 0;
     buttonColor: "#45A8C5",
     if (y < 0) y = 0;
     textColor: "#ffffff",
     if (x + width > winWidth) x = winWidth - width;
     borderColor: "#ffffff",
     if (y + height > winHeight) y = winHeight - height;
     iconSize: 15,
     return [x, y];
    minimizedSize: 30
   }
   };


   const defaultPrefs = {
   const toggle = document.createElement("button");
     showMassUndo: true,
  toggle.id = "delta-toggle";
     showExplanation: true,
  Object.assign(toggle.style, {
     showEncouragement: true,
     position: "fixed",
     showYujify: true,
     bottom: "20px",
     showCustomize: true,
     right: "20px",
     showUndoChanges: true,
     padding: "0",
     showPreferences: true, // always true so button never hides
     background: "#2b2b2b",
     showWatermark: true
     border: "none",
   };
     borderRadius: "12px",
    zIndex: 9998,
    cursor: "move",
    userSelect: "none",
     touchAction: "none",
   });


   let settings = JSON.parse(localStorage.getItem("mahitoMenuSettings")) || defaultSettings;
   const icon = document.createElement("img");
   let prefs = JSON.parse(localStorage.getItem("mahitoMenuPrefs")) || defaultPrefs;
  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);


   function saveSettings() {
   const menu = document.createElement("div");
    localStorage.setItem("mahitoMenuSettings", JSON.stringify(settings));
   menu.id = "delta-menu";
   }
   Object.assign(menu.style, {
   function savePrefs() {
     display: "none",
     localStorage.setItem("mahitoMenuPrefs", JSON.stringify(prefs));
    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",
   });


   // ... (rest of your menu creation code here, unchanged)
   const title = document.createElement("div");
  title.textContent = "Delta Online";
  Object.assign(title.style, {
    textAlign: "center",
    fontSize: "18px",
    marginBottom: "10px",
    userSelect: "text",
    cursor: "default",
  });


  // -- Snippet for Preferences Panel --
   const textarea = document.createElement("textarea");
   const prefsPanel = document.createElement("div");
  textarea.placeholder = "Enter JavaScript...";
   Object.assign(prefsPanel.style, {
   Object.assign(textarea.style, {
     backgroundColor: "#333a66",
     width: "100%",
     padding: "8px",
    height: "120px",
    background: "#1e1e1e",
    color: "#fff",
     border: "none",
     borderRadius: "6px",
     borderRadius: "6px",
    padding: "10px",
    resize: "none",
    boxSizing: "border-box",
    cursor: "auto",
    userSelect: "text",
  });
  const runBtn = document.createElement("button");
  runBtn.textContent = "Run Code";
  Object.assign(runBtn.style, {
     marginTop: "10px",
     marginTop: "10px",
     display: "none",
     width: "100%",
     color: "#eee",
    padding: "10px",
     fontSize: "13px"
    background: "#4caf50",
     color: "#fff",
    border: "none",
     borderRadius: "6px",
    cursor: "pointer",
   });
   });


   function createCheckbox(labelText, prefKey) {
   runBtn.onclick = () => {
     const label = document.createElement("label");
     try {
     label.style.display = "block";
      eval(textarea.value);
    label.style.marginBottom = "6px";
    } catch (e) {
     label.style.cursor = "pointer";
      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 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;


    const checkbox = document.createElement("input");
      let newLeft = elemStartLeft + dx;
    checkbox.type = "checkbox";
      let newTop = elemStartTop + dy;
    checkbox.checked = prefs[prefKey];
      [newLeft, newTop] = constrainPosition(
    checkbox.style.marginRight = "6px";
        newLeft,
        newTop,
        element.offsetWidth,
        element.offsetHeight
      );
      element.style.left = newLeft + "px";
      element.style.top = newTop + "px";
      element.style.bottom = "auto";
      element.style.right = "auto";
    }


     checkbox.addEventListener("change", () => {
     function onDragEnd() {
       prefs[prefKey] = checkbox.checked;
      if (!isDragging) return;
      savePrefs();
      isDragging = false;
       applyPreferences();
      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);


     label.appendChild(checkbox);
     element.addEventListener("touchstart", (e) => {
     label.appendChild(document.createTextNode(labelText));
      if (!e.touches || !e.touches[0]) return;
     return label;
      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);
   }
   }


   prefsPanel.appendChild(createCheckbox("Show Mass undo Button", "showMassUndo"));
   function toggleMenu() {
  prefsPanel.appendChild(createCheckbox("Show Explanation Button", "showExplanation"));
    menu.style.display = menu.style.display === "none" ? "block" : "none";
  prefsPanel.appendChild(createCheckbox("Show Encouragement Button", "showEncouragement"));
   }
  prefsPanel.appendChild(createCheckbox("Show YUJIFY Button", "showYujify"));
  prefsPanel.appendChild(createCheckbox("Show Customize Menu Button", "showCustomize"));
  prefsPanel.appendChild(createCheckbox("Show Undo Changes Button", "showUndoChanges"));
  prefsPanel.appendChild(createCheckbox("Show Preferences Button (cannot be hidden)", "showPreferences"));
  prefsPanel.appendChild(createCheckbox("Show Watermark Footer", "showWatermark"));
 
  // Add Reset Preferences Button
  const resetPrefsBtn = document.createElement("button");
  resetPrefsBtn.textContent = "Reset Preferences";
  Object.assign(resetPrefsBtn.style, {
    background: "#cc4444",
    color: "#fff",
    border: "none",
    borderRadius: "5px",
    padding: "6px 10px",
    marginTop: "10px",
    cursor: "pointer",
    width: "100%"
  });
  resetPrefsBtn.addEventListener("click", () => {
    if (confirm("Are you sure you want to reset all preferences to default?")) {
      prefs = {...defaultPrefs}; // reset to default
      savePrefs();
      applyPreferences();
 
      // Update all checkboxes to reflect defaults
      [...prefsPanel.querySelectorAll("input[type=checkbox]")].forEach(input => {
        input.checked = prefs[input.parentNode.textContent.trim().toLowerCase().includes("mass undo") ? "showMassUndo" :
          input.parentNode.textContent.trim().toLowerCase().includes("explanation") ? "showExplanation" :
          input.parentNode.textContent.trim().toLowerCase().includes("encouragement") ? "showEncouragement" :
          input.parentNode.textContent.trim().toLowerCase().includes("yujify") ? "showYujify" :
          input.parentNode.textContent.trim().toLowerCase().includes("customize") ? "showCustomize" :
          input.parentNode.textContent.trim().toLowerCase().includes("undo changes") ? "showUndoChanges" :
          input.parentNode.textContent.trim().toLowerCase().includes("preferences") ? "showPreferences" :
          input.parentNode.textContent.trim().toLowerCase().includes("watermark") ? "showWatermark" : ""
        ];
      });
    }
   });
  prefsPanel.appendChild(resetPrefsBtn);


   // ... (append prefsPanel and other elements to menu as in your full code)
   makeDraggable(toggle, [], toggleMenu);
  makeDraggable(menu, [textarea, runBtn]);


   // (rest of your applyPreferences function, menu setup, button creation, etc. stays the same)
   menu.appendChild(title);
  menu.appendChild(textarea);
  menu.appendChild(runBtn);
  document.body.appendChild(toggle);
  document.body.appendChild(menu);
})();
})();

Latest revision as of 00:19, 29 June 2025

(function () {
  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");
  menu.id = "delta-menu";
  Object.assign(menu.style, {
    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 title = document.createElement("div");
  title.textContent = "Delta Online";
  Object.assign(title.style, {
    textAlign: "center",
    fontSize: "18px",
    marginBottom: "10px",
    userSelect: "text",
    cursor: "default",
  });

  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 runBtn = document.createElement("button");
  runBtn.textContent = "Run Code";
  Object.assign(runBtn.style, {
    marginTop: "10px",
    width: "100%",
    padding: "10px",
    background: "#4caf50",
    color: "#fff",
    border: "none",
    borderRadius: "6px",
    cursor: "pointer",
  });

  runBtn.onclick = () => {
    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 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);

    element.addEventListener("touchstart", (e) => {
      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);
  }

  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);
})();
Cookies help us deliver our services. By using our services, you agree to our use of cookies.