Last active
January 20, 2018 17:31
-
-
Save sharethewisdom/56585798273ef6a0e98e393c5c95e9c5 to your computer and use it in GitHub Desktop.
userscript to make youtube user friendly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Youtube watch redirect | |
// @namespace http://tampermonkey.net/ | |
// @version 0.2 | |
// @description redirect watch pages, channel and user video listings and add some magic | |
// @author Bart De Roy | |
// @match https://www.youtube.com | |
// @match https://www.youtube.com/* | |
// @homepageURL https://gist.github.com/sharethewisdom/ | |
// @downloadURL https://gist.github.com/sharethewisdom/56585798273ef6a0e98e393c5c95e9c5/raw/3f7266847a61f8ee4a3e0250b5112359eb54c762/yt.user.js | |
// @run-at document-idle | |
// @grant none | |
// ==/UserScript== | |
(function () { | |
Number.prototype.between = function(a, b) { | |
var min = Math.min.apply(Math, [a, b]), | |
max = Math.max.apply(Math, [a, b]); | |
return this > min && this < max; | |
}; | |
var watchflags = '?autoplay=1&cc_load_policy=1&fs=0&modestbranding=1&rel=0', | |
userflags = '/videos?view=0&sort=p&flow=grid', | |
searchflags = '/results?sp=EgIQAQ%253D%253D&q=', | |
locparam, timeparam, loc, shorturl, | |
w = window, | |
i = 6, | |
yt = "Youtube userscript: "; | |
var checkRedirect = function(i) { | |
setTimeout(function () { | |
loc = w.location.href; | |
if (loc.indexOf("watch") !== -1) { | |
try { | |
locparam = loc.match(/(v=)([^#\&\?\#]*).*/); | |
} catch(err){console.log(yt + 'locparam ' + e);} | |
if (locparam) { | |
try { | |
timeparam = loc.match(/(\&t=)([^#\&\?\#]*).*/); | |
//calculate the timestamp in seconds | |
var timeparts = timeparam[timeparam.length-1].match(/(\d+H)?(\d+M)?(\d+S)?/i); | |
var seconds = (((parseInt(timeparts[1]) || 0) * 3600 ) + | |
((parseInt(timeparts[2]) || 0) * 60 ) + | |
(parseInt(timeparts[3]) || 0)); | |
if (seconds) watchflags += '&start=' + seconds.toString(); | |
} catch(err){console.log(yt + 'time ' + err);} | |
w.location.replace('https://www.youtube.com/embed/' + | |
locparam[locparam.length-1] + watchflags); | |
console.log(yt + 'redirect ' + locparam[locparam.length-1] ); | |
} else { | |
console.log(yt + 'redirect failed to find video id.'); | |
} | |
} | |
else if (loc.indexOf("user") !== -1) { | |
if (loc.indexOf("sort") == -1) { | |
locparam = loc.match(/(user\/)([^$\/]*).*/); | |
if (locparam) { | |
w.location.replace('https://www.youtube.com/user/' + | |
locparam[locparam.length-1] + userflags); | |
console.log(yt + 'redirect ' + locparam[locparam.length-1]); | |
} else { | |
console.log(yt + 'redirect failed to find user id.'); | |
} | |
} | |
} | |
else if (loc.indexOf("channel") !== -1) { | |
if (loc.indexOf("sort") == -1) { | |
locparam = loc.match(/(channel\/)([^$\/]*).*/); | |
if (locparam) { | |
w.location.replace('https://www.youtube.com/channel/' + | |
locparam[locparam.length-1] + userflags); | |
console.log(yt + 'redirect ' + locparam[locparam.length-1]); | |
} else { | |
console.log(yt + 'redirect failed to find channel id.'); | |
} | |
} | |
} | |
// this needs work | |
else if (loc.indexOf("results?search_query=") !== -1) { | |
var sessionlink = document.getElementsByClassName('filter-col')[1].getElementsByClassName('spf-link')[0].href; | |
console.log(yt + sessionlink); | |
if (sessionlink) { | |
w.location.replace(sessionlink); | |
console.log(yt + 'redirect video search'); | |
} else { | |
console.log(yt + 'redirect failed to find session link.'); | |
} | |
//} | |
//else { console.log(yt + 'video search already redirected');} | |
//}, 500); | |
} | |
if (--i) checkRedirect(i); | |
}, 100); | |
}; | |
var setPlaybackRate = function(r,v) { | |
var newrate = Math.round(r * (1 / 0.1)) / (1 / 0.1); | |
v.playbackRate = newrate; | |
console.log(yt + newrate); | |
}; | |
var modEmbed = function() { | |
setTimeout(function () { | |
var loc = w.location.href; | |
if (loc.indexOf("embed") !== -1) { | |
var sn = document.title.match(/Security Now (\d+).*/); | |
if (sn){ | |
var snotes = { | |
tag:document.createElement('a'), | |
href:"https://www.grc.com/sn/sn-" + sn[sn.length-1] + "-notes.pdf", | |
style: "position:absolute;top:.5rem;left:1rem;color:#ccc;" + | |
"padding:.1rem .6rem;background-color:#222;font-size:1rem;" + | |
"text-decoration:none;" | |
}; | |
snotes.tag.setAttribute('target', 'blank'); | |
snotes.tag.setAttribute('style', snotes.style); | |
snotes.tag.setAttribute('href', snotes.href); | |
snotes.tag.innerHTML = "Security Now shownotes ep. " + sn[sn.length-1]; | |
document.body.appendChild(snotes.tag); | |
} | |
try { | |
var player = document.getElementById("player"), | |
rate = 1; | |
player.getElementsByClassName("html5-video-player")[0].focus(); | |
player.getElementsByClassName("ytp-gradient-top")[0].remove(); | |
player.getElementsByClassName("ytp-chrome-top")[0].remove(); | |
var vid = player.getElementsByTagName("video")[0]; | |
document.addEventListener('copy', function(event){ | |
try { | |
locparam = loc.match(/embed\/([^$\?]*).*/); | |
} catch(err) { | |
console.log( yt + 'copy ' + err ); | |
} | |
if (locparam) { | |
shorturl = 'https://youtu.be/' + locparam[locparam.length-1]; | |
console.log(yt + 'copied ' + shorturl); | |
event.clipboardData.setData('text/plain', shorturl); | |
event.preventDefault(); | |
} | |
}); | |
player.addEventListener("keyup", function(event){ | |
switch (event.keyCode) { | |
case 27: | |
history.back(); | |
break; | |
case 109: | |
rate -= 0.1; | |
if (rate.between(0,2.1)) setPlaybackRate(rate,vid); | |
break; | |
case 107: | |
rate += 0.1; | |
if (rate.between(0,2.1)) setPlaybackRate(rate,vid); | |
break; | |
} | |
}, false); | |
console.log(yt + 'player focused'); | |
} | |
catch (e) { console.log(yt + e); } | |
} | |
}, 100); | |
}; | |
w.addEventListener('spfrequest', checkRedirect, false); | |
//w.addEventListener('load', checkRedirect, false); | |
//w.addEventListener('popstate', checkRedirect, false); | |
//w.addEventListener('load', modEmbed, false); | |
modEmbed(); | |
checkRedirect(); | |
})(); | |
// todo: | |
// playlist, | |
// user_uploads, | |
// search, | |
// hotkey to disable watch page redirection (persistent options?) | |
// embed?listType=playlist&list= | |
// window.location.href.replace() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can you make scripts that set a timer to automatically pause/stop on all Youtube embed videos? Like pause a video after X seconds!!?