📄 lrc.js
字号:
curr_ko=null;
function LRCClass(){
var XMLHTTP = null;
var FORMAT_LRC = 1;
var FORMAT_SMI = 2;
var FORMAT_DEF = 3;
var player = null;
var playertype = "mediaplayer";
var url = "";
var output = "";
var content = "";
var LRC = new Array();
this.ddeLRC = new Array();
var format = FORMAT_LRC;
this.fixTime = 0;
this.lrctimer = null;
this.flag = "";
this.dispaly=1;
this.scObj="scrollId";
this.scLength=15;
if(arguments.length>=1) this.setPlayer(arguments[0]);
if(arguments.length>=2) this.setURL(arguments[1]);
if(arguments.length>=3) this.setOutput(arguments[2]);
if(arguments.length>=4) this.setDisplayType(arguments[3]);
if(arguments.length>=5) this.setScrollId(arguments[4]);
if(arguments.length>=6) this.setScLength(arguments[5]);
}
LRCClass.prototype.setPlayer=function(){
if(arguments.length>=1){
arg = arguments[0];
if(typeof(arg)=="string") this.player = document.getElementById(arg);
else if(typeof(arg)=="object") this.player = arg;
}
}
LRCClass.prototype.getPlayer=function(){
if( typeof(this.player)=='object') return this.player;
else return null;
}
LRCClass.prototype.setURL=function(){
if(arguments.length>=1){
this.url = arguments[0];
this.LRC="";
this.LRC = new Array();
this.ddeLRC = new Array();
}
}
LRCClass.prototype.getURL=function(){
return this.url;
}
LRCClass.prototype.setDisplayType=function(dtype){
this.display=dtype;
}
LRCClass.prototype.getDisplayType=function(){
return this.display;
}
LRCClass.prototype.setScrollId=function(sid){
if(typeof(sid)=="string") this.scObj = document.getElementById(sid)
else if(typeof(sid)=="object") this.scObj = sid;
//alert(this.scObj);
}
LRCClass.prototype.getScrollId=function(){
return this.scObj;
}
LRCClass.prototype.setScLength=function(sLength){
this.scLength=sLength;
}
LRCClass.prototype.getScLength=function(){
return this.scLength;
}
LRCClass.prototype.setContent=function(){
if(arguments.length>=1){
this.content = arguments[0];
}
}
LRCClass.prototype.getContent=function(){
return this.content;
}
LRCClass.prototype.setLRC=function(){
if(arguments.length>=1){
this.LRC = arguments[0];
}
}
LRCClass.prototype.getLRC=function(){
return this.LRC;
}
LRCClass.prototype.setOutput=function(str){
this.output = str;
}
LRCClass.prototype.getOutput=function(){
if(this.output!=""){
arg = this.output;
if(typeof(arg)=="string") return document.getElementById(arg);
else if(typeof(arg)=="object") return arg;
}
alert("can not found ouput area");
return null;
}
LRCClass.prototype.transformLRC=function(){
try{
if(this.getContent().length>0){
isGeci=true;
}else{
isGeci=false;
}
}catch(e){}
if( this.getContent()==null || this.getContent()=="" ){return;}
if( this.format == this.FORMAT_LRC ){
lines = this.getContent().split("\n");
arr = new Array();
str="";
for(i=0;i<lines.length;i++){
this.pushLine(trim(lines[i]));
}
this.ddeLRC.sort();
var mm = "aaa";
for(var i=this.ddeLRC.length-1;i>=0;i--){
if(this.ddeLRC[i]==mm){
mm = this.ddeLRC[i];
this.ddeLRC.splice(i,1);
}else{
mm = this.ddeLRC[i];
}
}
if(this.ddeLRC.length>0){//LRC
isLRC=true;
if(nonLRCHandle) clearTimeout(nonLRCHandle);
if(typeof(scrollId)!='undefined'){
scrollId.style.overflow='hidden';
scrollId.scrollTop=0;
}
}else{
isLRC=false;
if(typeof(scrollId)!='undefined'){
scrollId.style.overflow='auto';
scrollId.scrollTop=0;
}
nonLRC();
}
lasttime = 0;
idx = 0;
for(i=0;i<this.ddeLRC.length;i++){
if(this.ddeLRC[i].length>9){
_time = getLyrcTime(this.ddeLRC[i]);
_lrc = getLyrc(this.ddeLRC[i]);
if(_time<lasttime) {
continue;
}
lasttime = _time;
var dde = new LRCItem(_time,_lrc);
arr[idx++] = dde;
str+="arr["+(idx-1)+"]="+this.ddeLRC[i]+"...."+dde.time+"::"+dde.content+"\r\n";
}
}
var dde = new LRCItem(3600,"The End...");
arr[idx]= dde;
this.setLRC(arr);
}else{
return;
}
}
function trim(str)
{
return str.replace(/^\s*|\s*$/g,"");
}
function LRCClass.prototype.pushLine(line){
supper = this;
mm = line.split("]");
if(mm.length>=2){
text = mm[mm.length-1];
for(j=0;j<mm.length-1;j++){
lrcTime = formatTime(mm[j]+"]");
supper.ddeLRC.push(lrcTime+""+text);
}
}
}
function formatTime(str){
return str;
}
function printDdeLRC(){
s = "";
for(i=0;i<supper.ddeLRC.length;i++){
s += supper.ddeLRC[i]+"\r\n";
}
}
LRCClass.prototype.syncLRC=function(){
try{
var xmlhttp=false;
var str = "";
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (E) {
xmlhttp = false;
}
}
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
xmlhttp = new XMLHttpRequest();
}
supper = this;
var uu = this.getURL();
if (trim(uu)==""||typeof(uu)=='undefined'){
this.supper.setContent("");
//DataInfo.lrcState = 0;
return ;
}
xmlhttp.open("GET", uu,true);
supper = this;
xmlhttp.onreadystatechange=function()
{
try{
if (xmlhttp.readyState==4) {
if(xmlhttp.status<400){
str = unescape(xmlhttp.responseText);
if (str.indexOf("<title>无法找到网页</title>")>-1){
return ;
}
if(str=="") {
document.getElementById("contentLRC").innerHTML = "暂没找到歌词!";
}
this.supper.setContent(str);
}
else{
}
}
}
catch(eee){}
}
xmlhttp.send();
}catch(ee){}
}
LRCClass.prototype.start=function(){
this.flag = "start";
supper = this;
supper.output.innerHTML="欢迎使用SentFun音乐搜藏家……";
pp = supper.getPlayer();
this.lrctimer = window.setTimeout("detectSpeed(supper.getPlayer(),supper)",500);
}
LRCClass.prototype.destroy=function(){
this.flag = "stop";
if(this.lrctimer) clearTimeout(this.lrctimer);
this.url = "";
this.content="";
this.LRC="";
this.LRC = new Array();
this.LRC.length=0;
this.ddeLRC = new Array();
this.ddeLRC.length=0;
}
LRCClass.prototype.stop=function(){
this.flag = "stop";
if(this.lrctimer) clearTimeout(this.lrctimer);
}
function detectSpeed(player,ko){
try{
KO = ko;
var pp = player;
if(typeof(pp)=="string") pp = document.getElementById(pp);
if( ko.flag == "stop" ){
where = ko.getOutput();
if(where) where.innerHTML="欢迎使用SentFun音乐搜藏家……";
}else{
this.lrctimer = window.setTimeout("detectSpeed(pp,KO)",1000);
}
if(ko.getDisplayType()==0){
syncShowLyrc(KO);
}else{
syncVShowLyrc(KO);
}
}catch(E){document.title="Error..."+E.toString();}
}
function syncShowLyrc(ko){
try{
loader2 = ko.getPlayer();
where = ko.getOutput();
if(loader2){
curtime = loader2.controls.CurrentPosition;
lrcs = ko.getLRC();
if( typeof(lrcs)=='undefined' || lrcs.length==0 ){
ko.transformLRC();
lrcs = ko.getLRC();
}
for(j=0;j<lrcs.length;j++){
var lrc = lrcs[j];
var nextlrc = lrcs[(j<lrcs.length-1)?j+1:j];
t1=lrc.time+ko.fixTime;
t2=nextlrc.time+ko.fixTime;
if(t1<=curtime&&curtime<t2){
if(lrc.content)
where.innerHTML=lrc.content;
return;
}
}
}
}catch(E){document.title="syncShowLyrc="+E.toString();}
}
var last_match_line_seq = -1;
function syncVShowLyrc(ko){
try{
loader2 = ko.getPlayer();
where = ko.getOutput();
scid= ko.getScrollId();
sLength=ko.getScLength();
if(loader2){
lrcs = ko.getLRC();
//curtime = loader2.currentPosition;
curtime = loader2.controls.currentPosition;
if( typeof(lrcs)=='undefined' || lrcs.length==0 ){
ko.transformLRC();
lrcs = ko.getLRC();
}
if(isLRC&&nonLRCHandle) clearTimeout(nonLRCHandle);
var temp="";
var tmp_seq=0;
for(j=0;j<lrcs.length;j++){
var lrc = lrcs[j];
var nextlrc = lrcs[(j<lrcs.length-1)?j+1:j];
t1=lrc.time+ko.fixTime;
t2=nextlrc.time+ko.fixTime;
//alert(t1+'--'+curtime+'--'+t2)
if(t1<=curtime&&curtime<t2){
tmp_seq = j;
temp+="<span style='height:20px; line-height:20px;font-weight:bold;font-size:12px;margin-left:18px'> <span class='red1'>"+trim(lrcs[j].content)+"</span></span><br>"
if(typeof(scrollId)!='undefined') {scrollId.scrollTop=j*sLength;}
}else{
temp+="<span style='height:20px; line-height:20px;margin-left:22px'>"+lrc.content+"</span><br>"
}
}
if(where&&isGeci&&isLRC) {
if(last_match_line_seq != tmp_seq){where.innerHTML=temp}
}
last_match_line_seq = tmp_seq;
if(!isGeci){
if(typeof(scrollId)!='undefined'){
scrollId.style.overflow='hidden';
scrollId.scrollTop=0;
}
staticLRC.innerHTML='<br>暂无LRC歌词,<a href="http://search.sogua.com/lyric.asp?key='+escape(DataInfo.singer)+' '+ escape(DataInfo.song) +'" target="_blank">点击搜索TXT歌词</a> <a href=http://music.sogua.com/service/lrcadd.asp?id='+DataInfo.sid+'&song='+escape(DataInfo.song)+'&singer='+escape(DataInfo.singer)+' target=_blank><font color=#FF0000>网友添加歌词</font></a>';
if(nonLRCHandle) clearTimeout(nonLRCHandle);
}
}
}catch(E){
document.title="syncVShowLyrc="+E.toString();
}
}
function LRCItem(){
var time = -1;
var content = "";
if(arguments.length>=2){
this.time = arguments[0];
this.content = arguments[1];
}
}
function getLyrc(str){
try{
_ar = str.split("]");
return _ar[_ar.length-1];
}catch(E){
}
return "";
}
function getLyrcTime(str){
try{
t = str.split("]")[0].split("[")[1];
mm = t.split(":");
if(mm.length<2) return 0;
min = mm[1].split(".")[0];
t = parseInt(mm[0])*60+parseFloat(min);
if(!isNaN(t)) return t;
}catch(E){
//alert(E);
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -