mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-11 10:31:47 +00:00
Added song image
This commit is contained in:
@ -1,5 +1,5 @@
|
|||||||
const {
|
const {
|
||||||
TouchBar
|
TouchBar, nativeImage
|
||||||
} = require('electron');
|
} = require('electron');
|
||||||
const {
|
const {
|
||||||
TouchBarButton,
|
TouchBarButton,
|
||||||
@ -8,10 +8,14 @@ const {
|
|||||||
TouchBarSegmentedControl,
|
TouchBarSegmentedControl,
|
||||||
TouchBarScrubber
|
TouchBarScrubber
|
||||||
} = TouchBar;
|
} = TouchBar;
|
||||||
|
const fetch = require('node-fetch');
|
||||||
|
|
||||||
// This selects the title
|
// This selects the song title
|
||||||
const titleSelector = '.title.style-scope.ytmusic-player-bar';
|
const titleSelector = '.title.style-scope.ytmusic-player-bar';
|
||||||
|
|
||||||
|
// This selects the song image
|
||||||
|
const imageSelector = '#layout > ytmusic-player-bar > div.middle-controls.style-scope.ytmusic-player-bar > img';
|
||||||
|
|
||||||
// These keys will be used to go backwards, pause and skip songs
|
// These keys will be used to go backwards, pause and skip songs
|
||||||
const keys = ['k', 'space', 'j'];
|
const keys = ['k', 'space', 'j'];
|
||||||
|
|
||||||
@ -31,12 +35,24 @@ const getTitle = win => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Grab the image src using the selector
|
||||||
|
const getImage = win => {
|
||||||
|
return win.webContents.executeJavaScript(
|
||||||
|
'document.querySelector(\'' + imageSelector + '\').src'
|
||||||
|
).catch(error => {
|
||||||
|
console.log(error);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = win => {
|
module.exports = win => {
|
||||||
// Songtitle label
|
// Songtitle label
|
||||||
const songTitle = new TouchBarLabel({
|
const songTitle = new TouchBarLabel({
|
||||||
label: ''
|
label: ''
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// This will store the song image once available
|
||||||
|
const songImage = {};
|
||||||
|
|
||||||
// The song control buttons (keys to press are in the same order)
|
// The song control buttons (keys to press are in the same order)
|
||||||
const buttons = new TouchBarSegmentedControl({
|
const buttons = new TouchBarSegmentedControl({
|
||||||
mode: 'buttons',
|
mode: 'buttons',
|
||||||
@ -58,7 +74,7 @@ module.exports = win => {
|
|||||||
const touchBar = new TouchBar({
|
const touchBar = new TouchBar({
|
||||||
items: [
|
items: [
|
||||||
new TouchBarScrubber({
|
new TouchBarScrubber({
|
||||||
items: [songTitle],
|
items: [songImage, songTitle],
|
||||||
continuous: false
|
continuous: false
|
||||||
}),
|
}),
|
||||||
new TouchBarSpacer({
|
new TouchBarSpacer({
|
||||||
@ -70,7 +86,19 @@ module.exports = win => {
|
|||||||
|
|
||||||
// If the page title changes, update touchbar and song title
|
// If the page title changes, update touchbar and song title
|
||||||
win.on('page-title-updated', async () => {
|
win.on('page-title-updated', async () => {
|
||||||
|
// Set the song title
|
||||||
songTitle.label = await getTitle(win);
|
songTitle.label = await getTitle(win);
|
||||||
|
|
||||||
|
// Get image source
|
||||||
|
const imageSrc = await getImage(win);
|
||||||
|
|
||||||
|
// Fetch and set song image
|
||||||
|
await fetch(imageSrc)
|
||||||
|
.then(response => response.buffer())
|
||||||
|
.then(data => {
|
||||||
|
songImage.icon = nativeImage.createFromBuffer(data).resize({height: 23});
|
||||||
|
});
|
||||||
|
|
||||||
win.setTouchBar(touchBar);
|
win.setTouchBar(touchBar);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user