Skip to content

Commit d68d73e

Browse files
authored
Merge pull request #101 from semvis123/master
Added Touch Bar plugin
2 parents 2de27d2 + f71e0e9 commit d68d73e

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed

plugins/touchbar/back.js

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
const {
2+
TouchBar, nativeImage
3+
} = require('electron');
4+
const {
5+
TouchBarButton,
6+
TouchBarLabel,
7+
TouchBarSpacer,
8+
TouchBarSegmentedControl,
9+
TouchBarScrubber
10+
} = TouchBar;
11+
const fetch = require('node-fetch');
12+
13+
// This selects the song title
14+
const titleSelector = '.title.style-scope.ytmusic-player-bar';
15+
16+
// This selects the song image
17+
const imageSelector = '#layout > ytmusic-player-bar > div.middle-controls.style-scope.ytmusic-player-bar > img';
18+
19+
// These keys will be used to go backwards, pause, skip songs, like songs, dislike songs
20+
const keys = ['k', 'space', 'j', '_', '+'];
21+
22+
const presskey = (window, key) => {
23+
window.webContents.sendInputEvent({
24+
type: 'keydown',
25+
keyCode: key
26+
});
27+
};
28+
29+
// Grab the title using the selector
30+
const getTitle = win => {
31+
return win.webContents.executeJavaScript(
32+
'document.querySelector(\'' + titleSelector + '\').innerText'
33+
).catch(error => {
34+
console.log(error);
35+
});
36+
};
37+
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+
47+
module.exports = win => {
48+
// Songtitle label
49+
const songTitle = new TouchBarLabel({
50+
label: ''
51+
});
52+
53+
// This will store the song image once available
54+
const songImage = {};
55+
56+
// The song control buttons (keys to press are in the same order)
57+
const buttons = new TouchBarSegmentedControl({
58+
mode: 'buttons',
59+
segments: [
60+
new TouchBarButton({
61+
label: '⏮'
62+
}),
63+
new TouchBarButton({
64+
label: '⏯️'
65+
}),
66+
new TouchBarButton({
67+
label: '⏭'
68+
}),
69+
new TouchBarButton({
70+
label: '👎'
71+
}),
72+
new TouchBarButton({
73+
label: '👍'
74+
})
75+
],
76+
change: i => presskey(win, keys[i])
77+
});
78+
79+
// This is the touchbar object, this combines everything with proper layout
80+
const touchBar = new TouchBar({
81+
items: [
82+
new TouchBarScrubber({
83+
items: [songImage, songTitle],
84+
continuous: false
85+
}),
86+
new TouchBarSpacer({
87+
size: 'flexible'
88+
}),
89+
buttons
90+
]
91+
});
92+
93+
// If the page title changes, update touchbar and song title
94+
win.on('page-title-updated', async () => {
95+
// Set the song title
96+
songTitle.label = await getTitle(win);
97+
98+
// Get image source
99+
const imageSrc = await getImage(win);
100+
101+
// Fetch and set song image
102+
await fetch(imageSrc)
103+
.then(response => response.buffer())
104+
.then(data => {
105+
songImage.icon = nativeImage.createFromBuffer(data).resize({height: 23});
106+
});
107+
108+
win.setTouchBar(touchBar);
109+
});
110+
};

0 commit comments

Comments
 (0)