Last active
August 18, 2020 00:47
-
-
Save Meshiest/f104af2c0e4b6e589d33cca6a339c398 to your computer and use it in GitHub Desktop.
Automatically embed youtube videos in google search video results. Click "Raw" to install if you have tampermonkey
This file contains hidden or 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 Google Search Youtube Video Embedder | |
// @namespace http://tampermonkey.net/ | |
// @version 0.7 | |
// @description Automatically embed youtube videos in google search results | |
// @author github.com/meshiest | |
// @match https://www.google.com/search* | |
// @updateurl https://gist.github.com/Meshiest/f104af2c0e4b6e589d33cca6a339c398/raw/autoyoutubeembed.user.js | |
// @grant none | |
// ==/UserScript== | |
window.onload = event => { | |
'use strict'; | |
// find the cite elem | |
const citeElem = document.querySelector('cite'); | |
// make sure it's a youtube citation | |
if (!citeElem || citeElem.innerText !== 'www.youtube.com › watch' && citeElem.innerText !== 'm.youtube.com › watch') { | |
return; | |
} | |
// get the youtube link | |
const link = citeElem.parentNode.previousSibling.querySelector('a'); | |
// make sure it exists | |
if (!link) return; | |
// now get the original preview container (image and link) | |
const previewElem = citeElem.parentNode.parentNode.previousSibling; | |
// get the dimensions | |
const { width, height } = previewElem.getBoundingClientRect(); | |
// remove the preview element children | |
previewElem.childNodes.forEach(e => e.remove()); | |
// embed the iframe | |
const iframe = document.createElement('iframe'); | |
Object.entries({ | |
width, height, | |
src: link.href.replace('m.youtube', 'www.youtube').replace(/watch\?.*v=/, 'embed/'), | |
frameborder: '0', | |
allow: 'autoplay; encrypted-media', | |
allowfullscreen: true, | |
}).forEach(([key, value]) => iframe.setAttribute(key, value)); | |
// put it in the preview | |
previewElem.appendChild(iframe); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment