From b9dfc2979e47367d4f55607c7f9b45c5de0a09db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98yvind=20Andreas=20Nilsen?= Date: Mon, 2 Sep 2024 16:07:27 +0200 Subject: [PATCH] added functionality to save custom countdown --- feide-microsoft.user.js | 85 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 82 insertions(+), 3 deletions(-) diff --git a/feide-microsoft.user.js b/feide-microsoft.user.js index 410ebfb..1055dce 100644 --- a/feide-microsoft.user.js +++ b/feide-microsoft.user.js @@ -1,8 +1,8 @@ // ==UserScript== // @name Feide - Auto-Press Microsoft Sign-In. // @namespace https://git.ntnu.no/M365-Drift/MonkeyMagic -// @version 1.0.1 -// @description Automatically press the Microsoft sign-in button after a countdown, with options to cancel or trigger manually. Use `Ctrl` + `Enter` to skip countdown. Use `Esc` to cancel countdown. +// @version 1.1.0 +// @description Automatically press the Microsoft sign-in button after a countdown, with options to cancel or trigger manually. Use `Ctrl` + `Enter` to skip countdown. Use `Esc` to cancel countdown. To set a custom countdown, use `Ctrl` + `Shift` + `S`. // @author Øyvind Nilsen (on@ntnu.no) // @match https://idp.feide.no/simplesaml/* // @icon https://www.google.com/s2/favicons?sz=64&domain=feide.no @@ -17,13 +17,27 @@ const button = document.getElementById('microsoft-signin-button'); if (!button) return; - let countdown = 5; + let countdown = getCountdown(); + + if (countdown === null) { + countdown = 5; + } + let countdownInterval; function updateButtonText() { button.value = `Use work or school account (${countdown}s)`; } + function storeCountdown(value) { + localStorage.setItem('countdown', value); + } + + // Function to retrieve the countdown value from local storage + function getCountdown() { + return localStorage.getItem('countdown'); + } + function startCountdown() { updateButtonText(); countdownInterval = setInterval(() => { @@ -43,12 +57,77 @@ button.value = 'Use work or school account'; } + function showCountdownInput() { + const overlay = document.createElement('div'); + overlay.style.position = 'fixed'; + overlay.style.top = '0'; + overlay.style.left = '0'; + overlay.style.width = '100%'; + overlay.style.height = '100%'; + overlay.style.backgroundColor = 'rgba(0, 0, 0, 0.5)'; + overlay.style.display = 'flex'; + overlay.style.justifyContent = 'center'; + overlay.style.alignItems = 'center'; + overlay.style.zIndex = '1000'; + + const box = document.createElement('div'); + box.style.backgroundColor = 'grey'; + box.style.padding = '20px'; + box.style.borderRadius = '10px'; + box.style.textAlign = 'center'; + + const label = document.createElement('label'); + label.textContent = 'Set custom countdown'; + label.style.display = 'block'; + label.style.marginBottom = '10px'; + label.style.color = 'white'; + + const input = document.createElement('input'); + input.type = 'number'; + input.min = '1'; + input.value = countdown; + input.style.marginBottom = '10px'; + input.style.width = '100%'; + + const saveButton = document.createElement('button'); + saveButton.textContent = 'Save'; + saveButton.style.marginRight = '10px'; + + const cancelButton = document.createElement('button'); + cancelButton.textContent = 'Cancel'; + + saveButton.addEventListener('click', function() { + const newCountdown = parseInt(input.value, 10); + if (!isNaN(newCountdown) && newCountdown > 0) { + countdown = newCountdown; + storeCountdown(countdown); + updateButtonText(); + } + document.body.removeChild(overlay); + }); + + cancelButton.addEventListener('click', function() { + document.body.removeChild(overlay); + }); + + box.appendChild(label); + box.appendChild(input); + box.appendChild(saveButton); + box.appendChild(cancelButton); + overlay.appendChild(box); + document.body.appendChild(overlay); + + input.focus(); + } + document.addEventListener('keydown', function(event) { if (event.key === 'Escape') { stopCountdown(); } else if (event.ctrlKey && event.key === 'Enter') { clearInterval(countdownInterval); button.click(); + } else if (event.ctrlKey && event.shiftKey && event.key === 'S') { + showCountdownInput(); } });