Skip to content

Instantly share code, notes, and snippets.

@jamesliu96
Last active April 23, 2017 03:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jamesliu96/96576d2420877e7729fb to your computer and use it in GitHub Desktop.
Save jamesliu96/96576d2420877e7729fb to your computer and use it in GitHub Desktop.
YouTube extension script for Live stream videos
/*! Damoo v2.1.9 | Copyright (c) 2015-2016 James Liu | Released under the MIT license */
;(function(b){var d=function(a,c,h,b){if(!(this instanceof d))return new d(a,c,h,b);this.canvas=new k(a,c,h,b);this.thread=new g(h)};d.version="v2.1.9";d.dom=b.document;var p=b.requestAnimationFrame||b.mozRequestAnimationFrame||b.webkitRequestAnimationFrame||b.msRequestAnimationFrame||b.oRequestAnimationFrame||function(a){return setTimeout(a,17)},q=b.cancelAnimationFrame||b.mozCancelAnimationFrame||b.webkitCancelAnimationFrame||b.webkitCancelRequestAnimationFrame||b.msCancelAnimationFrame||b.oCancelAnimationFrame||
function(a){clearTimeout(a)};d.prototype.show=function(){this.canvas.parent.appendChild(this.canvas.layer);return this};d.prototype.hide=function(){this.canvas.parent.removeChild(this.canvas.layer);return this};d.prototype.emit=function(a){"string"===typeof a&&(a={text:a});var c=a,b=this.canvas.font,f=d.dom.createElement("canvas"),e=f.getContext("2d");e.font=b;f.width=e.measureText(c.text).width;f.height=1.5*b.size;e.font=b;e.textAlign="start";e.textBaseline="top";c.shadow&&(e.shadowOffsetX=1,e.shadowOffsetY=
1,e.shadowColor="#fff",e.shadowColor=c.shadow.color);e.fillStyle="#fff";e.fillStyle=c.color;e.fillText(c.text,0,0);this.thread.push({canvas:f,fixed:a.fixed,index:this.thread.index,speed:.6*Math.pow(f.width,1/3),offset:{x:this.canvas.width,y:this.canvas.font.size*this.thread.index}});return this};d.prototype.clear=function(){this.thread.empty();return this};var l,m=function(){this.canvas.clear();for(var a=0;a<this.thread.pool.length;a++){var c=this.thread.get(a),b=c.offset.x;this.canvas.draw(c,b,c.offset.y);
c.offset.x-=c.speed;b<=-c.canvas.width&&this.thread.remove(a)}l=p(function(a){return function(){m.call(a)}}(this))};d.prototype.start=function(){void 0===this.state&&this.clear().show();this.state||(m.call(this),this.state=1);return this};d.prototype.suspend=function(){if(void 0===this.state)return this;q(l);this.state=0;return this};d.prototype.resume=function(){return this.start()};var k=function(a,c,b,f){this.parent=1==a.nodeType?a:d.dom.getElementById(a);this.id=c;this.rows=b;this.width=this.parent.offsetWidth;
this.height=this.parent.offsetHeight;12>this.height/this.rows&&(this.rows=this.height/12);this.font=new n(this.height/this.rows,f);this.layer=d.dom.createElement("canvas");this.context=this.layer.getContext("2d");this.layer.id=this.id;this.layer.width=this.width;this.layer.height=this.height;this.layer.style.display="block";this.layer.style.backgroundColor="transparent";this.parent.style.position?(this.layer.style.position="fixed",this.layer.style.left=this.parent.offsetLeft,this.layer.style.top=
this.parent.offsetTop):(this.parent.style.position="relative",this.layer.style.position="absolute",this.layer.style.left=0,this.layer.style.top=0);this.layer.style.zIndex=99999};k.prototype.clear=function(){this.context.clearRect(0,0,this.width,this.height)};k.prototype.draw=function(a,c,b){a.fixed&&(c=(this.width-a.canvas.width)/2);this.context.drawImage(a.canvas,c,b)};var n=function(a,b){this.size=a;this.family=b||"sans-serif"};n.prototype.toString=function(){return this.size+"px "+this.family};
var g=function(a){this.index=0;this.rows=a;this.pool=[]};g.prototype.push=function(a){this.index++;this.index>=this.rows&&(this.index=0);this.pool.push(a)};g.prototype.get=function(a){return this.pool[a]};g.prototype.remove=function(a){var b=this.get(a).index;this.index>b&&(this.index=b);this.pool.splice(a,1)};g.prototype.empty=function(){this.index=0;this.pool=[]};b.Damoo=d})(window);
var damoo = Damoo(document.querySelector('.html5-video-player'), 'dm-canvas', 30);
damoo.canvas.layer.height = damoo.canvas.height = damoo.canvas.height - document.querySelector(".ytp-chrome-bottom").offsetHeight;
damoo.canvas.layer.onclick = function() {
document.querySelector(".html5-main-video").click();
damoo.state ? damoo.suspend() : damoo.resume();
};
damoo.canvas.layer.style.opacity = .8;
damoo.start();
var damoo_list = [];
var damoo_last_id = "";
var interval = setInterval(function() {
damoo_list = [];
var s = document.querySelectorAll("#all-comments>li");
var t = document.querySelectorAll("#all-comments>li>.content>.comment-text");
if (t.length > 0) {
var last_index = 0;
for (var i = 0; i < t.length; i++) {
damoo_list[i] = t[i].innerText;
if (s[i].dataset['id'] == damoo_last_id) {
last_index = i;
}
}
for (var i = last_index + 1; i < damoo_list.length; i++) {
damoo.emit({ text: damoo_list[i], color: "#fff", shadow: { color: "#000" }});
}
damoo_last_id = s[t.length - 1].dataset['id'];
}
}, 2000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment