Created
August 21, 2016 01:35
-
-
Save ticky/603c3964c0f4b186c02a34abc7142c65 to your computer and use it in GitHub Desktop.
Script to convert old youtube (object) embeds to new youtube (iframe) embeds
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
Array.from(document.querySelectorAll('param[name="src"][value*="youtube"]')).forEach(function(srcEl) { | |
var src = srcEl.getAttribute('value'); | |
if (!src) { | |
return; | |
} | |
var objectEl = srcEl.parentElement; | |
if (!objectEl) { | |
return; | |
} | |
var container = objectEl.parentElement; | |
if (!container) { | |
return; | |
} | |
var width = objectEl.getAttribute('width') || '420'; | |
var height = objectEl.getAttribute('height') || '336'; | |
src = 'https://www.youtube.com/embed/' + src.split('/').pop().split('?').shift(); | |
var iframe = document.createElement('iframe'); | |
iframe.setAttribute('width', width); | |
iframe.setAttribute('height', height); | |
iframe.setAttribute('src', src); | |
iframe.setAttribute('frameborder', '0'); | |
iframe.setAttribute('allowfullscreen', 'allowfullscreen'); | |
container.insertBefore(iframe, objectEl); | |
container.removeChild(objectEl); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment