-3

I am trying to write a regex for jquery to get all the data from a youtube search result. It's a chrome extension so when it gets integrated to the chrome browser when you open youtube like https://www.youtube.com/results?search_query=american+idol I want fetch all the data of the current videos in the results page. When you inspect the elements.

But I have to write something in jquery to fetch all those values for all the videos on the results page. Can anyone please help.

I hope my question is clear.

Thanks

Anonymous
  • 145
  • 1
  • 4
  • 14
  • Firstly, [don't use Regex for this](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags), parse the HTML and traverse it. Secondly, and more importantly, I'm don't believe this is even possible as you'll probably be prevented from retrieving the HTML of the YT page by the Same Origin Policy – Rory McCrossan Mar 01 '16 at 16:11
  • @Rory McCrossan thanks your link helped understand a few things and I did solve the problem. :) – Anonymous Mar 02 '16 at 11:21

1 Answers1

0

This is how I solved it, if it comes in handy for someone else later.

//Function to fetch the YouTube Id, Video Id, Title and Views 
function getDom(className) {
    if(className == '.yt-lockup-byline' || className == '.yt-lockup-title') {
        var items = $(className).find('a');
    } else if(className == '.yt-lockup-meta-info') {
        var items = $(className).find('li');
    }
    var length = items.length;
    var position = -1;
    var obj_b = next();

    function next() {
        position = (position + 1 < length) ? position + 1 : 0;
        return items.eq(position);
    }

    if (className == '.yt-lockup-byline') {
        $.each(obj_b.prevObject, function() {
            var str_result = this.attributes[2].nodeValue;
            console.log("YouTube ID :- "+str_result);
        });
    } else if (className == '.yt-lockup-meta-info') {
        $.each(obj_b.prevObject, function() {
            var str_result = this.innerHTML;
            console.log("Views :- "+str_result);
        });
    } else if (className == '.yt-lockup-title') {
        $.each(obj_b.prevObject, function() {
            var str_result = this.attributes[3].nodeValue;
            var str_video_id = this.attributes[0].nodeValue;
            console.log("YouTube Title :- "+str_result);
            console.log("YouTube Video ID :- "+str_video_id);
        });
    }

}
//For fetching YotuTube Id
getDom('.yt-lockup-byline');
//For fetching views and release date
getDom('.yt-lockup-meta-info');
//For fetching title and video id
getDom('.yt-lockup-title');
halfer
  • 19,824
  • 17
  • 99
  • 186
Anonymous
  • 145
  • 1
  • 4
  • 14