Permalink
Browse files

Parse YouTube video_id from URL

  • Loading branch information...
1 parent 3b671c7 commit c40912a86833e1958ee8b4fb22db484be9dddab8 @dbarlett dbarlett committed Aug 7, 2012
Showing with 9 additions and 2 deletions.
  1. +9 −2 src/plugins/youtube.js
View
@@ -21,10 +21,17 @@ var _ytOpts;
*/
var _ytPoolMaps = {};
+function _ytIdFromUrl(url) {
+ var regExp = /.*(?:youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=)([^#\&\?]*).*/;
+ var match = url.match(regExp);
+ if (match&&match[1].length==11){
+ return match[1];
+ }
+}
function _ytStartPool(target) {
if (_ytTimeTriggers && _ytTimeTriggers.length) {
- var h = target['d']['videoData']['video_id'];
+ var h = _ytIdFromUrl(target['getVideoUrl']());
if (_ytPoolMaps[h]) {
_ytStopPool(target);
}else {
@@ -54,7 +61,7 @@ function _ytPool(target, hash) {
}
function _ytStopPool(target) {
- var h = target['d']['videoData']['video_id'];
+ var h = _ytIdFromUrl(target['getVideoUrl']());
if (_ytPoolMaps[h] && _ytPoolMaps[h].timer) {
_ytPool(target, h); // Pool one last time before clearing it.
clearTimeout(_ytPoolMaps[h].timer);

0 comments on commit c40912a

Please sign in to comment.