Greasy Fork is available in English.

Youtube Space=Pause

Pressing space when watching a video on Youtube will always pause the video instead of functioning like Page Down key.

< Σχολιασμός για τον κώδικα Youtube Space=Pause

Αναφορά: Εντάξει - ο κώδικας λειτουργεί αλλά έχει σφάλματα

§
Δημοσιεύτηκε: 11/05/2023

For some reason, it sometimes doesn't work, but I made a small change and now it works. The small change is replacing the checking if the keyCode is 32, with checking if the keyCode is either 32 or 'Space'. Here is the resulting code, for the main function:

window.addEventListener('keydown', function(e) {
    var playButton = document.querySelector('button.ytp-play-button');
    var isKey1 = e.keyCode === 32; // Space
    var isKey2 = e.keyCode === 'Space'; // Space
    var validTarget = e.target === document.body || e.target === document.querySelector('#player-api');
    if (validTarget && (isKey1 || isKey2) && playButton) {
        e.preventDefault();
        playButton.click();
    }
});
§
Δημοσιεύτηκε: 01/05/2024
Επεξεργάστηκε: 01/05/2024

For some reason, it sometimes doesn't work, but I made a small change and now it works. The small change is replacing the checking if the keyCode is 32, with checking if the keyCode is either 32 or 'Space'. Here is the resulting code, for the main function:

window.addEventListener('keydown', function(e) {
var playButton = document.querySelector('button.ytp-play-button');
var isKey1 = e.keyCode === 32; // Space
var isKey2 = e.keyCode === 'Space'; // Space
var validTarget = e.target === document.body || e.target === document.querySelector('#player-api');
if (validTarget && (isKey1 || isKey2) && playButton) {
e.preventDefault();
playButton.click();
}
});

THANKS, NOW WORK!

Δημοσίευση απάντησης

Συνδεθείτε για να δημοσιεύσετε μια απάντηση.