nodebb-plugin-cplayer
Version:
cPlayer's simple and naive plugin for nodebb.
43 lines • 1.55 kB
JavaScript
(function(){
"use strict";
let btoa = function(str){
let b = new Buffer(str);
return b.toString("base64");
}
let api = require("NeteaseCloudMusicApi").api;
let cplayer = {};
cplayer.parse = function(sth,callback){
let songRegex = /<a.*?href="http:\/\/music.163.com\/#[\/m\/]*\/song\?.*?id=(\d+).*?>.+?<\/a>/gi;
let cplayers = 0;
//console.log(sth);
if(!sth||!sth.postData||!sth.postData.content) return callback(null,sth);
let content = sth.postData.content;
if(content.match(songRegex)){
content.replace(songRegex,function(replacement,musicId){
let id = "cPlayer" + (cplayers++) + ("_"+sth.postData.pid);
let options = {};
api.song(musicId,(songInfo)=>{
songInfo = JSON.parse(songInfo).songs[0];
options.name = songInfo.name;
options.url = songInfo.mp3Url;
options.artist = songInfo.artists[0].name;
options.image = songInfo.album.picUrl;
//console.log(options);
api.lrc(musicId,(lrcJSON)=>{
lrcJSON = JSON.parse(lrcJSON);
if(lrcJSON.lrc!==undefined){
options.lyric = lrcJSON.lrc.lyric;
};
content = content.replace(songRegex,"<div id='"+id+"'><script>var "+id+"=new cPlayer(eval(atob(\""+btoa("({\"element\":document.getElementById('"+id+"'),list:["+JSON.stringify(options)+"]})")+"\")));</script>");
console.log(content);
sth.postData.content = content;
return callback(null,sth);
});
});
});
}else{
return callback(null,sth);
};
};
module.exports = cplayer;
})();