2016-05-11 232 views
0

所以,我在这个主题中看到了很多问题,但所有的答案都是从旧的gdata url开始的,现在已经不再适用了。
我想获得评论的每个视频,如果我点击此页面上的(附加)按钮:
https://www.youtube.com/feed/subscriptions
显然,这是一个tampermonkey脚本。
我的结局是,我无法从内容中获得一点难以理解的高峰,所以我可以决定是否我的whana点击了youtube链接。
也许我计划回复功能的评论,但这只是一个未来的计划。
我现在拥有的一切:如何使用Jquery和Youtube API V3获得Youtube评论?

// ==UserScript== 
// @name   hovercards for youtube 
// @namespace http://tampermonkey.net/ 
// @version  0.1 
// @description try to take over the world! 
// @author  You 
// @match  https://www.youtube.com/* 
// @grant  none 
// @require  https://code.jquery.com/jquery-latest.min.js 

// ==/UserScript== 
/* jshint -W097 */ 
    //'use strict'; 

jQuery(document).ready(function() { 
$("a.yt-uix-tile-link,.yt-ui-ellipsis-2k").on("mouseover",function() { 
     //Replace video block start  
$(function() { 
    $('a.yt-uix-tile-link,.yt-ui-ellipsis-2k').each(function() { 
     var yt_url = this.href, 
      yt_id = yt_url.split('?v=')[1], 
      yt_title = $(this).text(); 
     $(this).replaceWith('<div class="youtube-box" style="background-image:url(https://img.youtube.com/vi/' + yt_id + '/0.jpg);"><span class="youtube-title">' + yt_title + '</span><span class="youtube-bar"><span class="yt-bar-left"></span><span class="yt-bar-right"></span></span> </div>'); 
     $("div.youtube-box").on("mouseover",function() { 
      $(this).replaceWith('<iframe width="560" height="315" frameborder="0" allowfullscreen class="youtube-frame" src="https://www.youtube.com/embed/' + yt_id + '?autoplay=1"></iframe>'); 
     }); 
    }); 
}); 
    //Replace video block end 
}); 

}); 

而且我想separete代码isent工作在这个时候,所以我想补充这对课程的工作格式:

jQuery(document).ready(function() { 
jQuery("'a.yt-uix-tile-link,.yt-ui-ellipsis-2k").append("<input type='button' value='Give me my comments' class='button'>"); 
jQuery(".button").on("click",function() { 
jQuery.getJSON('https://www.googleapis.com/youtube/v3/comments?id=yVqreR8VXwQ&key=YOURAPIKEY&part=snippet',function(data){ 
//Replace the YOURAPI key section for your key 
if (typeof(data.items[0]) != "undefined") { 
    console.log('video exists ' + data.items[0].snippet.comment); 
    $(".result").append(data.items[0].snippet.comment); 
    } else { 
    console.log('video not exists'); 
    jQuery("result").append("Nope, we don't get any data"); 
} 
}); 
}); 
}); 

当我看着到broweser这个网址我的API密钥:
'https://www.googleapis.com/youtube/v3/comments?id=yVqreR8VXwQ&key=YOURAPIKEY&part=snippet'
我得到这样的结果:

{ 
"kind": "youtube#commentListResponse", 
"etag": "\"kiOs9cZLH2FUp6r6KJ8eyq_LIOk/pGLBhpjR05yQoJV31WoAx2PEFVw\"", 
"items": [] 
} 

我不明白为什么,为什么我没有得到任何物品?
网址对我来说很好。

回答

1
 GET https://www.googleapis.com/youtube/v3/commentThreads?key=AIzaSyDDBk8tAkod1VRRNyFZF09fgQyMpnSe5HI&textFormat=plainText&part=snippet&videoId=kffacxfA7G4&maxResults=50

你可以试试这个它的工作对我罚款。

+0

嗯看起来很好根据你的回答我做了这个: http://jsfiddle.net/bTLX8/15/ 唯一的问题是,我们不仅有一个视频,但很多,更多,我们需要chancge视频ID字符串到实际的视频ID。 这个怎么样? – user3545446