From f3ab2e5e92c3e07edbd3029f76d076da84b4f1fd Mon Sep 17 00:00:00 2001 From: Psychedelic Squid Date: Mon, 12 Mar 2012 21:38:09 +0000 Subject: [PATCH 1/3] Auto-embed youtube videos, in much the same way as images are already done. --- public/ytembed.js | 22 ++++++++++++++++++++++ views/quotes.jade | 14 +++++++++++--- 2 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 public/ytembed.js diff --git a/public/ytembed.js b/public/ytembed.js new file mode 100644 index 0000000..b3b4ff3 --- /dev/null +++ b/public/ytembed.js @@ -0,0 +1,22 @@ +// let's fetch us some goddamn API +var apiEmbed = document.createElement('script'); +apiEmbed.src = 'http://www.youtube.com/player_api'; +document.getElementsByTagName('script')[0].parentNode.insertBefore(apiEmbed, document.getElementsByTagName('script')[0]); + +// this will be called by the player API when it's finished downloading +function onYouTubePlayerAPIReady() { + var youTubePlaceholders = document.getElementsByClassName('ytplaceholder'); + for(var i = 0; i < youTubePlaceholders.length; i++) { + var videoURL = youTubePlaceholders[i].innerText; + var videoIDMaybe = videoURL.match(/[?&]v=([A-Za-z0-9\-_]+)(?:[?&]|$)/); + youTubePlaceholders[i].innerText = ''; + if(videoIDMaybe) { + var ytVideoID = videoIDMaybe[1]; + var player = new YT.Player(youTubePlaceholders[i], { + height: '290', + width: '480', + videoId: ytVideoID + }); + } + } +} diff --git a/views/quotes.jade b/views/quotes.jade index 0010440..b0aedd5 100644 --- a/views/quotes.jade +++ b/views/quotes.jade @@ -1,11 +1,19 @@ ul#quotelist + -var hasYouTubeVids=false -each quote in quotes -if(quote.match(locals.url_regex)) li.quote - a(href=quote) - -if(quote.match(/(jpg|png|gif|jpeg|tiff)$/)) + -if(quote.match(/(jpg|png|gif|jpeg|tiff)$/)) + a(href=quote) img(src=quote) - -else + -else if(quote.match(/youtube.com\/watch/)) + -hasYouTubeVids = true + span(class='ytplaceholder') + =quote + -else + a(href=quote) =quote -else li.quote #{quote} + -if(hasYouTubeVids) + script(src='/ytembed.js') From 39f5bbdc8bc00929ce85bb0297ee78c2a7790b30 Mon Sep 17 00:00:00 2001 From: Psychedelic Squid Date: Mon, 12 Mar 2012 21:57:55 +0000 Subject: [PATCH 2/3] Smaller videos don't needlessly overlap each other. --- public/ytembed.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/public/ytembed.js b/public/ytembed.js index b3b4ff3..28e23bf 100644 --- a/public/ytembed.js +++ b/public/ytembed.js @@ -13,8 +13,8 @@ function onYouTubePlayerAPIReady() { if(videoIDMaybe) { var ytVideoID = videoIDMaybe[1]; var player = new YT.Player(youTubePlaceholders[i], { - height: '290', - width: '480', + height: '203', + width: '336', videoId: ytVideoID }); } From bef96ebc519eaf6643481b4d14a019041f9ebdf1 Mon Sep 17 00:00:00 2001 From: Psychedelic Squid Date: Mon, 12 Mar 2012 22:07:08 +0000 Subject: [PATCH 3/3] So apparently some people still use Firefox? --- public/ytembed.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/ytembed.js b/public/ytembed.js index 28e23bf..3a979b6 100644 --- a/public/ytembed.js +++ b/public/ytembed.js @@ -7,7 +7,7 @@ document.getElementsByTagName('script')[0].parentNode.insertBefore(apiEmbed, doc function onYouTubePlayerAPIReady() { var youTubePlaceholders = document.getElementsByClassName('ytplaceholder'); for(var i = 0; i < youTubePlaceholders.length; i++) { - var videoURL = youTubePlaceholders[i].innerText; + var videoURL = youTubePlaceholders[i].innerHTML; var videoIDMaybe = videoURL.match(/[?&]v=([A-Za-z0-9\-_]+)(?:[?&]|$)/); youTubePlaceholders[i].innerText = ''; if(videoIDMaybe) {