|
| 1 | +const { TouchBar } = require('electron') |
| 2 | +const { |
| 3 | + TouchBarButton, |
| 4 | + TouchBarLabel, |
| 5 | + TouchBarSpacer, |
| 6 | + TouchBarSegmentedControl, |
| 7 | + TouchBarScrubber |
| 8 | +} = TouchBar |
| 9 | + |
| 10 | +// this selects the title |
| 11 | +const titleSelector = '.title.style-scope.ytmusic-player-bar' |
| 12 | + |
| 13 | +// these keys will be used to go backwards, pause and skip songs |
| 14 | +const keys = ['k','space','j'] |
| 15 | + |
| 16 | +const presskey = (window, key) => { |
| 17 | + window.webContents.sendInputEvent({ |
| 18 | + type: "keydown", |
| 19 | + keyCode: key |
| 20 | + }) |
| 21 | +} |
| 22 | + |
| 23 | +// grab the title using the selector |
| 24 | +const getTitle = (win) => { |
| 25 | + return win.webContents.executeJavaScript( |
| 26 | + `document.querySelector('` + titleSelector + `').innerText` |
| 27 | + ).catch(e => { |
| 28 | + console.log(e) |
| 29 | + }) |
| 30 | +} |
| 31 | + |
| 32 | +module.exports = (win) => { |
| 33 | + // songtitle label |
| 34 | + let songTitle = new TouchBarLabel({label: ''}) |
| 35 | + |
| 36 | + // the song control buttons (keys to press are in the same order) |
| 37 | + const buttons = new TouchBarSegmentedControl({ |
| 38 | + mode: 'buttons', |
| 39 | + segments: [ |
| 40 | + new TouchBarButton({label: '<'}), |
| 41 | + new TouchBarButton({label: '⏯️'}), |
| 42 | + new TouchBarButton({label: '>'}) |
| 43 | + ], |
| 44 | + change: (i) => presskey(win,keys[i]) |
| 45 | + }) |
| 46 | + |
| 47 | + // this is the touchbar object, this combines everything with proper layout |
| 48 | + const touchBar = new TouchBar({ |
| 49 | + items: [ |
| 50 | + new TouchBarScrubber({items: [songTitle], continuous: false}), |
| 51 | + new TouchBarSpacer({size:'flexible'}), |
| 52 | + buttons |
| 53 | + ] |
| 54 | + }) |
| 55 | + |
| 56 | + // if the page title changes, update touchbar and song title |
| 57 | + win.on("page-title-updated", async () => { |
| 58 | + songTitle.label = await getTitle(win) |
| 59 | + win.setTouchBar(touchBar) |
| 60 | + }) |
| 61 | +} |
0 commit comments