Drafts Action to Convert YouTube Link to Embeded Video

August 06, 2017 by Gabe | [mmd] |

I have no idea why it took me so long to create this action in Drafts. Often when I want to link to a YouTube video what I really want is in embedded video player to save people the effort of having to click through to see the video. So I lifted a bunch of stack exchange JavaScript to create this handy little Drafts action. Download the action from the Drafts directory.

Here's the bit of JavaScript that does all of the heavy lifting:

function youtube_parser(url){
var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
var match = url.match(regExp);
return (match&&match[7].length==11)? match[7] : false;

var sel = draft.content;

youTubeLink = draft.getTag("youtubeUrl_text");

var videoId = youtube_parser(youTubeLink);

var iframeMarkup = '<iframe width="560" height="315" src="//www.youtube.com/embed/' 
+ videoId + '" frameborder="0" allowfullscreen></iframe>';

draft.content = (sel+"\n\n\n"+iframeMarkup);


While this has some JS specific to reading and setting the Drafts content, most of the rest is pretty portable to another editor like 1Writer