|
| 1 | +// ==UserScript== |
| 2 | +// @name Countdown Timer |
| 3 | +// @version 1.0.0 |
| 4 | +// @author K1ngw1ng |
| 5 | +// @description Countdown Timer |
| 6 | +// @match *://*/* |
| 7 | +// @grant none |
| 8 | +// ==/UserScript== |
| 9 | + |
| 10 | +(function() { |
| 11 | + 'use strict'; |
| 12 | + |
| 13 | + let timerInterval = null; |
| 14 | + let remainingTime = 60 * 60; // 60 minutes |
| 15 | + let isRunning = false; |
| 16 | + |
| 17 | + // Start the countdown timer |
| 18 | + function startTimer(display) { |
| 19 | + if (isRunning) return; // Prevent multiple intervals |
| 20 | + isRunning = true; |
| 21 | + timerInterval = setInterval(function () { |
| 22 | + let minutes = parseInt(remainingTime / 60, 10); |
| 23 | + let seconds = parseInt(remainingTime % 60, 10); |
| 24 | + |
| 25 | + minutes = minutes < 10 ? "0" + minutes : minutes; |
| 26 | + seconds = seconds < 10 ? "0" + seconds : seconds; |
| 27 | + |
| 28 | + display.textContent = minutes + ":" + seconds; |
| 29 | + |
| 30 | + if (--remainingTime < 0) { |
| 31 | + remainingTime = 0; |
| 32 | + stopTimer(); |
| 33 | + } |
| 34 | + }, 1000); |
| 35 | + } |
| 36 | + // Stop the countdown timer |
| 37 | + function stopTimer() { |
| 38 | + if (timerInterval) { |
| 39 | + clearInterval(timerInterval); |
| 40 | + timerInterval = null; |
| 41 | + } |
| 42 | + isRunning = false; |
| 43 | + } |
| 44 | + |
| 45 | + // Save the div's current position to localStorage |
| 46 | + function savePosition(left, top) { |
| 47 | + localStorage.setItem('timerDivLeft', left); |
| 48 | + localStorage.setItem('timerDivTop', top); |
| 49 | + } |
| 50 | + |
| 51 | + // Load the div's last position from localStorage |
| 52 | + function loadPosition() { |
| 53 | + const left = localStorage.getItem('timerDivLeft'); |
| 54 | + const top = localStorage.getItem('timerDivTop'); |
| 55 | + return { left, top }; |
| 56 | + } |
| 57 | + |
| 58 | + // Create the timer div and append it to the body |
| 59 | + const timerDiv = document.createElement('div'); |
| 60 | + timerDiv.innerHTML = ` |
| 61 | + <div id="timerContainer"> |
| 62 | + <p>Time: <span id="time">60:00</span></p> |
| 63 | + <button id="startButton">Start</button> |
| 64 | + <button id="stopButton">Stop</button> |
| 65 | + </div> |
| 66 | + `; |
| 67 | + timerDiv.style.position = 'fixed'; |
| 68 | + timerDiv.style.background = 'rgba(0, 0, 0, 0.2'; |
| 69 | + timerDiv.style.color = 'white'; |
| 70 | + timerDiv.style.padding = '10px'; |
| 71 | + timerDiv.style.borderRadius = '5px'; |
| 72 | + timerDiv.style.cursor = 'move'; |
| 73 | + timerDiv.style.zIndex = '10000'; |
| 74 | + document.body.appendChild(timerDiv); |
| 75 | + |
| 76 | + // Restore position from localStorage or default to top-left corner |
| 77 | + const savedPosition = loadPosition(); |
| 78 | + timerDiv.style.left = savedPosition.left ? savedPosition.left + 'px' : '10px'; |
| 79 | + timerDiv.style.top = savedPosition.top ? savedPosition.top + 'px' : '10px'; |
| 80 | + |
| 81 | + // Make the div draggable |
| 82 | + let isDragging = false; |
| 83 | + let offsetX = 0; |
| 84 | + let offsetY = 0; |
| 85 | + |
| 86 | + timerDiv.addEventListener('mousedown', function(e) { |
| 87 | + isDragging = true; |
| 88 | + offsetX = e.clientX - timerDiv.getBoundingClientRect().left; |
| 89 | + offsetY = e.clientY - timerDiv.getBoundingClientRect().top; |
| 90 | + }); |
| 91 | + |
| 92 | + document.addEventListener('mousemove', function(e) { |
| 93 | + if (isDragging) { |
| 94 | + const newLeft = e.clientX - offsetX; |
| 95 | + const newTop = e.clientY - offsetY; |
| 96 | + timerDiv.style.left = newLeft + 'px'; |
| 97 | + timerDiv.style.top = newTop + 'px'; |
| 98 | + |
| 99 | + // Save the new position to localStorage |
| 100 | + savePosition(newLeft, newTop); |
| 101 | + } |
| 102 | + }); |
| 103 | + |
| 104 | + document.addEventListener('mouseup', function() { |
| 105 | + isDragging = false; |
| 106 | + }); |
| 107 | + |
| 108 | + // Initialize the timer display |
| 109 | + const display = document.querySelector('#time'); |
| 110 | + |
| 111 | + // Event listeners for Start and Stop buttons |
| 112 | + document.querySelector('#startButton').addEventListener('click', function() { |
| 113 | + startTimer(display); |
| 114 | + }); |
| 115 | + |
| 116 | + document.querySelector('#stopButton').addEventListener('click', function() { |
| 117 | + stopTimer(); |
| 118 | + }); |
| 119 | + |
| 120 | +})(); |
0 commit comments