Last active
October 16, 2020 23:19
-
-
Save chadlavi/27c08ea6648d375c3f5de3ec7f43a015 to your computer and use it in GitHub Desktop.
a js script to embed youtube videos when a youtube url is present
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 embedder | |
// @version 2.0.3 | |
// @namespace https://gist.github.com/chadlavi/27c08ea6648d375c3f5de3ec7f43a015 | |
// @downloadURL https://gist.github.com/chadlavi/27c08ea6648d375c3f5de3ec7f43a015/raw/youtube-embedder.user.js | |
// @updateURL https://gist.github.com/chadlavi/27c08ea6648d375c3f5de3ec7f43a015/raw/youtube-embedder.user.js | |
// @description embed youtube videos when a url is present | |
// @author Chad Lavimoniere | |
// @exclude http*://*youtube.com* | |
// @exclude http*://*facebook.com* | |
// @exclude http*://*twitter.com* | |
// @exclude http*://mltshp.com* | |
// @exclude http*://news.ycombinator.com/* | |
// @exclude http*://*.duckduckgo.com/* | |
// @exclude http*://6.*.org/* | |
// @include http*://*.* | |
// @grant none | |
// ==/UserScript== | |
(() => { | |
'use strict' | |
function $e(t='div',p={},c=[]){ | |
let el=document.createElement(t) | |
Object.assign(el,p) | |
el.append(...c) | |
return el | |
} | |
const youtubeEmbed = (link) => { | |
const url = link.href.replace(/m\.youtube/, 'youtube') | |
.replace(/watch\?[^v]*v=/, 'embed/') | |
.replace(/\&.*$/, '') | |
.replace(/youtu\.be/, 'youtube.com/embed/') | |
const embed = $e( | |
'div', | |
{ | |
className: 'greasemonkey-youtube-embed' | |
}, | |
[ | |
$e('br'), | |
$e( | |
'div', | |
{ | |
style: 'float: none; clear: both; width: 100%; position: relative; padding-bottom: 56.25%; padding-top: 25px; height: 0;', | |
}, | |
[ | |
$e( | |
'object', | |
{ | |
style: 'position: absolute; top: 0; left: 0; width: 100%; height: 100%;', | |
data: url, | |
} | |
) | |
] | |
), | |
$e('br') | |
] | |
) | |
link.parentNode.insertBefore(embed, link) | |
} | |
const links = document.links | |
for (let i=0; i<links.length; i++){ | |
const link = links[i] | |
const {href, innerHTML, className} = link | |
if (( | |
href.match(/youtube.com\/watch/g) | |
|| href.match(/^youtu\.be\/.*/) | |
|| href.match(/^http.?:\/\/youtu\.be\/.*/) | |
) && !innerHTML.match(/</)) { | |
if (window.location.host.match(/reddit.com/)) { | |
if (!(className.match(/title/i) || className.match(/thumb/))) { | |
youtubeEmbed(link) | |
console.log(i+', '+link) | |
} | |
} else { | |
youtubeEmbed(link) | |
console.log(i+', '+link) | |
} | |
} | |
} | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment