YouTube - Pause background videos

Pause videos playing in other background tabs. When you start playback of a video in foreground tab, background playback will be paused.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name         YouTube - Pause background videos
// @namespace    q1k
// @version      1.0.1
// @description  Pause videos playing in other background tabs. When you start playback of a video in foreground tab, background playback will be paused.
// @author       q1k
// @match        *://www.youtube.com/*
// @grant        none
// @run-at       document-start
// ==/UserScript==

function findElement(selector) {
    return new Promise(function(resolve) {
        if (document.querySelector(selector)) {
            return resolve(document.querySelector(selector));
        }
        const observer = new MutationObserver(function(mutations) {
            if (document.querySelector(selector)) {
                resolve(document.querySelector(selector));
                observer.disconnect();
            }
        });
        observer.observe(document, {
            childList: true,
            subtree: true
        });
    });
}

var video;
const channel = new BroadcastChannel("video-channel");

findElement("#player #movie_player video").then(function(el){
    video = el;
    /*if(!!(video.currentTime > 0 && !video.paused && !video.ended && video.readyState > 2)) {
        channel.postMessage("pause");
    }*/
    Object.defineProperty(el, 'isplaying', {
        get: function(){
            return !!(this.currentTime > 0 && !this.paused && !this.ended && this.readyState > 2);
        }
    });
    video.addEventListener("playing",function(){
        //start play, send pause
        if(document.hasFocus()){
            channel.postMessage("pause");
        }
    })
});

channel.addEventListener("message",function(ev){
    if(ev.data=="pause"){
        //received pause
        if(!document.hasFocus()){ //ignore if tab is active
            if(video.isplaying) {
                video.pause();
            }
        }
    }
});