Skip to content

Instantly share code, notes, and snippets.

@mzavoloka
Last active April 16, 2024 04:28
Show Gist options
  • Save mzavoloka/e2517fe6d435ea525fdb12471322dfda to your computer and use it in GitHub Desktop.
Save mzavoloka/e2517fe6d435ea525fdb12471322dfda to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Youtube embed redirect
// @namespace Mikhail Zavoloka
// @version 1.0
// @description Open youtube videos in embed mode (by replacing 'watch' to 'embed' in url)
// @match *://*.youtube.com/*
// @run-at document-start
// @grant window.onurlchange
// ==/UserScript==
function replace_watch_to_embed() {
if( !/youtube.com\/watch\?v\=/.test(location.href) ) { return }
var urlparams = new URLSearchParams(location.search);
var videoid = urlparams.get('v');
urlparams.delete('v');
urlparams.set('autoplay', 1);
urlparams.toString();
var new_url = location.protocol + "//"
+ location.host
+ '/embed/'
+ videoid
+ '?'
+ urlparams.toString()
+ location.hash;
location.replace(new_url);
};
replace_watch_to_embed()
// Must use urlchage bcs clicking video from youtube's main page doesn't trigger userscript reload
if (window.onurlchange === null) {
window.addEventListener('urlchange', (info) => replace_watch_to_embed());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment