I've been trying to use the above example in a chrome extension I'm working on. It's an extension for scrobbling songs played on youtube to my last.fm account. I'm mostly interested in retrieving information about the duration of the song. The following code examples are taking from my extension content script (http://code.google.com/chrome/extensions/content_scripts.html)
Simple code to extract the video ID:
var videoID = document.URL.replace(/^[^v]+v.(.{11}).*/,"$1"); // get video id from URL
Then different approaches to use the code above:
var script = document.createElement("script");
script.setAttribute("type", "text/javascript");
script.setAttribute("src", "http://gdata.youtube.com/feeds/api/videos/"+ videoID +"?alt=json-in-script&callback=processData");
document.getElementsByTagName('head')[0].appendChild(script);
function processData(data) {
var title = data.entry.title.$t;
var duration = data.entry.media$group.media$content[0].duration;
}
And alternately, using a helper function:
function loadJSON(url) {
var headID = document.getElementsByTagName("head")[0];
var newScript = document.createElement('script');
newScript.type = 'text/javascript';
newScript.src = url;
headID.appendChild(newScript);
}
loadJSON("http://gdata.youtube.com/feeds/api/videos/"+ videoID +"?alt=json-in-script&callback=processData");
Any help would be much appreciated, thanks.