Get YouTube or Vimeo Thumbnails in one shot with jQuery

后端 未结 5 499
陌清茗
陌清茗 2021-02-06 19:44

I\'m trying to put YouTube thumbnail and Vimeo thumbnail together in the same script, but its not really easy for me because I\'m a new to jQuery.

I would to ask if some

5条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-06 20:43

    I used everything that Yi Jiang suggested. I had to change a couple of lines to get it working properly for me, the changes are noted with below:

    function processURL(url, success){
    var id;
    
    if (url.indexOf('youtube.com') > -1) {
        
        id = url.split('v=')[1].split('&')[0];
        return processYouTube(id);
    } else if (url.indexOf('youtu.be') > -1) {
        id = url.split('/')[1];
        return processYouTube(id);
    } else if (url.indexOf('vimeo.com') > -1) {
        
        if (url.match(/http:\/\/(www\.)?vimeo.com\/(\d+)($|\/)/)) {
            id = url.split('/')[1];
        } else if (url.match(/^vimeo.com\/channels\/[\d\w]+#[0-9]+/)) {
            id = url.split('#')[1];
        } else if (url.match(/vimeo.com\/groups\/[\d\w]+\/videos\/[0-9]+/)) {
            id = url.split('/')[4];
        } else {
            throw new Error('Unsupported Vimeo URL');
        }
    
        $.ajax({
            url: 'http://vimeo.com/api/v2/video/' + id + '.json',
            dataType: 'jsonp',
            success: function(data) {
                
                 success(data[0].thumbnail_large);
            }
        });
    } else {
        throw new Error('Unrecognised URL');
    }
    
    function processYouTube(id) {
        if (!id) {
            throw new Error('Unsupported YouTube URL');
        }
        
        success('http://i2.ytimg.com/vi/' + id + '/hqdefault.jpg');
    }
    }
    

    The 2 bottom changes were just to fix the 'success' spelling.

提交回复
热议问题