github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/public/libs/CommentCoreLibrary/parsers/AcfunFormat.js (about) 1 /** 2 AcFun Format 3 Licensed Under MIT License 4 An alternative format comment parser 5 **/ 6 function AcfunParser(jsond){ 7 var list = []; 8 try{ 9 var jsondt = JSON.parse(jsond); 10 }catch(e){ 11 console.log('Error: Could not parse json list!'); 12 return []; 13 } 14 for(var i=0;i<jsondt.length;i++){ 15 //Read each comment and generate a correct comment object 16 var data = {}; 17 var xc = jsondt[i]['c'].split(','); 18 if(xc.length > 0){ 19 data.stime = parseFloat(xc[0]) * 1000; 20 data.color = parseInt(xc[1]) 21 data.mode = parseInt(xc[2]); 22 data.size = parseInt(xc[3]); 23 data.hash = xc[4]; 24 data.date = parseInt(xc[5]); 25 data.position = "absolute"; 26 if(data.mode != 7){ 27 data.text = jsondt[i].m.replace(/(\/n|\\n|\n|\r\n|\\r)/g,"\n"); 28 data.text = data.text.replace(/\r/g,"\n"); 29 data.text = data.text.replace(/\s/g,"\u00a0"); 30 }else{ 31 data.text = jsondt[i].m; 32 } 33 if(data.mode == 7){ 34 //High level positioned dm 35 try{ 36 var x = JSON.parse(data.text); 37 }catch(e){ 38 console.log('[Err] Error parsing internal data for comment'); 39 console.log('[Dbg] ' + data.text); 40 continue; 41 } 42 data.position = "relative"; 43 data.text = x.n; /*.replace(/\r/g,"\n");*/ 44 data.text = data.text.replace(/\ /g,"\u00a0"); 45 console.log(data.text); 46 if(x.a != null){ 47 data.opacity = x.a; 48 }else{ 49 data.opacity = 1; 50 } 51 if(x.p != null){ 52 data.x = x.p.x / 1000; // relative position 53 data.y = x.p.y / 1000; 54 }else{ 55 data.x = 0; 56 data.y = 0; 57 } 58 data.shadow = x.b; 59 data.dur = 4000; 60 if(x.l != null) 61 data.moveDelay = x.l * 1000; 62 if(x.z != null && x.z.length > 0){ 63 data.movable = true; 64 data.motion = []; 65 var moveDuration = 0; 66 var last = {x:data.x, y:data.y, alpha:data.opacity, color:data.color}; 67 for(var m = 0; m < x.z.length; m++){ 68 var dur = x.z[m].l != null ? (x.z[m].l * 1000) : 500; 69 moveDuration += dur; 70 var motion = { 71 x:{from:last.x, to:x.z[m].x/1000, dur: dur, delay: 0}, 72 y:{from:last.y, to:x.z[m].y/1000, dur: dur, delay: 0} 73 }; 74 last.x = motion.x.to; 75 last.y = motion.y.to; 76 if(x.z[m].t !== last.alpha){ 77 motion.alpha = {from:last.alpha, to:x.z[m].t, dur: dur, delay: 0}; 78 last.alpha = motion.alpha.to; 79 } 80 if(x.z[m].c != null && x.z[m].c !== last.color){ 81 motion.color = {from:last.color, to:x.z[m].c, dur: dur, delay: 0}; 82 last.color = motion.color.to; 83 } 84 data.motion.push(motion); 85 } 86 data.dur = moveDuration + (data.moveDelay ? data.moveDelay : 0); 87 } 88 if(x.r != null && x.k != null){ 89 data.rX = x.r; 90 data.rY = x.k; 91 } 92 93 } 94 list.push(data); 95 } 96 } 97 return list; 98 }