Greasemonkey Youtube reloader

Background & Concept

When I was little, Greasemonkey helped me play some in-browser strategy games more efficiently. Just recently, I reinstalled it and started optimizing my browsing experience, e.g. by automatically removing annoying pop-ups. After finding myself reloading a newly discovered song on Youtube, I figured I’d rather just write a small greasemonkey script to do that for me. It can be configured to reload automatically or on fixed time intervals.

Code

// ==UserScript==
// @name     Youtube reload
// @version  1
// @include	 https://www.youtube.com/*
// @grant    none
// ==/UserScript==

var reloadAutomatically = true; // set false for fixed interval reloading

// FIXED INTERVAL RELOADING
var fixedReloadMinutes = 0;
var fixedReloadSeconds = 3;

function getFixedInterval(minutes, seconds){
	return (minutes*60+seconds)*1000;
}

// AUTOMATIC RELOADING
function getVideoLength(){
  var durationString = document.getElementsByClassName("ytp-time-duration")[0].textContent;
  var durationArray = durationString.split(":");
  var minutes = parseInt(durationArray[0]);
  var seconds = parseInt(durationArray[1]);
  return (60*minutes+seconds)*1000;
}


function reloadPage(){
  console.log("reloading");
  location.reload();
}


if (reloadAutomatically){
	setTimeout(reloadPage, getVideoLength());
}
else {
	setTimeout(reloadPage, getFixedInterval(fixedReloadMinutes,fixedReloadSeconds));
}