User:Stumblean/common.js
Appearance
Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
(function () {
// Utility function for constraining position within viewport
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];
}
// Create toggle button with image icon
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", // show move cursor to indicate draggable
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", // so dragging the button works smoothly
});
toggle.appendChild(icon);
// Create Delta menu
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);
}
};
// Toggle menu visibility when clicking toggle button
toggle.onclick = () => {
menu.style.display = menu.style.display === "none" ? "block" : "none";
};
// Generic drag handlers for mouse and touch
function makeDraggable(element, excludeElements = []) {
let isDragging = false;
let dragStartX = 0;
let dragStartY = 0;
let elemStartLeft = 0;
let elemStartTop = 0;
function onDragStart(x, y, target) {
// Ignore drag if started on excluded elements inside container
if (excludeElements.includes(target)) return false;
isDragging = true;
dragStartX = x;
dragStartY = y;
const rect = element.getBoundingClientRect();
elemStartLeft = rect.left;
elemStartTop = rect.top;
return true;
}
function onDragMove(x, y) {
if (!isDragging) return;
let newLeft = elemStartLeft + (x - dragStartX);
let newTop = elemStartTop + (y - dragStartY);
[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() {
isDragging = false;
}
// Mouse events
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);
// Touch events
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);
}
// Apply draggable to both toggle button and menu
// For menu, exclude textarea and runBtn from drag start
makeDraggable(toggle);
makeDraggable(menu, [textarea, runBtn]);
// Assemble menu content
menu.appendChild(title);
menu.appendChild(textarea);
menu.appendChild(runBtn);
document.body.appendChild(toggle);
document.body.appendChild(menu);
})();