// ==UserScript==
// @name h-da AutoLogin (with saved credentials)
// @namespace http://my.h-da.de/
// @version 1.1
// @description Automatically logs into my.h-da.de after storing credentials securely in Tampermonkey
// @author You
// @match http://my.h-da.de/*
// @match https://my.h-da.de/*
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_deleteValue
// @run-at document-end
// ==/UserScript==
(function() {
'use strict';
async function getCredentials() {
let username = await GM_getValue("username", null);
let password = await GM_getValue("password", null);
// Ask user for credentials if not saved
if (!username || !password) {
username = prompt("Enter your h-da username:");
password = prompt("Enter your h-da password:");
if (username && password) {
await GM_setValue("username", username);
await GM_setValue("password", password);
alert("Credentials saved. You will be logged in automatically from now on.");
} else {
alert("Username or password missing. Script will not log in automatically.");
return null;
}
}
return { username, password };
}
async function tryLogin() {
const creds = await getCredentials();
if (!creds) return;
let loggedIn = false;
let tries = 20; // more tries in case of slow load
const interval = setInterval(() => {
if (loggedIn || tries <= 0) {
clearInterval(interval);
return;
}
tries--;
const loginForm = document.getElementById("loginForm");
if (loginForm) {
try {
const inputs = Array.from(loginForm.querySelectorAll("input"))
.filter(a => a.getAttribute("type") !== "hidden");
const [loginInput, passwordInput] = inputs;
loginInput.value = creds.username;
passwordInput.value = creds.password;
const submitButton = loginForm.querySelector("button[name=submit]");
if (submitButton) {
submitButton.click();
loggedIn = true;
}
} catch (e) {
console.error("AutoLogin error:", e);
}
}
}, 100);
}
// Add an optional way to reset saved credentials
unsafeWindow.resetHDAAuth = async function() {
await GM_deleteValue("username");
await GM_deleteValue("password");
alert("Saved credentials deleted. They will be requested again on next login.");
};
tryLogin();
})();