// ==UserScript== // @name 网易云音乐歌单列表一键导出CSV // @namespace http://tampermonkey.net/ // @version 1.1 // @description 一键导出全部歌曲(歌名和歌手)为 CSV 表格。 // @author jack // @license MIT // @match *://music.163.com/* // @grant none // @icon https://s1.music.126.net/style/favicon.ico // ==/UserScript== (function() { 'use strict'; if (window !== window.top) return; const btn = document.createElement('button'); btn.innerHTML = '导出全部歌曲'; btn.style.cssText = ` position: fixed; bottom: 30px; right: 30px; z-index: 99999; padding: 12px 20px; background-color: #d33a31; color: white; border: none; border-radius: 8px; font-size: 14px; font-weight: bold; cursor: pointer; box-shadow: 0 4px 6px rgba(0,0,0,0.3); transition: background-color 0.2s; `; btn.onmouseover = () => btn.style.backgroundColor = '#c23028'; btn.onmouseout = () => btn.style.backgroundColor = '#d33a31'; btn.onclick = function() { let doc; const iframe = document.getElementById('g_iframe'); if (iframe && iframe.contentDocument) { doc = iframe.contentDocument || iframe.contentWindow.document; console.log('使用iframe内的document'); } else { doc = document; console.log('未找到iframe,使用当前页面document'); } try { const songTable = doc.querySelector('table.m-table'); if (!songTable) { alert('未找到歌曲表格!请确认你在歌单页面,且已下滑加载所有歌曲。'); return; } const allRows = songTable.querySelectorAll('tr'); const dataRows = Array.from(allRows).filter(row => row.querySelector('.ttc')); console.log('找到歌曲行数量:', dataRows.length); if (dataRows.length === 0) { alert('当前页面没有加载出歌曲,请确保已经下滑到底部加载完毕!'); return; } let csvContent = '\uFEFF歌名,歌手\n'; dataRows.forEach(row => { const songEl = row.querySelector('.ttc b[title]') || row.querySelector('.ttc'); const songName = songEl ? (songEl.getAttribute('title') || songEl.textContent.trim()) : '未知歌名'; const artistEl = row.querySelector('td:nth-child(4) .text[title]') || row.querySelector('td:nth-child(4) a[title]'); const artistName = artistEl ? (artistEl.getAttribute('title') || artistEl.textContent.trim()) : '未知歌手'; csvContent += `"${songName}","${artistName}"\n`; }); const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' }); const url = URL.createObjectURL(blob); const link = document.createElement('a'); link.href = url; link.download = `网易云全部歌曲_${new Date().getTime()}.csv`; link.style.display = 'none'; link.addEventListener('click', function(e) { e.stopPropagation(); }); document.body.appendChild(link); link.click(); document.body.removeChild(link); URL.revokeObjectURL(url); alert(`导出成功!共 ${dataRows.length} 首歌曲`); } catch (error) { console.error('抓取数据时出错了:', error); alert(`抓取失败!错误信息:${error.message}\n请打开浏览器控制台(F12)查看详细报错`); } }; document.body.appendChild(btn); })();