dsh-cloudmusic-player 0.5.0 → 0.7.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +322 -0
- package/README.md +16 -9
- package/client.js +84 -9
- package/cordis.patch.yml +6 -0
- package/index.js +114 -11
- package/lib/backend.mjs +3 -2
- package/lib/channel.mjs +229 -0
- package/lib/elog.mjs +53 -1
- package/lib/helper.ps1 +55 -15
- package/lib/like.mjs +287 -0
- package/lib/player.mjs +199 -3
- package/lib/smtc-browser.mjs +304 -0
- package/lib/smtc-probe.ps1 +190 -0
- package/lib/web-backend.mjs +1444 -0
- package/lib/winrt-audio.mjs +185 -0
- package/lib/winrt-audio.ps1 +163 -0
- package/locale/en.json +6 -1
- package/locale/zh.json +6 -1
- package/package.json +3 -3
- package/test/channel.test.mjs +79 -0
- package/test/elog.test.mjs +155 -2
- package/test/like.test.mjs +632 -0
- package/test/smtc-browser.test.mjs +240 -0
- package/test/web-backend.test.mjs +645 -0
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* winrt-audio.mjs — Node 侧行协议封装,驱动 lib/winrt-audio.ps1 常驻 WinRT 后台音频引擎。
|
|
3
|
+
*
|
|
4
|
+
* 为什么需要它:日推要在「个人 Edge 无 CDP + ego 关着 + 不弹任何页面」的前提下精确
|
|
5
|
+
* 播指定歌。网页 <audio> 要 CDP、SMTC 只能控制不能指定曲目、WMP COM 解不了 m4a 也不
|
|
6
|
+
* 注册媒体会话——只有 WinRT MediaPlayer 满足全部约束(无窗口、不弹页、不依赖 CDP)。
|
|
7
|
+
*
|
|
8
|
+
* 协议:stdin 一行一个 JSON 请求 {seq,cmd,...},stdout 一行一个 JSON 响应 {seq,...}。
|
|
9
|
+
* 第一行是 ready 握手 {seq:0,ok:true,ready:true}。沿用 player.mjs 的 seq 配对模式。
|
|
10
|
+
* 不依赖任何 npm 包;PowerShell 走 System32 绝对路径(宿主 PATH 裸名会 ENOENT)。
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { spawn } from 'node:child_process'
|
|
14
|
+
import { createInterface } from 'node:readline'
|
|
15
|
+
import { join, dirname } from 'node:path'
|
|
16
|
+
import { fileURLToPath } from 'node:url'
|
|
17
|
+
|
|
18
|
+
const HERE = dirname(fileURLToPath(import.meta.url))
|
|
19
|
+
const PS1 = join(HERE, 'winrt-audio.ps1')
|
|
20
|
+
|
|
21
|
+
/** System32 里 PowerShell 5.1 的固定路径;优先 env.SystemRoot,兜底 C:\Windows。 */
|
|
22
|
+
export function resolvePowershell() {
|
|
23
|
+
const root = process.env.SystemRoot || 'C:\\Windows'
|
|
24
|
+
return join(root, 'System32', 'WindowsPowerShell', 'v1.0', 'powershell.exe')
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const READY_TIMEOUT_MS = 8000
|
|
28
|
+
const DEFAULT_TIMEOUT_MS = 6000
|
|
29
|
+
|
|
30
|
+
export class WinrtAudio {
|
|
31
|
+
constructor(opts = {}) {
|
|
32
|
+
this.log = typeof opts.log === 'function' ? opts.log : () => {}
|
|
33
|
+
this.getConfig = typeof opts.getConfig === 'function' ? opts.getConfig : () => ({})
|
|
34
|
+
this.psPath = opts.psPath || resolvePowershell()
|
|
35
|
+
this.ps1Path = opts.ps1Path || PS1
|
|
36
|
+
this.requestTimeoutMs = Number(opts.requestTimeoutMs) || DEFAULT_TIMEOUT_MS
|
|
37
|
+
|
|
38
|
+
this.child = null
|
|
39
|
+
this.rl = null
|
|
40
|
+
this.starting = null
|
|
41
|
+
this.stopped = false
|
|
42
|
+
this.ready = false
|
|
43
|
+
this.lastError = ''
|
|
44
|
+
this.seq = 0
|
|
45
|
+
/** @type {Map<number,{resolve:Function,reject:Function,timer:any}>} */
|
|
46
|
+
this.pending = new Map()
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** 起常驻引擎(幂等)。返回 ready 信息。 */
|
|
50
|
+
async start() {
|
|
51
|
+
if (this.stopped) throw new Error('winrt audio stopped')
|
|
52
|
+
if (this.ready && this.child && !this.child.killed) return this._readyInfo()
|
|
53
|
+
if (this.starting) return this.starting
|
|
54
|
+
this.starting = this.#spawnOnce().finally(() => { this.starting = null })
|
|
55
|
+
return this.starting
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async #spawnOnce() {
|
|
59
|
+
const child = spawn(this.psPath, [
|
|
60
|
+
'-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Bypass',
|
|
61
|
+
'-File', this.ps1Path,
|
|
62
|
+
], { windowsHide: true, stdio: ['pipe', 'pipe', 'pipe'] })
|
|
63
|
+
this.child = child
|
|
64
|
+
this.ready = false
|
|
65
|
+
this.stderrTail = ''
|
|
66
|
+
|
|
67
|
+
child.stderr.setEncoding('utf8')
|
|
68
|
+
child.stderr.on('data', (chunk) => {
|
|
69
|
+
this.stderrTail = (this.stderrTail + chunk).slice(-4096)
|
|
70
|
+
})
|
|
71
|
+
child.stdin.on('error', (err) => {
|
|
72
|
+
this.lastError = `stdin: ${err && err.message ? err.message : err}`
|
|
73
|
+
})
|
|
74
|
+
child.on('exit', (code) => {
|
|
75
|
+
this.ready = false
|
|
76
|
+
this.child = null
|
|
77
|
+
// 所有在途请求立刻失败,别挂到超时
|
|
78
|
+
for (const [, p] of this.pending) {
|
|
79
|
+
clearTimeout(p.timer)
|
|
80
|
+
p.reject(new Error(`winrt audio exited (code=${code}) ${this.stderrTail.slice(-200)}`))
|
|
81
|
+
}
|
|
82
|
+
this.pending.clear()
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
const ready = await new Promise((resolve, reject) => {
|
|
86
|
+
let settled = false
|
|
87
|
+
const timer = setTimeout(() => {
|
|
88
|
+
if (settled) return
|
|
89
|
+
settled = true
|
|
90
|
+
try { child.kill() } catch { /* ignore */ }
|
|
91
|
+
reject(new Error(`winrt audio not ready in ${READY_TIMEOUT_MS}ms ${this.stderrTail.slice(-300)}`))
|
|
92
|
+
}, READY_TIMEOUT_MS)
|
|
93
|
+
|
|
94
|
+
this.rl = createInterface({ input: child.stdout })
|
|
95
|
+
this.rl.on('line', (line) => {
|
|
96
|
+
const text = String(line).trim()
|
|
97
|
+
if (!text) return
|
|
98
|
+
let msg
|
|
99
|
+
try { msg = JSON.parse(text) } catch {
|
|
100
|
+
this.log(`winrt-audio non-json: ${text.slice(0, 160)}`)
|
|
101
|
+
return
|
|
102
|
+
}
|
|
103
|
+
if (msg && msg.seq === 0 && msg.ready === true) {
|
|
104
|
+
if (settled) return
|
|
105
|
+
settled = true
|
|
106
|
+
clearTimeout(timer)
|
|
107
|
+
if (msg.ok === false) { reject(new Error(msg.detail || msg.error || 'winrt unavailable')); return }
|
|
108
|
+
this.ready = true
|
|
109
|
+
this.lastError = ''
|
|
110
|
+
resolve(this._readyInfo())
|
|
111
|
+
return
|
|
112
|
+
}
|
|
113
|
+
this.#dispatch(msg)
|
|
114
|
+
})
|
|
115
|
+
})
|
|
116
|
+
return ready
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
#dispatch(msg) {
|
|
120
|
+
if (!msg || typeof msg.seq !== 'number') return
|
|
121
|
+
const p = this.pending.get(msg.seq)
|
|
122
|
+
if (!p) return
|
|
123
|
+
this.pending.delete(msg.seq)
|
|
124
|
+
clearTimeout(p.timer)
|
|
125
|
+
p.resolve(msg)
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
_readyInfo() {
|
|
129
|
+
return { ok: true, ready: true, backend: 'winrt-mediaplayer', engine: this.ps1Path }
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** 发一条命令,等配对响应。 */
|
|
133
|
+
async request(cmd, payload = {}, timeoutMs) {
|
|
134
|
+
try { await this.start() } catch (err) {
|
|
135
|
+
return { ok: false, error: 'winrt-unavailable', detail: String(err?.message || err) }
|
|
136
|
+
}
|
|
137
|
+
const seq = ++this.seq
|
|
138
|
+
const msg = { seq, cmd, ...payload }
|
|
139
|
+
const timeout = Number(timeoutMs) || this.requestTimeoutMs
|
|
140
|
+
const res = await new Promise((resolve) => {
|
|
141
|
+
const timer = setTimeout(() => {
|
|
142
|
+
this.pending.delete(seq)
|
|
143
|
+
resolve({ ok: false, error: 'winrt-timeout', detail: `${cmd} timed out in ${timeout}ms` })
|
|
144
|
+
}, timeout)
|
|
145
|
+
this.pending.set(seq, { resolve, reject: (err) => resolve({ ok: false, error: 'winrt-error', detail: String(err?.message || err) }), timer })
|
|
146
|
+
try {
|
|
147
|
+
this.child.stdin.write(JSON.stringify(msg) + '\n')
|
|
148
|
+
} catch (err) {
|
|
149
|
+
clearTimeout(timer)
|
|
150
|
+
this.pending.delete(seq)
|
|
151
|
+
resolve({ ok: false, error: 'winrt-write-failed', detail: String(err?.message || err) })
|
|
152
|
+
}
|
|
153
|
+
})
|
|
154
|
+
if (res && res.ok === true) this.lastError = ''
|
|
155
|
+
else if (res && res.detail) this.lastError = String(res.detail)
|
|
156
|
+
return res
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** 播一首(URL 来自明文 song/url API)。 */
|
|
160
|
+
play({ url, songId, title, artist, positionMs, level, muted } = {}) {
|
|
161
|
+
return this.request('play', { url, songId, title, artist, positionMs, level, muted })
|
|
162
|
+
}
|
|
163
|
+
pause() { return this.request('pause') }
|
|
164
|
+
resume() { return this.request('resume') }
|
|
165
|
+
toggle() { return this.request('toggle') }
|
|
166
|
+
seek(positionMs) { return this.request('seek', { positionMs }) }
|
|
167
|
+
volume(level, muted) { return this.request('volume', { level, muted }) }
|
|
168
|
+
state() { return this.request('state') }
|
|
169
|
+
stop() { return this.request('stop') }
|
|
170
|
+
|
|
171
|
+
async close() {
|
|
172
|
+
this.stopped = true
|
|
173
|
+
if (this.child && !this.child.killed) {
|
|
174
|
+
try { this.child.stdin.write(JSON.stringify({ seq: ++this.seq, cmd: 'quit' }) + '\n') } catch { /* ignore */ }
|
|
175
|
+
try { this.child.kill() } catch { /* ignore */ }
|
|
176
|
+
}
|
|
177
|
+
this.child = null
|
|
178
|
+
this.rl = null
|
|
179
|
+
this.ready = false
|
|
180
|
+
for (const [, p] of this.pending) { clearTimeout(p.timer); try { p.resolve({ ok: false, error: 'winrt-closed' }) } catch { /* ignore */ } }
|
|
181
|
+
this.pending.clear()
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export default WinrtAudio
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
param()
|
|
2
|
+
|
|
3
|
+
# winrt-audio.ps1 - resident background audio engine for cloudmusic-player web channel.
|
|
4
|
+
# Reads one JSON command per line from stdin, writes one JSON response per line to stdout.
|
|
5
|
+
# Uses WinRT MediaPlayer (no window, no browser page, no CDP, not registered in SMTC).
|
|
6
|
+
# IMPORTANT: this file must stay pure ASCII. PowerShell 5.1 decodes script text with the
|
|
7
|
+
# system ANSI codepage (GBK on zh-CN); non-ASCII comments become mojibake / ParserError.
|
|
8
|
+
|
|
9
|
+
$ErrorActionPreference = 'Stop'
|
|
10
|
+
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
|
|
11
|
+
[Console]::InputEncoding = [System.Text.Encoding]::UTF8
|
|
12
|
+
|
|
13
|
+
function Out-Json([hashtable]$obj) {
|
|
14
|
+
[Console]::Out.WriteLine(($obj | ConvertTo-Json -Compress -Depth 8))
|
|
15
|
+
[Console]::Out.Flush()
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
try {
|
|
19
|
+
Add-Type -AssemblyName System.Runtime.WindowsRuntime -ErrorAction Stop
|
|
20
|
+
[Windows.Media.Playback.MediaPlayer,Windows.Media.Playback,ContentType=WindowsRuntime] | Out-Null
|
|
21
|
+
[Windows.Media.Core.MediaSource,Windows.Media.Core,ContentType=WindowsRuntime] | Out-Null
|
|
22
|
+
} catch {
|
|
23
|
+
Out-Json @{ seq=0; ok=$false; ready=$false; error='winrt-unavailable'; detail=($_.Exception.Message) }
|
|
24
|
+
exit 0
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
$mp = $null
|
|
28
|
+
$curSongId = ''
|
|
29
|
+
$curTitle = ''
|
|
30
|
+
$curArtist = ''
|
|
31
|
+
$curUrl = ''
|
|
32
|
+
|
|
33
|
+
function Get-Session {
|
|
34
|
+
if ($null -eq $mp) { return $null }
|
|
35
|
+
try { return $mp.PlaybackSession } catch { return $null }
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function Get-StateObj([int]$seq) {
|
|
39
|
+
$st = Get-Session
|
|
40
|
+
if ($null -eq $st) {
|
|
41
|
+
return @{ seq=$seq; ok=$true; active=$false; status='Paused'; pos=[double]0; end=[double]0; songId=$curSongId; title=$curTitle; artist=$curArtist; volume=[double]1; muted=$false }
|
|
42
|
+
}
|
|
43
|
+
$state = [string]$st.PlaybackState
|
|
44
|
+
$pos = [double]0
|
|
45
|
+
$end = [double]0
|
|
46
|
+
try { $pos = [double]$st.Position.TotalSeconds } catch {}
|
|
47
|
+
try { $end = [double]$st.NaturalDuration.TotalSeconds } catch {}
|
|
48
|
+
$vol = [double]1
|
|
49
|
+
$mut = $false
|
|
50
|
+
try { $vol = [double]$mp.Volume } catch {}
|
|
51
|
+
try { $mut = [bool]$mp.IsMuted } catch {}
|
|
52
|
+
return @{ seq=$seq; ok=$true; active=$true; status=$state; pos=$pos; end=$end; songId=$curSongId; title=$curTitle; artist=$curArtist; url=$curUrl; volume=$vol; muted=$mut }
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
Out-Json @{ seq=0; ok=$true; ready=$true; engine='winrt-mediaplayer' }
|
|
56
|
+
|
|
57
|
+
while ($true) {
|
|
58
|
+
$line = [Console]::In.ReadLine()
|
|
59
|
+
if ($null -eq $line) { break }
|
|
60
|
+
if ([string]::IsNullOrWhiteSpace($line)) { continue }
|
|
61
|
+
$req = $null
|
|
62
|
+
try { $req = $line | ConvertFrom-Json } catch {
|
|
63
|
+
Out-Json @{ seq=0; ok=$false; error='bad-json'; detail=$_.Exception.Message }
|
|
64
|
+
continue
|
|
65
|
+
}
|
|
66
|
+
$seq = 0
|
|
67
|
+
try { $seq = [int]$req.seq } catch {}
|
|
68
|
+
$cmd = [string]$req.cmd
|
|
69
|
+
try {
|
|
70
|
+
switch ($cmd) {
|
|
71
|
+
'play' {
|
|
72
|
+
$url = [string]$req.url
|
|
73
|
+
if ([string]::IsNullOrEmpty($url)) { Out-Json @{ seq=$seq; ok=$false; error='no-url' }; break }
|
|
74
|
+
if ($null -eq $mp) {
|
|
75
|
+
$mp = New-Object Windows.Media.Playback.MediaPlayer
|
|
76
|
+
$mp.IsLoopingEnabled = $false
|
|
77
|
+
}
|
|
78
|
+
# Source must be a MediaSource, not a raw System.Uri.
|
|
79
|
+
$src = [Windows.Media.Core.MediaSource]::CreateFromUri([System.Uri]$url)
|
|
80
|
+
$mp.Source = $src
|
|
81
|
+
$curUrl = $url
|
|
82
|
+
$curSongId = [string]$req.songId
|
|
83
|
+
$curTitle = [string]$req.title
|
|
84
|
+
$curArtist = [string]$req.artist
|
|
85
|
+
if ($null -ne $req.level) { try { $mp.Volume = [double]$req.level } catch {} }
|
|
86
|
+
if ($null -ne $req.muted) { try { $mp.IsMuted = [bool]$req.muted } catch {} }
|
|
87
|
+
$st = Get-Session
|
|
88
|
+
if ($null -ne $st) {
|
|
89
|
+
if ($null -ne $req.positionMs -and [double]$req.positionMs -gt 0) {
|
|
90
|
+
try { $st.Position = [TimeSpan]::FromMilliseconds([double]$req.positionMs) } catch {}
|
|
91
|
+
}
|
|
92
|
+
try { $st.PlaybackRate = 1.0 } catch {}
|
|
93
|
+
}
|
|
94
|
+
$mp.Play()
|
|
95
|
+
$r = Get-StateObj $seq
|
|
96
|
+
$r.cmd = 'play'
|
|
97
|
+
Out-Json $r
|
|
98
|
+
}
|
|
99
|
+
'pause' {
|
|
100
|
+
if ($null -ne $mp) { try { $mp.Pause() } catch {} }
|
|
101
|
+
$r = Get-StateObj $seq; $r.cmd = 'pause'; Out-Json $r
|
|
102
|
+
}
|
|
103
|
+
'resume' {
|
|
104
|
+
if ($null -ne $mp) {
|
|
105
|
+
$st = Get-Session
|
|
106
|
+
if ($null -ne $st) { try { $st.PlaybackRate = 1.0 } catch {} }
|
|
107
|
+
try { $mp.Play() } catch {}
|
|
108
|
+
}
|
|
109
|
+
$r = Get-StateObj $seq; $r.cmd = 'resume'; Out-Json $r
|
|
110
|
+
}
|
|
111
|
+
'toggle' {
|
|
112
|
+
if ($null -ne $mp) {
|
|
113
|
+
$st = Get-Session
|
|
114
|
+
$state = if ($null -ne $st) { [string]$st.PlaybackState } else { 'Paused' }
|
|
115
|
+
if ($state -eq 'Playing') {
|
|
116
|
+
try { $mp.Pause() } catch {}
|
|
117
|
+
} else {
|
|
118
|
+
if ($null -ne $st) { try { $st.PlaybackRate = 1.0 } catch {} }
|
|
119
|
+
try { $mp.Play() } catch {}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
$r = Get-StateObj $seq; $r.cmd = 'toggle'; Out-Json $r
|
|
123
|
+
}
|
|
124
|
+
'seek' {
|
|
125
|
+
$ms = [double]0
|
|
126
|
+
try { $ms = [double]$req.positionMs } catch {}
|
|
127
|
+
if ($ms -lt 0) { $ms = 0 }
|
|
128
|
+
$st = Get-Session
|
|
129
|
+
if ($null -ne $st) { try { $st.Position = [TimeSpan]::FromMilliseconds($ms) } catch {} }
|
|
130
|
+
$r = Get-StateObj $seq; $r.cmd = 'seek'; Out-Json $r
|
|
131
|
+
}
|
|
132
|
+
'volume' {
|
|
133
|
+
if ($null -ne $mp) {
|
|
134
|
+
if ($null -ne $req.level) { try { $mp.Volume = [double]$req.level } catch {} }
|
|
135
|
+
if ($null -ne $req.muted) { try { $mp.IsMuted = [bool]$req.muted } catch {} }
|
|
136
|
+
}
|
|
137
|
+
$r = Get-StateObj $seq; $r.cmd = 'volume'; Out-Json $r
|
|
138
|
+
}
|
|
139
|
+
'state' {
|
|
140
|
+
$r = Get-StateObj $seq; $r.cmd = 'state'; Out-Json $r
|
|
141
|
+
}
|
|
142
|
+
'stop' {
|
|
143
|
+
if ($null -ne $mp) {
|
|
144
|
+
try { $mp.Pause() } catch {}
|
|
145
|
+
try { $mp.Dispose() } catch {}
|
|
146
|
+
$mp = $null
|
|
147
|
+
}
|
|
148
|
+
$curSongId = ''; $curTitle = ''; $curArtist = ''; $curUrl = ''
|
|
149
|
+
Out-Json @{ seq=$seq; ok=$true; active=$false; status='Paused'; pos=[double]0; end=[double]0; songId=''; title=''; artist=''; cmd='stop' }
|
|
150
|
+
}
|
|
151
|
+
'quit' {
|
|
152
|
+
if ($null -ne $mp) { try { $mp.Dispose() } catch {}; $mp = $null }
|
|
153
|
+
Out-Json @{ seq=$seq; ok=$true; cmd='quit' }
|
|
154
|
+
break
|
|
155
|
+
}
|
|
156
|
+
default {
|
|
157
|
+
Out-Json @{ seq=$seq; ok=$false; error='bad-cmd'; detail=("unknown cmd: " + $cmd) }
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
} catch {
|
|
161
|
+
Out-Json @{ seq=$seq; ok=$false; error='cmd-failed'; detail=($_.Exception.Message) }
|
|
162
|
+
}
|
|
163
|
+
}
|
package/locale/en.json
CHANGED
|
@@ -1,13 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"meta": {
|
|
3
3
|
"title": "NetEase Music Player",
|
|
4
|
-
"description": "A small floating player inside DSH, wired to your local NetEase Cloud Music client: prev / play-pause / next, drag the volume, scrolling lyrics."
|
|
4
|
+
"description": "A small floating player inside DSH, wired to your local NetEase Cloud Music client: prev / play-pause / next, a like heart, drag the volume, scrolling lyrics."
|
|
5
5
|
},
|
|
6
6
|
"player": {
|
|
7
7
|
"play": "Play",
|
|
8
8
|
"pause": "Pause",
|
|
9
9
|
"next": "Next",
|
|
10
10
|
"prev": "Previous",
|
|
11
|
+
"like": "Like",
|
|
12
|
+
"unlike": "Unlike",
|
|
13
|
+
"albumLabel": "Album",
|
|
14
|
+
"sourceLabel": "Source",
|
|
15
|
+
"sourceUnknown": "Unknown",
|
|
11
16
|
"volume": "Volume",
|
|
12
17
|
"lyrics": "Lyrics",
|
|
13
18
|
"close": "Collapse",
|
package/locale/zh.json
CHANGED
|
@@ -1,13 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"meta": {
|
|
3
3
|
"title": "网易云播放器",
|
|
4
|
-
"description": "在 DSH 里挂一个悬浮小播放器,联动本机网易云音乐客户端:上一首 / 播放暂停 /
|
|
4
|
+
"description": "在 DSH 里挂一个悬浮小播放器,联动本机网易云音乐客户端:上一首 / 播放暂停 / 下一首、喜欢红心、拖音量、滚动歌词。"
|
|
5
5
|
},
|
|
6
6
|
"player": {
|
|
7
7
|
"play": "播放",
|
|
8
8
|
"pause": "暂停",
|
|
9
9
|
"next": "下一首",
|
|
10
10
|
"prev": "上一首",
|
|
11
|
+
"like": "喜欢",
|
|
12
|
+
"unlike": "取消喜欢",
|
|
13
|
+
"albumLabel": "专辑",
|
|
14
|
+
"sourceLabel": "来源",
|
|
15
|
+
"sourceUnknown": "未知",
|
|
11
16
|
"volume": "音量",
|
|
12
17
|
"lyrics": "歌词",
|
|
13
18
|
"close": "收起",
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-cloudmusic-player",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "DSH
|
|
3
|
+
"version": "0.7.19",
|
|
4
|
+
"description": "DSH 悬浮播放器:联动本地网易云音乐客户端或网页版,调音量 / 切歌 / 喜欢红心 / 跳进度 / 滚动歌词 / 一键日推",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"dsh",
|
|
7
7
|
"dsh-plugin",
|
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
"docs"
|
|
48
48
|
],
|
|
49
49
|
"scripts": {
|
|
50
|
-
"test": "node --test test/elog.test.mjs test/lyrics.test.mjs test/transport.test.mjs test/daily.test.mjs",
|
|
50
|
+
"test": "node --test test/elog.test.mjs test/lyrics.test.mjs test/transport.test.mjs test/daily.test.mjs test/like.test.mjs test/channel.test.mjs test/web-backend.test.mjs test/smtc-browser.test.mjs",
|
|
51
51
|
"enable-desktop": "node scripts/enable-desktop.mjs",
|
|
52
52
|
"sync": "powershell -NoProfile -ExecutionPolicy Bypass -File scripts/sync-github.ps1"
|
|
53
53
|
},
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// channel.mjs 单元测试
|
|
2
|
+
import { test } from 'node:test'
|
|
3
|
+
import assert from 'node:assert/strict'
|
|
4
|
+
import { ChannelRouter, detectClientRunning } from '../lib/channel.mjs'
|
|
5
|
+
|
|
6
|
+
function fakeNative() {
|
|
7
|
+
return {
|
|
8
|
+
backend: { id: 'windows-powershell', platform: 'win32' },
|
|
9
|
+
degraded: '', windowLeak: false, ready: true, restartLimit: 3,
|
|
10
|
+
state: async () => ({ ok: true, via: 'native' }),
|
|
11
|
+
action: async (a) => ({ ok: true, via: 'native', action: a }),
|
|
12
|
+
volume: async () => ({ ok: true, via: 'native' }),
|
|
13
|
+
readyInfo: () => ({ backend: 'windows-powershell', capabilities: {}, platform: 'win32' }),
|
|
14
|
+
deepHealth: async () => ({ ok: true, platform: 'win32' }),
|
|
15
|
+
on() {}, start() {}, stop() {}, restart: async () => ({ ok: true }),
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
function fakeWeb(loggedIn = true) {
|
|
19
|
+
return {
|
|
20
|
+
id: 'web-cdp', capabilities: { playback: true }, lastError: '',
|
|
21
|
+
_isLoggedIn: async () => loggedIn,
|
|
22
|
+
ensure: async () => ({ ok: true }),
|
|
23
|
+
state: async () => ({ ok: true, via: 'web' }),
|
|
24
|
+
action: async (a) => ({ ok: true, via: 'web', action: a }),
|
|
25
|
+
volume: async () => ({ ok: true, via: 'web' }),
|
|
26
|
+
deepHealth: async () => ({ ok: true, platform: 'web' }),
|
|
27
|
+
stop() {},
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
function router(mode, online, webOk) {
|
|
31
|
+
const r = new ChannelRouter({
|
|
32
|
+
native: fakeNative(), web: fakeWeb(webOk),
|
|
33
|
+
getConfig: () => ({ channelMode: mode, processName: 'cloudmusic' }),
|
|
34
|
+
log: () => {},
|
|
35
|
+
})
|
|
36
|
+
r.clientOnline = async () => online
|
|
37
|
+
r.webAvailable = async () => webOk
|
|
38
|
+
return r
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
test('channelMode 四种模式选择正确', async () => {
|
|
42
|
+
assert.equal(await router('client-only', true, true).pick(), 'native')
|
|
43
|
+
assert.equal(await router('client-only', false, true).pick(), 'native')
|
|
44
|
+
assert.equal(await router('web-only', true, true).pick(), 'web')
|
|
45
|
+
assert.equal(await router('client-first', true, true).pick(), 'native')
|
|
46
|
+
assert.equal(await router('client-first', false, true).pick(), 'web')
|
|
47
|
+
assert.equal(await router('web-first', true, true).pick(), 'web')
|
|
48
|
+
assert.equal(await router('web-first', true, false).pick(), 'native')
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
test('未知模式回退 client-first', async () => {
|
|
52
|
+
assert.equal(await router('???', true, true).pick(), 'native')
|
|
53
|
+
assert.equal(await router('???', false, true).pick(), 'web')
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
test('state/action/volume 走当前通道且形状一致', async () => {
|
|
57
|
+
const r = router('client-first', false, true)
|
|
58
|
+
assert.equal((await r.state()).via, 'web')
|
|
59
|
+
assert.equal((await r.action('next')).via, 'web')
|
|
60
|
+
assert.equal((await r.volume({ level: 0.5 })).via, 'web')
|
|
61
|
+
r._clientCache = { at: 0, value: null }
|
|
62
|
+
r.clientOnline = async () => true
|
|
63
|
+
assert.equal((await r.state()).via, 'native')
|
|
64
|
+
assert.equal((await r.action('next')).via, 'native')
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
test('backend/degraded/readyInfo 反映当前通道', async () => {
|
|
68
|
+
const r = router('web-only', true, true)
|
|
69
|
+
await r.pick()
|
|
70
|
+
assert.equal(r.backend.id, 'web-cdp')
|
|
71
|
+
assert.equal(r.readyInfo().backend, 'web-cdp')
|
|
72
|
+
assert.equal(r.readyInfo().capabilities.playback, true)
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
test('detectClientRunning 非 win32 返回 false', async () => {
|
|
76
|
+
if (process.platform !== 'win32') {
|
|
77
|
+
assert.equal(await detectClientRunning({}), false)
|
|
78
|
+
}
|
|
79
|
+
})
|
package/test/elog.test.mjs
CHANGED
|
@@ -22,6 +22,7 @@ import {
|
|
|
22
22
|
parseElogHeader,
|
|
23
23
|
eventTimeOf,
|
|
24
24
|
extractTrack,
|
|
25
|
+
extractSource,
|
|
25
26
|
classifyLine,
|
|
26
27
|
ElogWatcher,
|
|
27
28
|
defaultElogPath,
|
|
@@ -71,8 +72,11 @@ function line(rest, agoMs = 0, tag = '【playing】') {
|
|
|
71
72
|
}
|
|
72
73
|
|
|
73
74
|
/** 造「曲目详情」那条(playOneTrackInPlayingList / checkPlayPrivilege 同形) */
|
|
74
|
-
function trackLine({ id, name, artist, duration }, agoMs, kind = 'playOneTrackInPlayingList') {
|
|
75
|
-
const
|
|
75
|
+
function trackLine({ id, name, artist, duration, scene, href, text }, agoMs, kind = 'playOneTrackInPlayingList') {
|
|
76
|
+
const src = scene || href || text
|
|
77
|
+
? `"scene":"${scene || ''}","href":"${href || ''}","text":"${text || ''}",`
|
|
78
|
+
: ''
|
|
79
|
+
const json = `{"id":"${id}","displayOrder":1,${src}"localTrack":{},"track":{"id":"${id}","duration":${duration},"name":"${name}","artists":[{"id":"1","name":"${artist}"}],"album":{"id":"2","name":"专辑名"}}}`
|
|
76
80
|
return line(`,"${kind}",${json}`, agoMs)
|
|
77
81
|
}
|
|
78
82
|
|
|
@@ -86,6 +90,13 @@ function metaLine({ id, name, artist, duration }, agoMs, kind = 'nativePlay') {
|
|
|
86
90
|
return line(`,"${kind}",${mid}${json}`, agoMs)
|
|
87
91
|
}
|
|
88
92
|
|
|
93
|
+
/** 带顶层来源(scene/href/text)的 refreshCurPlaying,形状对齐真机日志 */
|
|
94
|
+
function sourceMetaLine({ id, name, artist, duration, scene, href, text }, agoMs, kind = 'refreshCurPlaying') {
|
|
95
|
+
const json = `{"id":"${id}","displayOrder":1,"ai":"","aiRcmd":false,"scene":"${scene}","href":"${href}","text":"${text}","localTrack":{},"track":{"id":"${id}","duration":${duration},"name":"${name}","artists":[{"id":"1","name":"${artist}"}],"album":{"id":"2","name":"专辑名"}}}`
|
|
96
|
+
const mid = kind === 'refreshCurPlaying' ? '"curPlaying=",' : ''
|
|
97
|
+
return line(`,"${kind}",${mid}${json}`, agoMs)
|
|
98
|
+
}
|
|
99
|
+
|
|
89
100
|
const startedLine = (id, duration, agoMs) =>
|
|
90
101
|
line(`,"native播放资源load完成,开始播放",{"songId":"${id}","songDuration":"${duration}"}`, agoMs)
|
|
91
102
|
|
|
@@ -196,6 +207,35 @@ test('extractTrack 对没有 id / name 的片段返回 null', () => {
|
|
|
196
207
|
assert.equal(extractTrack(undefined), null)
|
|
197
208
|
})
|
|
198
209
|
|
|
210
|
+
test('extractSource 从 curPlaying 顶层取 scene/href/text', () => {
|
|
211
|
+
const text =
|
|
212
|
+
'{"id":"1","scene":"playlist","href":"/playlist/18334824318","text":"临时","localTrack":{},"track":{"id":"1"}}'
|
|
213
|
+
assert.deepEqual(extractSource(text), {
|
|
214
|
+
scene: 'playlist',
|
|
215
|
+
href: '/playlist/18334824318',
|
|
216
|
+
text: '临时',
|
|
217
|
+
})
|
|
218
|
+
})
|
|
219
|
+
|
|
220
|
+
test('extractSource 顶层无来源字段时返回 null(不被 track 内同名字段误伤)', () => {
|
|
221
|
+
// 核心防错点:firstString 取第一个匹配,若不在 "localTrack" 处截断,
|
|
222
|
+
// 就会把 track 内部的 scene/text 当成来源 → 显示错误。
|
|
223
|
+
const text = '{"id":"1","localTrack":{},"track":{"scene":"track内","text":"track内","name":"歌"}}'
|
|
224
|
+
assert.equal(extractSource(text), null)
|
|
225
|
+
})
|
|
226
|
+
|
|
227
|
+
test('extractSource 还原 text 里的 JSON 转义', () => {
|
|
228
|
+
// 源文本里 text 值是 "我\"歌单\""(JSON 转义),捕获后应还原成 我"歌单"
|
|
229
|
+
const text = '{"scene":"playlist","text":"我\\"歌单\\"","localTrack":{}}'
|
|
230
|
+
assert.deepEqual(extractSource(text), { scene: 'playlist', href: '', text: '我"歌单"' })
|
|
231
|
+
})
|
|
232
|
+
|
|
233
|
+
test('extractSource 对空/非字符串输入返回 null', () => {
|
|
234
|
+
assert.equal(extractSource(undefined), null)
|
|
235
|
+
assert.equal(extractSource(''), null)
|
|
236
|
+
assert.equal(extractSource('{"foo":1,"localTrack":{}}'), null)
|
|
237
|
+
})
|
|
238
|
+
|
|
199
239
|
test('classifyLine 区分「开始load」与「load完成,开始播放」', () => {
|
|
200
240
|
// 「native播放资源开始load」是另一条事件(还没出声),不能当成起播
|
|
201
241
|
assert.equal(classifyLine(',"native播放资源开始load",{"songId":"1"}'), null)
|
|
@@ -606,6 +646,119 @@ test('Watcher:refreshCurPlaying 在播放中途出现时不打断进度', () =
|
|
|
606
646
|
}
|
|
607
647
|
})
|
|
608
648
|
|
|
649
|
+
test('Watcher:refreshCurPlaying 记录来源,nativePlay 不清除(last-known)', () => {
|
|
650
|
+
const file = tmpFile()
|
|
651
|
+
writeLog(file, [
|
|
652
|
+
sourceMetaLine(
|
|
653
|
+
{ id: '3', name: '正在播', artist: 'P', duration: 300_000, scene: 'playlist', href: '/playlist/18334824318', text: '临时' },
|
|
654
|
+
0,
|
|
655
|
+
),
|
|
656
|
+
])
|
|
657
|
+
const w = new ElogWatcher({ filePath: file })
|
|
658
|
+
w.start()
|
|
659
|
+
try {
|
|
660
|
+
assert.deepEqual(w.getState().source, {
|
|
661
|
+
scene: 'playlist',
|
|
662
|
+
href: '/playlist/18334824318',
|
|
663
|
+
text: '临时',
|
|
664
|
+
})
|
|
665
|
+
|
|
666
|
+
// 之后来一条不带来源的 nativePlay,来源应保持 last-known
|
|
667
|
+
appendLog(file, [
|
|
668
|
+
metaLine({ id: '3', name: '正在播', artist: 'P', duration: 300_000 }, 0, 'nativePlay'),
|
|
669
|
+
])
|
|
670
|
+
w.poll()
|
|
671
|
+
assert.equal(w.getState().source.text, '临时')
|
|
672
|
+
} finally {
|
|
673
|
+
w.stop()
|
|
674
|
+
}
|
|
675
|
+
})
|
|
676
|
+
|
|
677
|
+
test('Watcher:来源 text 为空也记录(前端显示「未知」)', () => {
|
|
678
|
+
const file = tmpFile()
|
|
679
|
+
writeLog(file, [
|
|
680
|
+
sourceMetaLine(
|
|
681
|
+
{ id: '4', name: '歌', artist: 'Q', duration: 200_000, scene: 'orpheusMessage', href: '', text: '' },
|
|
682
|
+
0,
|
|
683
|
+
),
|
|
684
|
+
])
|
|
685
|
+
const w = new ElogWatcher({ filePath: file })
|
|
686
|
+
w.start()
|
|
687
|
+
try {
|
|
688
|
+
const s = w.getState().source
|
|
689
|
+
assert.ok(s, '来源对象应存在(含空 text)')
|
|
690
|
+
assert.equal(s.text, '')
|
|
691
|
+
assert.equal(s.scene, 'orpheusMessage')
|
|
692
|
+
} finally {
|
|
693
|
+
w.stop()
|
|
694
|
+
}
|
|
695
|
+
})
|
|
696
|
+
|
|
697
|
+
test('Watcher:playOneTrackInPlayingList 顶层来源写入 source(每次切歌必发)', () => {
|
|
698
|
+
const file = tmpFile()
|
|
699
|
+
writeLog(file, [
|
|
700
|
+
trackLine(
|
|
701
|
+
{ id: '5', name: 'Letting Go', artist: '蔡健雅', duration: 259_000, scene: 'playlist', href: '', text: '穗织第一幼刀_喜欢的音乐' },
|
|
702
|
+
0,
|
|
703
|
+
),
|
|
704
|
+
])
|
|
705
|
+
const w = new ElogWatcher({ filePath: file })
|
|
706
|
+
w.start()
|
|
707
|
+
try {
|
|
708
|
+
assert.deepEqual(w.getState().source, {
|
|
709
|
+
scene: 'playlist',
|
|
710
|
+
href: '',
|
|
711
|
+
text: '穗织第一幼刀_喜欢的音乐',
|
|
712
|
+
})
|
|
713
|
+
} finally {
|
|
714
|
+
w.stop()
|
|
715
|
+
}
|
|
716
|
+
})
|
|
717
|
+
|
|
718
|
+
test('Watcher:随后无来源的 track 事件不清除上次来源(last-known)', () => {
|
|
719
|
+
const file = tmpFile()
|
|
720
|
+
writeLog(file, [
|
|
721
|
+
trackLine(
|
|
722
|
+
{ id: '5', name: 'Letting Go', artist: '蔡健雅', duration: 259_000, scene: 'playlist', href: '', text: '临时歌单' },
|
|
723
|
+
4_000,
|
|
724
|
+
),
|
|
725
|
+
])
|
|
726
|
+
const w = new ElogWatcher({ filePath: file })
|
|
727
|
+
w.start()
|
|
728
|
+
try {
|
|
729
|
+
assert.equal(w.getState().source.text, '临时歌单')
|
|
730
|
+
// 同次起播连发的那条无来源事件(checkPlayPrivilege)不该清空来源
|
|
731
|
+
appendLog(file, [
|
|
732
|
+
trackLine({ id: '5', name: 'Letting Go', artist: '蔡健雅', duration: 259_000 }, 0, 'checkPlayPrivilege'),
|
|
733
|
+
])
|
|
734
|
+
w.poll()
|
|
735
|
+
assert.equal(w.getState().source.text, '临时歌单')
|
|
736
|
+
} finally {
|
|
737
|
+
w.stop()
|
|
738
|
+
}
|
|
739
|
+
})
|
|
740
|
+
|
|
741
|
+
test('Watcher:每日推荐来源(dailyRecommend)正确抽取', () => {
|
|
742
|
+
const file = tmpFile()
|
|
743
|
+
writeLog(file, [
|
|
744
|
+
trackLine(
|
|
745
|
+
{ id: '6', name: '歌', artist: 'R', duration: 200_000, scene: 'dailyRecommend', href: '', text: '每日歌曲推荐' },
|
|
746
|
+
0,
|
|
747
|
+
),
|
|
748
|
+
])
|
|
749
|
+
const w = new ElogWatcher({ filePath: file })
|
|
750
|
+
w.start()
|
|
751
|
+
try {
|
|
752
|
+
assert.deepEqual(w.getState().source, {
|
|
753
|
+
scene: 'dailyRecommend',
|
|
754
|
+
href: '',
|
|
755
|
+
text: '每日歌曲推荐',
|
|
756
|
+
})
|
|
757
|
+
} finally {
|
|
758
|
+
w.stop()
|
|
759
|
+
}
|
|
760
|
+
})
|
|
761
|
+
|
|
609
762
|
test('Watcher:换歌时 nativePlay 先到、started 未到时仍报旧歌', () => {
|
|
610
763
|
const file = tmpFile()
|
|
611
764
|
writeLog(file, [
|