github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/public/libs/video-js/danmaku4vjs/danmaku4vjs.js (about)

     1  videojs.plugin('ABP', ABPinit);
     2  function ABPinit(){
     3  	function Danmu(ele){
     4  		///////////////////////////////////////////
     5  		///Prepare the html element for the plugin.
     6  		///////////////////////////////////////////
     7  		_this=this;
     8  		this.danmuDiv = document.createElement('div');
     9  		this.danmuDiv.className = 'vjs-danmu';
    10  		ele.el().insertBefore(this.danmuDiv,ele.el().getElementsByClassName('vjs-poster')[0]);
    11  
    12  		this.danmuShowControl = document.createElement('div');
    13  		this.danmuShowControl.className = 'vjs-danmu-control vjs-menu-button vjs-control';
    14  		this.danmuShowControlContent = document.createElement('span');
    15  		this.danmuShowControlContent.className = 'glyphicon glyphicon-eye-open';
    16  		this.danmuShowControl.appendChild(this.danmuShowControlContent);
    17  		ele.el().getElementsByClassName('vjs-control-bar')[0].appendChild(this.danmuShowControl);
    18  
    19  		///////////////////////////////////////////
    20  		//Bind CommentManager.
    21  		///////////////////////////////////////////
    22  		if(typeof CommentManager !== "undefined"){
    23  			this.cmManager = new CommentManager(this.danmuDiv);
    24  			this.cmManager.display = true;
    25  			this.cmManager.init();
    26  			this.cmManager.clear();
    27  
    28  			//Bind control to video.
    29  			var video=ele.el().children[0];
    30  			var lastPosition = 0;
    31  			video.addEventListener("progress", function(){
    32  				if(lastPosition == video.currentTime){
    33  					video.hasStalled = true;
    34  					_this.cmManager.stopTimer();
    35  				}else
    36  				lastPosition = video.currentTime;
    37  			});
    38  
    39  			video.addEventListener("timeupdate", function(){
    40  				if(_this.cmManager.display === false) return;
    41  				if(video.hasStalled){
    42  					_this.cmManager.startTimer();
    43  					video.hasStalled = false;
    44  				}
    45  				_this.cmManager.time(Math.floor(video.currentTime * 1000));
    46  			});
    47  
    48  			video.addEventListener("play", function(){
    49  				_this.cmManager.startTimer();
    50  			});
    51  
    52  			video.addEventListener("pause", function(){
    53  				_this.cmManager.stopTimer();
    54  			});
    55  
    56  			video.addEventListener("waiting", function(){
    57  				_this.cmManager.stopTimer();
    58  			});
    59  
    60  			video.addEventListener("playing",function(){
    61  				_this.cmManager.startTimer();
    62  			});
    63  
    64  			video.addEventListener("seeked",function(){
    65  				_this.cmManager.clear();
    66  			});
    67  
    68  			if(window){
    69  				window.addEventListener("resize", function(){
    70  					_this.cmManager.setBounds();
    71  				});
    72  			}
    73  
    74  			//Bind Control to button
    75  			this.danmuShowControl.addEventListener("click", function(){
    76  				if(_this.cmManager.display==true){
    77  					_this.cmManager.display=false;
    78  					_this.cmManager.clear();
    79  					_this.danmuShowControlContent.setAttribute("class","glyphicon glyphicon-eye-close");
    80  				}else{
    81  					_this.cmManager.display=true;
    82  					_this.danmuShowControlContent.setAttribute("class","glyphicon glyphicon-eye-open");
    83  				}
    84  			});
    85  
    86  			//Create Load function
    87  			this.load = function(url,callback){
    88  				if(callback == null)
    89  				  callback = function(){return;};
    90  				if (window.XMLHttpRequest){
    91  					xmlhttp=new XMLHttpRequest();
    92  				}
    93  				else{
    94  					xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    95  				}
    96  				xmlhttp.open("GET",url,true);
    97  				xmlhttp.send();
    98  				var cm = this.cmManager;
    99  				var cmvideo = video;
   100  				xmlhttp.onreadystatechange = function(){
   101  					if (xmlhttp.readyState==4 && xmlhttp.status==200){
   102  						if(navigator.appName == 'Microsoft Internet Explorer'){
   103  							var f = new ActiveXObject("Microsoft.XMLDOM");
   104  							f.async = false;
   105  							f.loadXML(xmlhttp.responseText);
   106  							cm.load(BilibiliParser(f));
   107  							cm.seek(cmvideo.currentTime*1000);
   108  							callback(true);
   109  						}else{
   110  							cm.seek(cmvideo.currentTime*1000);
   111  							cm.load(BilibiliParser(xmlhttp.responseXML));
   112  							callback(true);
   113  						}
   114  					}else
   115  					  callback(false);
   116  				}
   117  			}
   118  
   119  		}
   120  		return this;
   121  	}
   122  	this.danmu = new Danmu(this);
   123  }
   124