mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-10 10:11:46 +00:00
touchbar plugin - fixed code style
This commit is contained in:
@ -1,61 +1,76 @@
|
||||
const { TouchBar } = require('electron')
|
||||
const {
|
||||
TouchBar
|
||||
} = require('electron');
|
||||
const {
|
||||
TouchBarButton,
|
||||
TouchBarLabel,
|
||||
TouchBarSpacer,
|
||||
TouchBarSegmentedControl,
|
||||
TouchBarScrubber
|
||||
} = TouchBar
|
||||
TouchBarScrubber
|
||||
} = TouchBar;
|
||||
|
||||
// this selects the title
|
||||
const titleSelector = '.title.style-scope.ytmusic-player-bar'
|
||||
// This selects the title
|
||||
const titleSelector = '.title.style-scope.ytmusic-player-bar';
|
||||
|
||||
// these keys will be used to go backwards, pause and skip songs
|
||||
const keys = ['k','space','j']
|
||||
// These keys will be used to go backwards, pause and skip songs
|
||||
const keys = ['k', 'space', 'j'];
|
||||
|
||||
const presskey = (window, key) => {
|
||||
window.webContents.sendInputEvent({
|
||||
type: "keydown",
|
||||
keyCode: key
|
||||
})
|
||||
}
|
||||
window.webContents.sendInputEvent({
|
||||
type: 'keydown',
|
||||
keyCode: key
|
||||
});
|
||||
};
|
||||
|
||||
// grab the title using the selector
|
||||
const getTitle = (win) => {
|
||||
return win.webContents.executeJavaScript(
|
||||
`document.querySelector('` + titleSelector + `').innerText`
|
||||
).catch(e => {
|
||||
console.log(e)
|
||||
})
|
||||
}
|
||||
// Grab the title using the selector
|
||||
const getTitle = win => {
|
||||
return win.webContents.executeJavaScript(
|
||||
'document.querySelector(\'' + titleSelector + '\').innerText'
|
||||
).catch(error => {
|
||||
console.log(error);
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = (win) => {
|
||||
// songtitle label
|
||||
let songTitle = new TouchBarLabel({label: ''})
|
||||
module.exports = win => {
|
||||
// Songtitle label
|
||||
const songTitle = new TouchBarLabel({
|
||||
label: ''
|
||||
});
|
||||
|
||||
// the song control buttons (keys to press are in the same order)
|
||||
const buttons = new TouchBarSegmentedControl({
|
||||
mode: 'buttons',
|
||||
segments: [
|
||||
new TouchBarButton({label: '<'}),
|
||||
new TouchBarButton({label: '⏯️'}),
|
||||
new TouchBarButton({label: '>'})
|
||||
],
|
||||
change: (i) => presskey(win,keys[i])
|
||||
})
|
||||
// The song control buttons (keys to press are in the same order)
|
||||
const buttons = new TouchBarSegmentedControl({
|
||||
mode: 'buttons',
|
||||
segments: [
|
||||
new TouchBarButton({
|
||||
label: '<'
|
||||
}),
|
||||
new TouchBarButton({
|
||||
label: '⏯️'
|
||||
}),
|
||||
new TouchBarButton({
|
||||
label: '>'
|
||||
})
|
||||
],
|
||||
change: i => presskey(win, keys[i])
|
||||
});
|
||||
|
||||
// this is the touchbar object, this combines everything with proper layout
|
||||
const touchBar = new TouchBar({
|
||||
items: [
|
||||
new TouchBarScrubber({items: [songTitle], continuous: false}),
|
||||
new TouchBarSpacer({size:'flexible'}),
|
||||
buttons
|
||||
]
|
||||
})
|
||||
// This is the touchbar object, this combines everything with proper layout
|
||||
const touchBar = new TouchBar({
|
||||
items: [
|
||||
new TouchBarScrubber({
|
||||
items: [songTitle],
|
||||
continuous: false
|
||||
}),
|
||||
new TouchBarSpacer({
|
||||
size: 'flexible'
|
||||
}),
|
||||
buttons
|
||||
]
|
||||
});
|
||||
|
||||
// if the page title changes, update touchbar and song title
|
||||
win.on("page-title-updated", async () => {
|
||||
songTitle.label = await getTitle(win)
|
||||
win.setTouchBar(touchBar)
|
||||
})
|
||||
}
|
||||
// If the page title changes, update touchbar and song title
|
||||
win.on('page-title-updated', async () => {
|
||||
songTitle.label = await getTitle(win);
|
||||
win.setTouchBar(touchBar);
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user