forked from simon-fraser/MusicTube-Player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
208 lines (196 loc) · 5.73 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
const { app, BrowserWindow, globalShortcut, Menu, ipcMain } = require('electron')
const windowStateKeeper = require('electron-window-state')
const notifier = require('node-notifier')
const path = require('path')
// variables
let winWidth = 440
let winHeight = 620
let loadingScreen
let mainWindow
let aboutScreen
let willQuitApp = false
let windowParams = {
backgroundColor: '#131313',
icon: path.join(__dirname, 'assets/musictube.ico'),
title: 'Loading...'
}
function createLoadingWindow () {
loadingScreen = new BrowserWindow(Object.assign(windowParams, {
frame: false,
parent: mainWindow,
width: winWidth,
height: winWidth
}))
loadingScreen.loadURL(`file://${__dirname}/loading.html`)
loadingScreen.on('closed', () => { loadingScreen = null })
// Show loader
loadingScreen.webContents.on('did-finish-load', () => {
loadingScreen.show()
})
}
function createWindow () {
let mainWindowState = windowStateKeeper({
defaultWidth: winWidth,
defaultHeight: winHeight
})
mainWindow = new BrowserWindow(Object.assign(windowParams, {
frame: true,
height: mainWindowState.height,
show: false,
width: mainWindowState.width,
x: mainWindowState.x,
y: mainWindowState.y
}))
mainWindow.loadURL(`https://music.youtube.com/`)
mainWindow.hide()
// Show main window and hide loader
mainWindow.webContents.on('did-finish-load', () => {
mainWindowState.manage(mainWindow)
mainWindow.show()
if (loadingScreen !== null) loadingScreen.close()
})
// Close behaviour
mainWindow.on('close', (e) => {
mainWindowState.saveState(mainWindow)
if (!willQuitApp) {
e.preventDefault()
mainWindow.hide()
}
})
mainWindow.on('closed', () => { mainWindow = null })
}
function createAboutWindow () {
aboutScreen = new BrowserWindow({
backgroundColor: '#131313',
frame: true,
icon: path.join(__dirname, 'assets/musictube.ico'),
title: 'About MusicTube Player',
height: 400,
width: 320
})
aboutScreen.loadURL(`file://${__dirname}/about.html`)
}
// Application ready to run
app.on('ready', () => {
createLoadingWindow()
createWindow()
globalShortcuts()
createMenu()
skipOverAdverts()
})
// triggered when clicked the dock icon (osx)
app.on('activate', () => {
mainWindow.show()
})
// triggered when quitting from dock icon (osx)
app.on('before-quit', () => {
willQuitApp = true
})
// Notification message process
ipcMain.on('notify', (event, obj) => {
notifier.notify({
title: `${obj.status} • MusicTube Player`,
message: `${obj.title}\n${obj.by}`,
icon: path.join(__dirname, 'assets/musictube.ico')
})
})
function globalShortcuts () {
// Play,Pause
globalShortcut.register('MediaPlayPause', () => {
mainWindow.webContents.executeJavaScript(`document.getElementsByClassName('play-pause-button')[0].click()`)
setTimeout(() => {
mainWindow.webContents.executeJavaScript(`
var ipcRenderer = require('electron').ipcRenderer
var status = (document.getElementsByClassName('play-pause-button')[0].title.includes('Pla'))? 'Paused' : 'Playing'
var value = {
status: status,
title: document.getElementsByClassName('ytmusic-player-bar title')[0].innerText,
by: document.getElementsByClassName('ytmusic-player-bar byline')[0].innerText,
}
ipcRenderer.send('notify', value)
`)
}, 500)
})
// Next
globalShortcut.register('MediaNextTrack', () => {
mainWindow.webContents.executeJavaScript(`document.getElementsByClassName('next-button')[0].click()`)
setTimeout(() => {
mainWindow.webContents.executeJavaScript(`
var ipcRenderer = require('electron').ipcRenderer
var value = {
status: "Now Playing",
title: document.getElementsByClassName('ytmusic-player-bar title')[0].innerText,
by: document.getElementsByClassName('ytmusic-player-bar byline')[0].innerText,
}
ipcRenderer.send('notify', value)
`)
}, 500)
})
// Previous
globalShortcut.register('MediaPreviousTrack', () => {
mainWindow.webContents.executeJavaScript(`document.getElementsByClassName('previous-button')[0].click()`)
setTimeout(() => {
mainWindow.webContents.executeJavaScript(`
var ipcRenderer = require('electron').ipcRenderer
var value = {
status: "Now Playing",
title: document.getElementsByClassName('ytmusic-player-bar title')[0].innerText,
by: document.getElementsByClassName('ytmusic-player-bar byline')[0].innerText,
}
ipcRenderer.send('notify', value)
`)
}, 500)
})
}
function skipOverAdverts () {
setInterval(() => {
if (mainWindow) {
mainWindow.webContents.executeJavaScript(`
var skip = document.getElementsByClassName('videoAdUiSkipButton')[0]
if(typeof skip !== "undefined") { skip.click() }
`)
}
}, 1000)
}
function createMenu () {
Menu.setApplicationMenu(Menu.buildFromTemplate(
[{
label: 'Application',
submenu: [
{
label: 'About',
accelerator: 'CmdOrCtrl+I',
click () {
createAboutWindow()
}
},
{
label: 'Refresh',
accelerator: 'CmdOrCtrl+R',
role: 'forceReload'
},
{
label: 'New Window',
accelerator: 'CmdOrCtrl+N',
click () {
createLoadingWindow()
createWindow()
}
},
{
label: 'Show Developer Tools',
accelerator: 'CmdOrCtrl+Shift+I',
role: 'toggleDevTools'
},
{
label: 'Quit',
accelerator: 'CmdOrCtrl+Q',
click () {
willQuitApp = true
app.quit()
}
}
]
}]
))
}