touchbar plugin - fixed code style

This commit is contained in:
Sem Vissscher
2020-12-16 15:57:24 +01:00
parent c3e2c13808
commit 7473677477

View File

@ -1,61 +1,76 @@
const { TouchBar } = require('electron') const {
TouchBar
} = require('electron');
const { const {
TouchBarButton, TouchBarButton,
TouchBarLabel, TouchBarLabel,
TouchBarSpacer, TouchBarSpacer,
TouchBarSegmentedControl, TouchBarSegmentedControl,
TouchBarScrubber TouchBarScrubber
} = TouchBar } = TouchBar;
// this selects the title // This selects the title
const titleSelector = '.title.style-scope.ytmusic-player-bar' const titleSelector = '.title.style-scope.ytmusic-player-bar';
// 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'];
const presskey = (window, key) => { const presskey = (window, key) => {
window.webContents.sendInputEvent({ window.webContents.sendInputEvent({
type: "keydown", type: 'keydown',
keyCode: key keyCode: key
}) });
} };
// grab the title using the selector // Grab the title using the selector
const getTitle = (win) => { const getTitle = win => {
return win.webContents.executeJavaScript( return win.webContents.executeJavaScript(
`document.querySelector('` + titleSelector + `').innerText` 'document.querySelector(\'' + titleSelector + '\').innerText'
).catch(e => { ).catch(error => {
console.log(e) console.log(error);
}) });
} };
module.exports = (win) => { module.exports = win => {
// songtitle label // Songtitle label
let songTitle = new TouchBarLabel({label: ''}) const songTitle = new TouchBarLabel({
label: ''
});
// 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',
segments: [ segments: [
new TouchBarButton({label: '<'}), new TouchBarButton({
new TouchBarButton({label: '⏯️'}), label: '<'
new TouchBarButton({label: '>'}) }),
], new TouchBarButton({
change: (i) => presskey(win,keys[i]) label: '⏯️'
}) }),
new TouchBarButton({
label: '>'
})
],
change: i => presskey(win, keys[i])
});
// this is the touchbar object, this combines everything with proper layout // This is the touchbar object, this combines everything with proper layout
const touchBar = new TouchBar({ const touchBar = new TouchBar({
items: [ items: [
new TouchBarScrubber({items: [songTitle], continuous: false}), new TouchBarScrubber({
new TouchBarSpacer({size:'flexible'}), items: [songTitle],
buttons continuous: false
] }),
}) new TouchBarSpacer({
size: 'flexible'
}),
buttons
]
});
// 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 () => {
songTitle.label = await getTitle(win) songTitle.label = await getTitle(win);
win.setTouchBar(touchBar) win.setTouchBar(touchBar);
}) });
} };