Greasy Fork镜像 is available in English.

Disable YouTube number shortcuts

Stop the anoying 0 to 9 YouTube shortcuts from ruining your wathing experience while allowing all the other shortcuts to work

< Commentaires sur Disable YouTube number shortcuts

Question / commentaire

§
Posté le: 2023-04-10

Hi, what about using isNaN instead of keys and keys.indexOf?

(function() {
    'use strict';

    (window.opera ? document.body : document).addEventListener('keydown', function(e) {
        if (isNaN(e.key) || e.isComposing || e.ctrlKey || e.altKey) return;
        e.cancelBubble = true;
        e.stopImmediatePropagation();
        return false;
    }, !window.opera);
})();
§
Posté le: 2023-04-10

I realised it also needs a e.key === ' ' condition

if (e.key === ' ' || isNaN(e.key) || e.isComposing || e.ctrlKey || e.altKey) return;`

The source code is based on a very old stackoverflow post

Obviously this script is just created by newbie.

(function () {
    const keys = new Set('0123456789'.split(''));
    // keys.add(' '); // You can add other keys. Check in https://www.toptal.com/developers/keycode
    document.addEventListener('keydown', function (e) {
        if (keys.has(e.key) || e.isComposing || e.ctrlKey || e.altKey) return;
        e.stopPropagation();
        e.stopImmediatePropagation();
    }, true);
})();
§
Posté le: 2025-06-28
(function () {
    const keys = new Set('0123456789'.split(''));
    // keys.add(' '); // You can add other keys. Check in https://www.toptal.com/developers/keycode
    document.addEventListener('keydown', function (e) {
        if (keys.has(e.key) || e.isComposing || e.ctrlKey || e.altKey) return;
        e.stopPropagation();
        e.stopImmediatePropagation();
    }, true);
})();

Why not const keys = '0123456789' and keys.includes(e.key) instead?

§
Posté le: 2025-06-28
Édité le: 2025-06-28

(function () {
const keys = new Set('0123456789'.split(''));
// keys.add(' '); // You can add other keys. Check in https://www.toptal.com/developers/keycode
document.addEventListener('keydown', function (e) {
if (keys.has(e.key) || e.isComposing || e.ctrlKey || e.altKey) return;
e.stopPropagation();
e.stopImmediatePropagation();
}, true);
})();
Why not const keys = '0123456789' and keys.includes(e.key) instead?

Yes it can.

But if you want to handle other keys as well, Set is a better choice.

If you want to test more keys, the keys string would be long and searching would be time consuming.

Set is O(1) which is fast.

For "0123456789" case, they should have no difference.

Poster une réponse

Connectez-vous pour poster une réponse.

长期地址
遇到问题?请前往 GitHub 提 Issues。