I have been trying to make a JS bookmarklet that will make a sound on any keydown and keyup events, and on most websites it's been working. However, it doesn't work in google docs, where I wanted to use it the most. This may sound like a duplicate of this question, but I promise that I tried all of the solutions and they did not work, probably because the question's from 11 years ago. Here's my code:
function random(min,max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1) + min);
}
function makeListeners(kind) {
const generic = [];
const space = [];
const enter = [];
const backspace = [];
for (let x = 0;x < 3;x++) {
generic.push(new Audio(`https://you-dont-need-to-know-my-website.com/audio1.mp3`));
space.push(new Audio(`https://you-dont-need-to-know-my-website.com/audio2.mp3`));
enter.push(new Audio(`https://you-dont-need-to-know-my-website.com/audio3.mp3`));
backspace.push(new Audio(`you-dont-need-to-know-my-website.com/audio4.mp3`));
}
document.addEventListener(kind, function(e) {
if (e.code != 'Space' && e.code != 'Enter' && e.code !='Backspace') {
generic[random(0,2)].play();
}
else {
eval(e.code.toLowerCase())[random(0,2)].play();
}
});
}
makeListeners('keydown');
makeListeners('keyup');