index .js 代码:
const { ipcRenderer } = require('electron')
const { $ } = require('./helper')
$('#add-music-button').addEventListener('click', () => {
ipcRenderer.send('add-music-window')
})
const renderListHTML = (tracks) => {
const reackList = $('#tracksList')
const tracksListHTML = tracks.reduce((html, track) => {
html += `<li class=" row music-track list-group-item d-flex justify-content-between align-items-center>
<div cl;ass="col-10">
<i class="fas fa-music mr-2"></i>
<b>${track.fileName}</b>
</div>
<div class="col-2">
<i class="fas fa-play mr-2"></i>
<i class="fas fa-trash-alt"></i>
</div>
</li>`
return html
}, '')
const emptyTrackHTML = '<div class="alert alert-primary">还没有添加任何音乐</div>'
tracksListHTML.innerHTML = tracks.length ? `<ul class="list-group">${tracksListHTML}</ul>` : emptyTrackHTML
}
ipcRenderer.on('getTracks', (event, tracks) => {
console.log('receive tracks', tracks)
renderListHTML(tracks)
})
index.html代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>本地播放器</title>
<link rel="stylesheet" href="../node_modules/bootstrap/dist/css/bootstrap.min.css">
<link href="https://cdn.bootcss.com/font-awesome/5.8.1/css/all.css" rel="stylesheet">
</head>
<body>
<div class="container mt-4">
<h1>我的网易云音乐</h1>
<button type="button" id="add-music-button" class="btn btn-primary btn-lg btn-block mt-4">
添加歌曲到曲库
</button>
<div id="tracksList" class="mt-4">
</div>
</div>
<script>
require('./index.js')
</script>
</body>
</html>