Skip to content

Instantly share code, notes, and snippets.

@henno
Created June 10, 2024 12:19
Show Gist options
  • Save henno/eebdda5894f57b34bf46b66ce5ceab23 to your computer and use it in GitHub Desktop.
Save henno/eebdda5894f57b34bf46b66ce5ceab23 to your computer and use it in GitHub Desktop.
Tampermonkey: Stuudium - hinnetelahtri taust roheline/punane
// ==UserScript==
// @name Grade Highlighter
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Highlights grades based on their value
// @author Your Name
// @match *://torvakool.ope.ee/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Function to change the background color based on the input value
function changeBackgroundColor(input) {
const value = input.value.trim();
if (['MA', '1', '2'].includes(value)) {
input.style.backgroundColor = 'pink';
} else if (value.match(/^(AR|3|4|5)([\+-]?)$/)) {
input.style.backgroundColor = 'lightgreen';
} else {
input.style.backgroundColor = ''; // Reset to default
}
}
// Function to observe changes in the specified inputs
function observeChanges() {
const inputs = document.querySelectorAll('div.grades.grades_entry.grades_allow_important.follows_custom_params > div > input');
inputs.forEach(input => {
// Initial check
changeBackgroundColor(input);
// Add event listener for changes
input.addEventListener('input', () => changeBackgroundColor(input));
});
}
// Function to set up the mutation observer
function setupMutationObserver() {
const targetNode = document.body;
const config = { childList: true, subtree: true };
const callback = function(mutationsList, observer) {
for (let mutation of mutationsList) {
if (mutation.type === 'childList' || mutation.type === 'subtree') {
observeChanges();
}
}
};
const observer = new MutationObserver(callback);
observer.observe(targetNode, config);
}
// Set up the mutation observer initially
setupMutationObserver();
// Also observe changes when the specified key is pressed
document.addEventListener('keypress', (event) => {
if (event.key === 'g') { // Replace 'g' with the key you want to activate the script
observeChanges();
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment