Write a function that should check if a checkbox is checked and do an action, example:…
- User
Write a function that should check if a checkbox is checked and do an action, example:
checkboxAction('unlimitedEmps', () => { // Do something }); // If the unlimitedEmps is off, the function should stop
Code:
// ==UserScript== // @name Custom Draggable Debug Menu - Dark Mode with Transitions Fixed // @namespace http://tampermonkey.net/ // @version 1.1 // @description Adds a custom draggable debug menu with styled checkboxes, now in dark mode with minimize/restore transitions. // @author YSSF // @match https://zty.pe/ // @grant none // ==/UserScript==
(() => { 'use strict';
// Create the menu container const menu = document.createElement('div'); menu.id = 'custom-debug-menu'; menu.innerHTML = ` <div id="debug-menu-header"> <span>Debug Menu</span> <button id="debug-menu-toggle">−</button> </div> <div id="debug-menu-content"> <label class="switch"> <input type="checkbox" id="unlimitedEmps"> <span class="slider"></span> <span class="label-text">Unlimited EMPs</span> </label> <label class="switch"> <input type="checkbox" id="invincible"> <span class="slider"></span> <span class="label-text">Invincible</span> </label> </div> `; document.body.appendChild(menu); // Add styles const styles = document.createElement('style'); styles.innerHTML = ` #custom-debug-menu { position: fixed; top: 100px; left: 100px; width: 200px; background-color: #333; color: #fff; border: 1px solid #555; z-index: 9999; box-shadow: 0 2px 10px rgba(0,0,0,0.2); border-radius: 8px; overflow: hidden; user-select: none; font-family: Arial, sans-serif; } #debug-menu-header { background-color: #444; color: #fff; padding: 10px; cursor: move; display: flex; justify-content: space-between; align-items: center; } #debug-menu-header span { font-weight: bold; } #debug-menu-header button { background: none; color: #fff; border: none; font-size: 18px; cursor: pointer; } #debug-menu-content { padding: 10px; overflow: hidden; transition: max-height 0.3s ease, opacity 0.3s ease, padding 0.3s ease; max-height: 1000px; opacity: 1; } .switch { display: flex; align-items: center; margin-bottom: 10px; } .switch input { display: none; } .slider { position: relative; width: 40px; height: 20px; background-color: #666; border-radius: 20px; margin-right: 10px; transition: background-color 0.3s; cursor: pointer; } .slider::before { content: ""; position: absolute; width: 16px; height: 16px; left: 2px; top: 2px; background-color: #fff; border-radius: 50%; transition: transform 0.3s; } input:checked + .slider { background-color: #2196F3; } input:checked + .slider::before { transform: translateX(20px); } .label-text { flex-grow: 1; color: #fff; } `; document.head.appendChild(styles); // Make the menu draggable dragElement(menu); function dragElement(el) { const header = document.getElementById('debug-menu-header'); let posX = 0, posY = 0, mouseX = 0, mouseY = 0; if (header) { header.onmousedown = dragMouseDown; } else { el.onmousedown = dragMouseDown; } function dragMouseDown(e) { e = e || window.event; e.preventDefault(); mouseX = e.clientX; mouseY = e.clientY; document.onmouseup = closeDragElement; document.onmousemove = elementDrag; } function elementDrag(e) { e = e || window.event; e.preventDefault(); posX = mouseX - e.clientX; posY = mouseY - e.clientY; mouseX = e.clientX; mouseY = e.clientY; el.style.top = (el.offsetTop - posY) + 'px'; el.style.left = (el.offsetLeft - posX) + 'px'; } function closeDragElement() { document.onmouseup = null; document.onmousemove = null; } } // Set initial maxHeight of content const content = document.getElementById('debug-menu-content'); content.style.maxHeight = content.scrollHeight + 'px'; // Minimize/Restore functionality with transitions const toggleButton = document.getElementById('debug-menu-toggle'); let isMinimized = false; toggleButton.addEventListener('click', () => { if (isMinimized) { // Restore content.style.maxHeight = content.scrollHeight + 'px'; content.style.opacity = '1'; content.style.padding = '10px'; // Restore padding toggleButton.innerHTML = '−'; isMinimized = false; } else { // Minimize content.style.maxHeight = '0px'; content.style.opacity = '0'; content.style.padding = '0px'; // Remove padding toggleButton.innerHTML = '+'; isMinimized = true; } });})();
- Assistant
To achieve the functionality you're describing, you can create a
checkboxActionfunction that checks the state of a checkbox by its ID and executes a given action if it's checked. Here's how you can implement it:function checkboxAction(checkboxId, action) { const checkbox = document.getElementById(checkboxId); if (!checkbox) { console.warn(`Checkbox with id "${checkboxId}" not found.`); return; } if (checkbox.checked) { action(); } }Usage Example:
checkboxAction('unlimitedEmps', () => { // Your action code here console.log('The unlimitedEmps checkbox is checked!'); }); // If the 'unlimitedEmps' checkbox is unchecked, the action will not be executed.Explanation:
- The function
checkboxActiontakes two parameters:checkboxId: The ID of the checkbox you want to check.action: A callback function containing the code to execute if the checkbox is checked.
- Inside the function:
- It first tries to get the checkbox element by its ID using
document.getElementById. - If the checkbox doesn't exist, it logs a warning and exits the function.
- If the checkbox exists, it checks if it's checked using
checkbox.checked.- If it is checked, it executes the
actioncallback. - If it's not checked, the function stops (does nothing).
- If it is checked, it executes the
- It first tries to get the checkbox element by its ID using
Integration with Your Code:
You can place this function anywhere within your userscript after the checkboxes have been added to the DOM. Ensure that you call
checkboxActionwhenever you need to perform the check and action.Example within Your Script:
(() => { 'use strict'; // ... [Your existing code] // Define the checkboxAction function function checkboxAction(checkboxId, action) { const checkbox = document.getElementById(checkboxId); if (!checkbox) { console.warn(`Checkbox with id "${checkboxId}" not found.`); return; } if (checkbox.checked) { action(); } } // Usage example checkboxAction('unlimitedEmps', () => { // Do something when 'unlimitedEmps' is checked console.log('Unlimited EMPs activated!'); // Your code to enable unlimited EMPs... }); // You might want to add event listeners if you want to react to changes const unlimitedEmpsCheckbox = document.getElementById('unlimitedEmps'); unlimitedEmpsCheckbox.addEventListener('change', () => { checkboxAction('unlimitedEmps', () => { console.log('Unlimited EMPs activated!'); // Your code to enable unlimited EMPs... }); }); // Similarly for other checkboxes... })();Note:
- If you want the action to be executed every time the checkbox is checked or unchecked, consider adding an event listener to the checkbox's
changeevent. This way, you can respond immediately to user interactions. - If the action involves starting and stopping a process (like an interval or timeout), you may need to keep references to those processes so you can properly manage them when the checkbox state changes.
Advanced Usage with Start/Stop Actions:
If your action involves starting something when the checkbox is checked and stopping it when unchecked, you can enhance the function as follows:
function checkboxAction(checkboxId, actionStart, actionStop) { const checkbox = document.getElementById(checkboxId); if (!checkbox) { console.warn(`Checkbox with id "${checkboxId}" not found.`); return; } function checkCheckbox() { if (checkbox.checked) { actionStart(); } else { actionStop(); } } // Initial check checkCheckbox(); // Add event listener for changes checkbox.addEventListener('change', checkCheckbox); }Usage Example:
checkboxAction( 'unlimitedEmps', () => { // Start action when checked console.log('Unlimited EMPs activated!'); // Your code to enable unlimited EMPs... }, () => { // Stop action when unchecked console.log('Unlimited EMPs deactivated!'); // Your code to disable unlimited EMPs... } );This setup allows you to define both what should happen when the checkbox is checked and what should happen when it's unchecked, providing better control over the actions tied to the checkbox's state.
- The function