this is script of simple video tag that I use in my html document
var video = document.getElementById('video'); var documentTitle = document.title; var updateTitleForVideo = function(state) { if(state === 'visible') { document.title=documentTitle; return; } document.title = documentTitle + ' ['+ state +']'; }; video.onpause = function() { updateTitleForVideo('Paused'); }; video.onplay = function() { updateTitleForVideo('Played'); }; document.addEventListener('visibilitychange' , function(){ var state = document.visibilityState; if(!video.paused){ if(state === 'hidden'){ video.pause(); updateTitleForVideo('Paused'); } } if(!video.played){ if(state === 'visible'){ video.play(); updateTitleForVideo('Played'); } } });const video = document.getElementById("video")
//The rest of your code
// video.onpause etc...
window.addEventListener("blur", function() {
video.pause();
})
window.addEventListener("focus", function(){
video.play();
})