forked from minein4/BetterTaikoWeb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
134 lines (114 loc) · 3.47 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
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
const { app, BrowserWindow, ipcMain, nativeImage, globalShortcut} = require('electron');
const axios = require('axios');
const path = require('path');
const RPC = require('discord-rpc');
const clientId = '966159870833332264';
const rpc = new RPC.Client({ transport: 'ipc' });
const faviconUrl = 'https://play-lh.googleusercontent.com/Cv08QVGL1gd9aiMU9-rv71tCn-mM_rlXINYTNzjd5PYM7tVqxHm_2ooKMd_Kxn_6lBk';
async function fetchFavicon(url) {
try {
const response = await axios.get(url, { responseType: 'arraybuffer' });
return Buffer.from(response.data, 'binary').toString('base64');
} catch (error) {
console.error('Error fetching favicon:', error);
return null;
}
}
async function fetchTjaFile(url) {
try {
const response = await axios.get(url, { responseType: 'text' });
return response.data;
} catch (error) {
console.error('Error fetching TJA file:', error);
return null;
}
}
function extractSongNameFromTja(tjaContent) {
const titleLine = tjaContent.split('\n').find(line => line.startsWith('TITLE:'));
if (titleLine) {
return titleLine.split(':')[1].trim();
}
return null;
}
let mainWindow;
let icon;
async function createWindow() {
const faviconBase64 = await fetchFavicon(faviconUrl);
icon = faviconBase64 ? nativeImage.createFromDataURL(`data:image/png;base64,${faviconBase64}`) : null;
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
nodeIntegration: false,
contextIsolation: true,
},
autoHideMenuBar: true,
icon: icon
});
mainWindow.loadFile('index.html');
mainWindow.webContents.on('did-finish-load', () => {
globalShortcut.register('F5', () => {
mainWindow.reload();
});
globalShortcut.register('CommandOrControl+R', () => {
mainWindow.reload();
});
});
}
app.whenReady().then(createWindow);
ipcMain.on('load-site', async (event, url) => {
mainWindow.loadURL(url);
rpc.on('ready', () => {
rpc.setActivity({
details: 'Hitting all the notes in taiko',
state: 'Main Menu',
startTimestamp: Date.now(),
largeImageKey: 'logo',
largeImageText: 'Ogey',
smallImageKey: 'logo',
smallImageText: 'Rrat',
instance: false,
});
});
rpc.login({ clientId }).catch(console.error);
mainWindow.webContents.session.webRequest.onCompleted(async (details) => {
if (details.url.endsWith('.tja')) {
console.log(`Detected TJA file request: ${details.url}`);
const tjaContent = await fetchTjaFile(details.url);
if (tjaContent) {
const songName = extractSongNameFromTja(tjaContent);
if (songName) {
console.log(`Playing song: ${songName}`);
rpc.setActivity({
details: 'Hitting all the notes in taiko',
state: `Playing ${songName}`,
startTimestamp: Date.now(),
largeImageKey: 'logo',
largeImageText: 'Ogey',
smallImageKey: 'logo',
smallImageText: 'Rrat',
instance: false,
});
} else {
console.log('Song name not found in TJA file.');
}
} else {
console.log('Failed to fetch TJA file.');
}
}
});
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
app.on('before-quit', () => {
rpc.destroy();
});