Skip to content

Instantly share code, notes, and snippets.

@ticky
Created August 21, 2016 01:35
Show Gist options
  • Save ticky/603c3964c0f4b186c02a34abc7142c65 to your computer and use it in GitHub Desktop.
Save ticky/603c3964c0f4b186c02a34abc7142c65 to your computer and use it in GitHub Desktop.
Script to convert old youtube (object) embeds to new youtube (iframe) embeds
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