Skip to content

Commit

Permalink
added functionality to save custom countdown
Browse files Browse the repository at this point in the history
  • Loading branch information
on committed Sep 2, 2024
1 parent 7ab23d6 commit b9dfc29
Showing 1 changed file with 82 additions and 3 deletions.
85 changes: 82 additions & 3 deletions feide-microsoft.user.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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(() => {
Expand All @@ -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();
}
});

Expand Down

0 comments on commit b9dfc29

Please sign in to comment.