-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
95 lines (79 loc) · 2.5 KB
/
main.js
File metadata and controls
95 lines (79 loc) · 2.5 KB
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
let fs = require('fs')
, config = require('./config.json')
, saveConfig = () => {fs.writeFile('./config.json', JSON.stringify(config), 'utf8', () => {})}
, win
;
const DiscordRPC = require('discord-rpc')
, rpc = new DiscordRPC.Client({transport: 'ipc'})
, {app, BrowserWindow, Menu, remote, globalShortcut } = require('electron')
;
DiscordRPC.register(config.clientId);
const menuTemplate = [
{
label: "Interface",
submenu: [
{role: "Reload"},
{role: "toggleDevTools"},
{label: 'Enable RPC', type: 'checkbox', checked: config.state ? true : false, click: () => {config.state = config.state ? false : true; saveConfig()}}
]
}
];
app.on('ready', () => {
// Create the browser window.
win = new BrowserWindow({
width: 800,
height: 700,
show: false,
webPreferences: {
nodeIntegration: false
},
autoHideMenuBar: true
});
win.webContents.on('new-window', function(e, url) {
e.preventDefault();
require('electron').shell.openExternal(url);
});
win.setMinimumSize(300, 300);
win.setSize(800, 700);
win.setResizable(true);
// Menu.setApplicationMenu(Menu.buildFromTemplate(menuTemplate));
win.once('ready-to-show', () => {win.show()});
globalShortcut.register('MediaPlayPause', () => {
win.webContents.executeJavaScript('togglePlay()');
console.log('MediaPlayPause is pressed')
return;
});
console.log('MediaPlayPause', globalShortcut.isRegistered('MediaPlayPause'));
win.on('closed', () => {win = null;});
win.loadURL(`https://${config.source}`);
});
app.on('window-all-closed', () => {globalShortcut.unregisterAll(); rpc.destroy(); app.quit();})
function setActivity() {
if (!rpc || !win || !config.state) return;
yaderi();
}
async function yaderi() {
let data = await win.webContents.executeJavaScript('document.getElementById("songtitle").innerHTML');
let state = await win.webContents.executeJavaScript('isPlaying()');
let artist = data.split(' - ')[0]
let title = data.split(' - ')[1]
rpc.setActivity({
details: artist,
state: title,
largeImageKey: 'yaderi',
largeImageText: 'HyperYaderi.ru',
smallImageKey: state ? 'true' : 'false',
smallImageText: state ? 'Слушает' : 'На паузе'
})
}
rpc.on('ready', () => {
console.log('Authed for user', rpc.user.username);
rpc.setActivity({
details: 'Запускается...',
state: 'Загрузка плеера',
smallImageKey: 'pause',
smallImageText: 'На паузе'
})
setInterval(() => setActivity(), 5e3);
});
rpc.login({clientId: config.clientId}).catch(console.error);