Last active
January 3, 2020 11:20
-
-
Save naoyeye/023867a42625c0adb1b6d01166217815 to your computer and use it in GitHub Desktop.
获取网易云音乐、虾米专辑页面的曲目列表和封面大图
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name 获取网易云音乐、虾米专辑页面的曲目列表和封面大图 | |
// @namespace getTrackListAndCoverFromMusicPlatform | |
// @version 0.2 | |
// @lastest 2020-01-02 | |
// @description try to take over the world! | |
// @author J.Y Han | |
// @match https://www.xiami.com/album/* | |
// @match https://music.163.com/* | |
// @grant none | |
// @require https://cdn.bootcss.com/jquery/1.7.2/jquery.min.js | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
console.log('test!!!!') | |
let imgSrc = ''; | |
let trList = []; | |
let listOutput = []; | |
let outputStr = ''; | |
// 网易 | |
if ((/https:\/\/music\.163\.com\/#\/album\?id=(.*)/g).test(window.location.href)) { | |
console.log('网易') | |
const gFrm = document.getElementById('g_iframe'); | |
gFrm.onload = gFrm.onreadystatechange = function() { | |
if (!(/https:\/\/music\.163\.com\/#\/album\?id=(.*)/g).test(window.location.href)) { | |
console.log('111') | |
return; | |
} | |
if (this.readyState && this.readyState !== 'complete') { | |
console.log('2222') | |
return; | |
} else { | |
console.log('正在获取网易云音乐专辑页面的曲目列表和封面大图') | |
initScript(); | |
} | |
} | |
function initScript() { | |
let $listWrap = null; | |
const interval = setInterval(() => { | |
$listWrap = $('#g_iframe').contents().find('#song-list-pre-cache'); | |
if ($listWrap.size() > 0) { | |
clearInterval(interval); | |
handleListAndCover(); | |
}; | |
}, 1000); | |
function handleListAndCover() { | |
imgSrc = $('#g_iframe').contents().find('.u-cover img.j-img').attr('data-src'); | |
trList = $listWrap.find('.j-flag tbody tr'); | |
trList.map((i, item) => { | |
const $item = $(item); | |
const num = $item.find('.num').text(); | |
const title = $item.find('.txt b').attr('title'); | |
const duration = $item.find('.u-dur').text(); | |
const itemMeta = `${num}: ${title} ${duration}`; | |
listOutput.push(itemMeta); | |
}); | |
outputStr = `<div style="padding: 10px;"><img src="${imgSrc}" style="max-width: 300px;" /></div><div style="line-height: 1.7;padding: 10px; font-size: 14px;">${listOutput.join('<br/>')}</div>`; | |
$listWrap.after(outputStr); | |
console.log('获取完毕') | |
} | |
} | |
} | |
// 虾米 | |
if ((/https:\/\/www.xiami.com\/album\/(.*)/g).test(window.location.href)) { | |
console.log('虾米') | |
/* | |
// 普通图片:pic.xiami.net/images/album/imgs/729/833/66afe850_2102428729_1533288871009.jpeg?x-oss-process=image/resize,limit_0,m_fill,s_390/quality,q_80/format,jpg | |
// 高清大图 pic.xiami.net/images/album/imgs/729/833/66afe850_2102428729_1533288871009.jpeg?x-oss-process=image/quality,q_80/format,jpg | |
*/ | |
imgSrc = $('.cover-wrapper .cover img').attr('src') | |
imgSrc = imgSrc.split('?x-oss-process=image/') | |
imgSrc = imgSrc[0] + '?x-oss-process=image/quality,q_80/format,jpg' | |
trList = $('.song-table tbody').find('tr'); | |
trList.map((i, item) => { | |
const $item = $(item); | |
const num = $item.find('.index').text(); | |
const title = $item.find('.song-name').text(); | |
const duration = $item.find('.duration').text(); | |
const itemMeta = `${num}: ${title} ${duration}`; | |
listOutput.push(itemMeta); | |
}); | |
outputStr = `<div style="padding: 10px;"><img src="${imgSrc}" style="max-width: 300px;" /></div><div style="line-height: 1.7;padding: 10px; font-size: 14px;">${listOutput.join('<br/>')}</div>`; | |
$('.disc-songs').after(outputStr); | |
console.log('获取完毕') | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment