-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
107 lines (91 loc) · 2.39 KB
/
main.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
const { app, BrowserWindow, Menu } = require('electron/main')
const { Client } = require('discord-rpc')
const ytData = require('./scripts/yt-data')
const {
winSettings,
winModalSetting,
} = require('./scripts/utils/windowSettings')
const logTimeStamp = require('./scripts/utils/timestamp')
const rpc = new Client({ transport: 'ipc' })
const clientId = '623558135994384437'
let win, winModal
const connectorRPC = (attempt = 0) => {
if (attempt >= 5) {
logTimeStamp('Failed to connect to Discord RPC after multiple attempts')
win?.webContents.executeJavaScript(connectionStatus)
return
}
attempt += 1
rpc
.login({ clientId })
.catch(() => setTimeout(() => connectorRPC(attempt), 10e4))
}
const checkYtData = async () => {
if (!rpc || !win) return
try {
const data = await ytData(win)
// console.log(data);
rpc.setActivity(data)
} catch (error) {
console.error('Error retrieving YouTube data:', error)
}
}
const createWindow = () => {
win = new BrowserWindow(winSettings)
winModal = new BrowserWindow(winModalSetting)
winModal.hide()
win.webContents.openDevTools()
win.webContents.on('will-navigate', (e, url) => {
const uri = new URL(url).host
if (uri === 'accounts.google.com') {
e.preventDefault()
winModal.show()
winModal.loadURL(url)
}
})
win.on('closed', () => {
logTimeStamp('Exiting the Electron window')
app.quit()
})
winModal.on('closed', () => {
app.quit()
})
win.openDevTools()
Menu.setApplicationMenu(null)
win.loadURL('https://www.youtube.com')
connectorRPC()
}
rpc.on('ready', () => {
logTimeStamp('Connected to Discord RPC')
checkYtData()
setInterval(checkYtData, 5e3)
})
app.on('ready', () => {
logTimeStamp('Creating Electron window')
createWindow()
})
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
app.on('second-instance', () => {
if (win) {
if (win.isMinimized()) {
logTimeStamp('Secondary window opened for sign-up')
win.restore()
return
}
win.focus()
}
})
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
const gotSingleInstanceLock = app.requestSingleInstanceLock()
if (!gotSingleInstanceLock) {
logTimeStamp('Another instance of the application is already running')
app.quit()
} else {
logTimeStamp('Single instance lock acquired')
}