Created
July 5, 2024 22:07
-
-
Save jph00/80176d8829b87b8412c526d6b1496f6a to your computer and use it in GitHub Desktop.
Chrome tampermonkey helper for the elderly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Keyboard Shortcut Scripts | |
// @namespace http://tampermonkey.net/ | |
// @version 0.1 | |
// @description Run scripts with keyboard shortcuts | |
// @match *://*/* | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
function getRGB(color) { | |
let rgb = color.match(/\d+/g); | |
if (!rgb) return [255, 255, 255]; | |
return rgb.map(x => parseInt(x)); | |
} | |
function getBrightness(rgb) { return (rgb[0] * 299 + rgb[1] * 587 + rgb[2] * 114) / 1000; } | |
function darkenColor(rgb) { return rgb.map(x => Math.max(Math.floor(x / 2), 0)); } | |
function rgbToString(rgb) { return `rgb(${rgb[0]}, ${rgb[1]}, ${rgb[2]})`; } | |
function setDarkTextOnLightBackground() { | |
document.querySelectorAll('*').forEach(el => { | |
let color = window.getComputedStyle(el).color; | |
let rgb = getRGB(color); | |
if (getBrightness(rgb) < 200) { | |
el.style.setProperty('color', rgbToString(darkenColor(rgb)), 'important'); | |
} | |
}); | |
} | |
document.addEventListener('keydown', function(e) { | |
if (e.ctrlKey && e.shiftKey) { | |
if (e.keyCode === 49) { // Key code for '1' | |
document.querySelectorAll('*').forEach(el => {el.style.background = 'white'}); | |
} else if (e.keyCode === 50) { | |
console.log('Executing Ctrl+Shift+2'); | |
setDarkTextOnLightBackground(); | |
} | |
} | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment