This repository has been archived by the owner on Sep 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
140 lines (112 loc) Β· 3.67 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
/* jshint esversion: 9 */
'use strict';
const path = require('path');
const { app, BrowserWindow, Menu, session, globalShortcut } = require('electron');
const { autoUpdater } = require('electron-updater');
const { is } = require('electron-util');
const unhandled = require('electron-unhandled');
const debug = require('electron-debug');
const contextMenu = require('electron-context-menu');
const config = require('./config');
const menuEnable = config.get('menuEnable');
const menu = require('./menu');
const DiscordRPC = require('discord-rpc');
const { start } = require('repl');
unhandled();
contextMenu();
const debugEnable = config.get('debugEnable');
if (debugEnable) debug();
// Note: Must match `build.appId` in package.json
app.setAppUserModelId('com.sin.monkey-type-desktop');
// Uncomment this before publishing your first version.
// It's commented out as it throws an error if there are no published versions.
if (!is.development) {
const FOUR_HOURS = 1000 * 60 * 60 * 4;
setInterval(() => {
autoUpdater.checkForUpdates();
}, FOUR_HOURS);
autoUpdater.checkForUpdates();
}
// Prevent window from being garbage collected
let mainWindow;
const createMainWindow = async () => {
const win = new BrowserWindow({
title: app.name,
show: false,
width: 960,
height: 540,
icon: path.join(__dirname, '/static/icon.png')
});
win.on('ready-to-show', () => {
win.show();
});
win.on('closed', () => {
// Dereference the window
// For multiple windows store them in an array
mainWindow = undefined;
});
await win.loadURL('https://monkeytype.com/');
return win;
};
// Prevent multiple instances of the app
if (!app.requestSingleInstanceLock()) {
app.quit();
}
app.on('second-instance', () => {
if (mainWindow) {
if (mainWindow.isMinimized()) {
mainWindow.restore();
}
mainWindow.show();
}
});
app.on('window-all-closed', () => {
if (!is.macos) {
app.quit();
}
});
app.on('activate', async () => {
if (!mainWindow) {
mainWindow = await createMainWindow();
}
});
const clientId = config.get('discordRPC').clientId;
const rpc = new DiscordRPC.Client({ transport: 'ipc' });
const startTimestamp = new Date();
function setActivity() {
if (!rpc || !mainWindow) {
return;
}
// You can add your own presence application or presence data in the config.
// If you wanted to, you could try to get data from monkey type time left value and set this as the endTimestamp value.
rpc.setActivity({
details: config.get('discordRPC').RPC.details,
state: config.get('discordRPC').RPC.state,
largeImageKey: config.get('discordRPC').RPC.largeImageKey,
largeImageText: config.get('discordRPC').RPC.largeImageText,
// smallImageKey: config.get('discordRPC').RPC.smallImageKey,
// smallImageText: config.get('discordRPC').RPC.smallImageText,
startTimestamp: startTimestamp
});
}
rpc.on('ready', () => {
setActivity();
console.log('DiscordRPC: Initial set completed.');
setInterval(() => {
setActivity();
console.log('DiscordRPC: Refresh set completed.');
}, 15e3);
});
if (config.get('discordRPCEnable'))
rpc.login({ clientId }).catch(console.error);
(async () => {
await app.whenReady();
menuEnable ? Menu.setApplicationMenu(menu) : Menu.setApplicationMenu(null);
mainWindow = await createMainWindow();
// session.defaultSession.cookies.get({}).then(cookies => console.log(cookies));
// A more complete and custom config system will be implemented but at the moment you can just transfer your config cookie.
if (config.get('configCookieOverwrite').length > 50) {
const configCookie = { name: 'config', value: config.get('configCookieOverwrite') };
session.defaultSession.cookies.set(configCookie).then(() => {}, error => console.error(error));
}
})();