我试图从YouTube缩略图中抓取调色板,以便更好地将YouTube视频与整个网站设计融合在一起,我想知道是否有任何方法可以做到这一点?
我尝试过使用彩色小偷(https://github.com/lokesh/color-thief)和此代码(没有运气):
var img = new Image();
img.onload = function () {
var colorThief = new ColorThief();
colorThief.getColor(img);
};
img.crossOrigin = 'Anonymous';
img.src = 'http://img.youtube.com/vi/NuEfvIca0XU/mqdefault.jpg
从此我刚刚收到此错误:
Image from origin 'http://img.youtube.com' has been blocked from loading by Cross-Origin Resource Sharing policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access
如果没有从YouTube上下载缩略图并将其保存在我的服务器上,可以绕过此方法或抓取调色板吗?
答案 0 :(得分:3)
找到解决方案,但它涉及通过http://cors-anywhere.herokuapp.com/
传递imagelink这给我们留下了:
var img = new Image();
img.onload = function () {
var colorThief = new ColorThief();
colorThief.getColor(img);
};
img.crossOrigin = 'Anonymous';
img.src = 'http://cors-anywhere.herokuapp.com/http://img.youtube.com/vi/'+id+'/mqdefault.jpg';
答案 1 :(得分:0)
最好的想法是在服务器端实现这一点,因为出于安全原因,大多数浏览器会自动禁止通过javascript从其他域名获取内容。这就是你获得CSRF
的原因