-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlyrics.user.js
142 lines (135 loc) · 5.5 KB
/
lyrics.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// ==UserScript==
// @name 网易云音乐歌词推送
// @namespace https://youthlin.com/
// @match *://music.163.com/*
// @noframes true
// @grant GM.xmlHttpRequest
// @version 1.0
// @author Youth.霖
// @description 需要保持播放列表打开状态。创建于 2023/5/19 16:41:26
// ==/UserScript==
(function () {
'use strict';
// https://github.com/youthlin/swiftbarlyrics
setTimeout(() => {// 延迟 1s 启动
console.log('网易云音乐歌词推送启动中...')
function makeSureLyricsShow() {
if (!document.querySelector('#g_playlist')) {// 确保播放列表开启
document.querySelector('[data-action="panel"]')?.click();
}
}
setInterval(makeSureLyricsShow, 1000)
function send(data) {
if (data.lyrics) {// 推送当前歌词
GM.xmlHttpRequest({ // 需要 @grant GM.xmlHttpRequest 授权
method: "GET",
url: `http://localhost:51917/set?current=${encodeURI(data.lyrics)}`,
});
}
if (data.title && data.by) {// 推送当前播放曲目 - 艺术家
GM.xmlHttpRequest({ // 需要 @grant GM.xmlHttpRequest 授权
method: "GET",
url: `http://localhost:51917/set?title=${encodeURI(data.title)}&by=${encodeURI(data.by)}`,
});
}
}
function addTitleObserver() {// 监听播放曲目变动
// 当前播放
const title = document.querySelector('.words .name')?.innerText
const by = document.querySelector('.words .by')?.innerText
if (title && by) {
console.log('当前播放', title, by)
send({ title, by })
}
// 监听后续变动
new MutationObserver(records => {
for (let record of records) {
let title = ''
let by = ''
for (let node of record.addedNodes) {
if (node.classList?.contains('name')) {
title = node.innerText
}
if (node.classList?.contains('by')) {
by = node.innerText
}
}
console.log(`检测到切歌 ${title} - ${by}`)
send({ title, by })
}
}).observe(document.querySelector('.words'), {
childList: true,
characterData: true,// node 的文本内容变动
})
}
let foundLyricsNode = false
function addLyricsObserver() {// 监听歌词变动
if (foundLyricsNode) return
const node = document.querySelector('.listlyric');
if (node) {
let last = '';
new MutationObserver(records => {
for (let record of records) {
if (record.target.classList.contains('z-sel')) {
const lyrics = record.target.innerText
if (lyrics != last) {
last = lyrics
console.log(lyrics)
send({ lyrics })
}
break
}
}
}).observe(node, {
childList: true, // 直接子节点
subtree: true, // 所有后代
attributes: true, // 属性
attributeFilter: ['class'],
})
foundLyricsNode = true
addTitleObserver()
console.log('歌词监听节点:', node)
} else {
console.log('歌词监听节点未找到 1s 后重试')
setTimeout(addLyricsObserver, 1000)// 1s 后重试
}
}
addLyricsObserver()
// 切割控制
function handleAction() {
GM.xmlHttpRequest({
method: "GET",
url: `http://localhost:51917/action/get`,
onerror: r => {
setTimeout(handleAction, 1000)
},
onloadend: r => {
const action = r.response.trim()
console.log(action)
switch (action) {
case 'nop': break;
case 'prev':
document.querySelector('a[data-action="prev"]')?.click()
break;
case 'toggle':
let node = document.querySelector('a[data-action="play"]')
if (!node) {
node = document.querySelector('a[data-action="pause"]')
}
console.log('toggle node', node)
if (node) {
node.click()
}
break;
case 'next':
document.querySelector('a[data-action="next"]')?.click()
break;
}
setTimeout(handleAction, 0)
},
});
}
handleAction()
console.log('网易云音乐歌词推送已启动')
}, 1000);
})()