11const {
2- TouchBar
2+ TouchBar, nativeImage
33} = require ( 'electron' ) ;
44const {
55 TouchBarButton,
@@ -8,10 +8,14 @@ const {
88 TouchBarSegmentedControl,
99 TouchBarScrubber
1010} = TouchBar ;
11+ const fetch = require ( 'node-fetch' ) ;
1112
12- // This selects the title
13+ // This selects the song title
1314const titleSelector = '.title.style-scope.ytmusic-player-bar' ;
1415
16+ // This selects the song image
17+ const imageSelector = '#layout > ytmusic-player-bar > div.middle-controls.style-scope.ytmusic-player-bar > img' ;
18+
1519// These keys will be used to go backwards, pause and skip songs
1620const keys = [ 'k' , 'space' , 'j' ] ;
1721
@@ -31,12 +35,24 @@ const getTitle = win => {
3135 } ) ;
3236} ;
3337
38+ // Grab the image src using the selector
39+ const getImage = win => {
40+ return win . webContents . executeJavaScript (
41+ 'document.querySelector(\'' + imageSelector + '\').src'
42+ ) . catch ( error => {
43+ console . log ( error ) ;
44+ } ) ;
45+ } ;
46+
3447module . exports = win => {
3548 // Songtitle label
3649 const songTitle = new TouchBarLabel ( {
3750 label : ''
3851 } ) ;
3952
53+ // This will store the song image once available
54+ const songImage = { } ;
55+
4056 // The song control buttons (keys to press are in the same order)
4157 const buttons = new TouchBarSegmentedControl ( {
4258 mode : 'buttons' ,
@@ -58,7 +74,7 @@ module.exports = win => {
5874 const touchBar = new TouchBar ( {
5975 items : [
6076 new TouchBarScrubber ( {
61- items : [ songTitle ] ,
77+ items : [ songImage , songTitle ] ,
6278 continuous : false
6379 } ) ,
6480 new TouchBarSpacer ( {
@@ -70,7 +86,19 @@ module.exports = win => {
7086
7187 // If the page title changes, update touchbar and song title
7288 win . on ( 'page-title-updated' , async ( ) => {
89+ // Set the song title
7390 songTitle . label = await getTitle ( win ) ;
91+
92+ // Get image source
93+ const imageSrc = await getImage ( win ) ;
94+
95+ // Fetch and set song image
96+ await fetch ( imageSrc )
97+ . then ( response => response . buffer ( ) )
98+ . then ( data => {
99+ songImage . icon = nativeImage . createFromBuffer ( data ) . resize ( { height : 23 } ) ;
100+ } ) ;
101+
74102 win . setTouchBar ( touchBar ) ;
75103 } ) ;
76104} ;
0 commit comments