注意:在保存之后,您可能需要清除浏览器缓存才能看到所作出的变更的影响。
?_=1
来访问最新页面。https://zh.moegirl.org.cn/User:Iehcni/js/lyricExtractor.js?_=1
/** * 自動抓取網易雲音樂雙語歌詞並按行數奇偶分開,便於使用{{LyricsKai}} * <s>最終目標是根據網易雲頁面自動生成條目源代碼</s> * * 日志: * v0.01-190502 基本功能已經實現,但無法應對有空行分隔的歌詞,暫時的解決方法是用控制臺修改 * #lyric-content 的内容 * v0.021-190503 支持了自定義輸入歌詞;增加一個報錯 * * 使用方法:打開音樂界面的控制臺,複製本段代碼並輸入lyric();或傳入已分行的歌詞參數 * 内容將以一個數組(含分別代表中外兩語的兩個元素)呈現 **/ function lyric(lyricOrigin/*, format TODO:當傳入LyricsKai參數後自動格式化 */) { var lyricTxt = new Array(); var lyricFmt = new Array(); var lyricOut = new Array(); var n, flag, diff; if(!lyricOrigin) { console.log('未輸入歌詞,自動查找歌詞...'); var lyric; if (document.querySelector('.g-iframe')) { if (document.querySelector('.g-iframe').contentWindow.document.querySelector('#lyric-content')) { lyric = document.querySelector('.g-iframe').contentWindow.document.querySelector('#lyric-content'); n = lyric.querySelectorAll('br').length; flag = '<br>'; diff = 4; lyricTxt[0] = lyric.innerHTML; } else { return console.log('未找到歌詞,請檢查是否在網易雲!'); } } else if (document.querySelector('#lyric-content')) { lyric = document.querySelector('#lyric-content'); n = lyric.querySelectorAll('br').length; flag = '<br>'; diff = 4; lyricTxt[0] = lyric.innerHTML; } else { return console.log('未找到歌詞,請檢查是否在網易雲!'); } } else { if (typeof lyricOrigin == 'string') { lyricTxt[0] = lyricOrigin; if (lyricTxt[0].split('/n').length > 1 ) { lyricTxt[0] += '/n'; flag = '/n'; n = lyricTxt[0].split('/n').length - 1; diff = 2; } else { return console.log('未找到換行符,請將歌詞分行後輸入!'); } } else { return console.log('請檢查輸入值是否為字符(以""包裹)!'); } } for (i=0; i<n; i++) { if (i==0) { lyricFmt[i] = lyricTxt[i].substring(i, lyricTxt[0].indexOf(flag)); } else { lyricTxt[i] = lyricTxt[i-1].substring(lyricTxt[i-1].indexOf(flag) + diff); lyricFmt[i] = lyricTxt[i].substring(0, lyricTxt[i].indexOf(flag)); } } for (i=0; i<n; i++) { if (lyricFmt[i] == '') { lyricOut[0]+='\n'; lyricOut[1]+='\n'; } else if (lyricOut[0]&&lyricOut[0].indexOf('\n\n')!=-1) { var sum = 0; var lyricTmp = new Array(); lyricTmp[0] = lyricOut[0]; for (j=1; j<lyricOut[0]; j++){ if (lyricTmp[j-1].indexOf('\n\n')!=-1) { sum+=1; lyricTmp[j] = lyricTmp[j-1].substring(lyricTmp[j-1].indexOf('\n\n')) if (sum%2==0) { if (i%2==0) { lyricOut[0]+=lyricFmt[i]+'\n'; } else { lyricOut[1]+=lyricFmt[i]+'\n'; } } else { if (i%2==0) { lyricOut[1]+=lyricFmt[i]+'\n'; } else { lyricOut[0]+=lyricFmt[i]+'\n'; } } } else { // TODO:支持分段 } } } else { if (i%2==0) { lyricOut[1]+=lyricFmt[i]+'\n'; } else { lyricOut[0]+=lyricFmt[i]+'\n'; } } } return lyricOut; }