Skip to content

Commit c71cd7a

Browse files
committed
Refactored code
1 parent 3db8609 commit c71cd7a

File tree

3 files changed

+50
-43
lines changed

3 files changed

+50
-43
lines changed

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ function createWindow() {
5151
const { createPlayer } = require('./player.js')
5252
createPlayer(webContents, mainWindow)
5353
}
54-
webContents.executeJavaScript('const app = new YandexMusicPlayer()')
54+
webContents.executeJavaScript('const app = new YandexMusicPlayer(externalAPI)')
5555
})
5656
}
5757

src/player.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
function createPlayer(webContents, window) {
22
// eslint-disable-next-line global-require
3-
const Player = require('mpris-service')
3+
const MprisService = require('mpris-service')
44

5-
const player = Player({
5+
const mprisService = MprisService({
66
name: 'YandexMusic',
77
identity: 'YandexMusic media player',
88
supportedUriSchemes: ['file'],
@@ -11,37 +11,37 @@ function createPlayer(webContents, window) {
1111
})
1212

1313
// Events
14-
player.on('play', () => {
14+
mprisService.on('play', () => {
1515
webContents.send('player:play')
1616
})
1717

18-
player.on('pause', () => {
18+
mprisService.on('pause', () => {
1919
webContents.send('player:pause')
2020
})
2121

22-
player.on('playpause', () => {
22+
mprisService.on('playpause', () => {
2323
webContents.send('player:playPause')
2424
})
2525

26-
player.on('next', () => {
26+
mprisService.on('next', () => {
2727
webContents.send('player:next')
2828
})
2929

30-
player.on('previous', () => {
30+
mprisService.on('previous', () => {
3131
webContents.send('player:prev')
3232
})
33-
player.on('position', (data) => {
33+
mprisService.on('position', (data) => {
3434
webContents.send('player:setPosition', data.position)
3535
})
3636

37-
player.on('quit', () => {
37+
mprisService.on('quit', () => {
3838
process.exit()
3939
})
4040

4141
// Metadata
4242

4343
// return the position of your player
44-
player.getPosition = () => 0
44+
mprisService.getPosition = () => 0
4545

4646
// eslint-disable-next-line global-require
4747
const ipc = require('electron').ipcMain
@@ -50,16 +50,16 @@ function createPlayer(webContents, window) {
5050
const { trackId, title, artists, playbackStatus, length, seek, artUrl, album } = metadata
5151
window.setTitle(`${artists} - ${title}`)
5252
// @see http://www.freedesktop.org/wiki/Specifications/mpris-spec/metadata/
53-
player.metadata = {
54-
'mpris:trackid': player.objectPath(trackId),
53+
mprisService.metadata = {
54+
'mpris:trackid': mprisService.objectPath(trackId),
5555
'mpris:length': length,
5656
'mpris:artUrl': artUrl,
5757
'xesam:title': title,
5858
'xesam:album': album,
5959
'xesam:artist': artists
6060
}
61-
player.playbackStatus = playbackStatus
62-
player.seeked(seek)
61+
mprisService.playbackStatus = playbackStatus
62+
mprisService.seeked(seek)
6363
webContents.send('player:metadata', metadata)
6464
})
6565
}

src/preload.js

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* global document window externalAPI */
1+
/* global document window */
22

33
const ONE_M = 1000 * 1000
44

@@ -8,39 +8,45 @@ function q(selector) {
88
return document.querySelector(selector)
99
}
1010

11-
ipc.on('player:play', () => {
12-
externalAPI.togglePause('')
13-
})
11+
class YandexMusicPlayer {
12+
constructor(externalAPI) {
13+
this.externalAPI = externalAPI
14+
this.initCallbacks()
1415

15-
ipc.on('player:pause', () => {
16-
externalAPI.togglePause('PAUSE')
17-
})
16+
setInterval(() => {
17+
this.sendMetadata()
18+
this.hideAds()
19+
}, 1000)
20+
}
1821

19-
ipc.on('player:playPause', () => {
20-
externalAPI.togglePause()
21-
})
22+
initCallbacks() {
23+
ipc.on('player:play', () => {
24+
this.externalAPI.togglePause('')
25+
})
2226

23-
ipc.on('player:next', () => {
24-
externalAPI.next()
25-
})
27+
ipc.on('player:pause', () => {
28+
this.externalAPI.togglePause('PAUSE')
29+
})
2630

27-
ipc.on('player:prev', () => {
28-
externalAPI.prev()
29-
})
31+
ipc.on('player:playPause', () => {
32+
this.externalAPI.togglePause()
33+
})
3034

31-
ipc.on('player:setPosition', (ev, position) => {
32-
externalAPI.setPosition(position / ONE_M)
33-
})
35+
ipc.on('player:next', () => {
36+
this.externalAPI.next()
37+
})
3438

35-
class YandexMusicPlayer {
36-
constructor() {
37-
setInterval(() => {
38-
this.sendMetadata()
39-
this.hideAds()
40-
}, 1000)
39+
ipc.on('player:prev', () => {
40+
this.externalAPI.prev()
41+
})
42+
43+
ipc.on('player:setPosition', (ev, position) => {
44+
this.externalAPI.setPosition(position / ONE_M)
45+
})
4146
}
47+
4248
sendMetadata() {
43-
const currentTrack = externalAPI.getCurrentTrack()
49+
const currentTrack = this.externalAPI.getCurrentTrack()
4450

4551
if (!currentTrack) {
4652
return
@@ -53,17 +59,18 @@ class YandexMusicPlayer {
5359
artists: currentTrack.artists.map(a => a.title),
5460
artUrl: `https://${currentTrack.cover.replace('%%', '300x300')}`,
5561
length: currentTrack.duration * ONE_M,
56-
seek: externalAPI.getProgress().position * ONE_M
62+
seek: this.externalAPI.getProgress().position * ONE_M
5763
}
5864

59-
if (externalAPI.isPlaying()) {
65+
if (this.externalAPI.isPlaying()) {
6066
metadata.playbackStatus = 'Playing'
6167
} else {
6268
metadata.playbackStatus = 'Paused'
6369
}
6470

6571
ipc.send('player:metadata', metadata)
6672
}
73+
6774
hideAds() {
6875
const closeAdButton = q('.d-overhead__close button')
6976
if (closeAdButton) {

0 commit comments

Comments
 (0)