-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
216 lines (188 loc) · 6.59 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
209
210
211
212
213
214
215
216
const { app, BrowserWindow } = require('electron');
const path = require('path');
const RPC = require('discord-rpc');
const axios = require('axios');
const _ = require('lodash');
// Définir le Client ID Discord
const clientId = '1246126898023497878';
// Créer un client RPC
const rpc = new RPC.Client({ transport: 'ipc' });
const extractHashNumbers = (url) => {
const regex = /#(\d+)(?:\?.*)?$/;
const match = url.match(regex);
return match ? Number(match[1]) : null;
};
function fetchEMAPIProject(id) {
const url = 'https://projects.mubi.tech/api/projects/search?project';
return axios.get(url)
.then(response => {
const data = response.data;
// Trouver l'objet avec l'id désiré
const project = _.find(data.projects, { id: id });
if (project) {
console.log('Project found:', project);
return project; // Renvoyer le projet trouvé
} else {
console.log('Project not found');
return null; // Renvoyer null si le projet n'est pas trouvé
}
})
.catch(error => {
console.error('Error fetching data:', error);
return null; // Renvoyer null en cas d'erreur
});
}
// Définir les activités
let startTimestamp = new Date();
const setActivity = (details, state) => {
rpc.setActivity({
details: details,
state: state,
startTimestamp: startTimestamp,
largeImageKey: 'icon',
largeImageText: 'ElectraMod',
instance: false,
});
};
// Event 'ready' du client RPC
rpc.on('ready', () => {
console.log('Discord RPC is now active');
setActivity('Starting', 'Initializing');
});
// Connexion à Discord
rpc.login({ clientId }).catch(console.error);
// Définir updateActivity dans le scope global
let previousURL = '';
const updateActivity = async (win) => {
if (!win.isDestroyed()) {
const currentURL = win.webContents.getURL();
if (currentURL !== previousURL) {
console.log('changed page!', currentURL);
if (currentURL.includes('/scratch-gui/build/index.html') || currentURL.includes('electramod.vercel.app/')) {
if (currentURL.includes('#')) {
if (!currentURL.includes('#0')) {
const projectId = extractHashNumbers(currentURL);
const project = await fetchEMAPIProject(projectId);
if (project) {
setActivity(`Plays ${project.name}`, `by ${project.owner}`);
} else {
console.log('Project not found');
setActivity('in Editor', 'Code Editor');
}
} else {
setActivity('in Editor', 'Code Editor');
}
} else {
setActivity('in Editor', 'Code Editor');
}
} else if (currentURL.includes('/scratch-gui/build/addons.html') || currentURL.includes('electramod.vercel.app/addons.html')) {
setActivity('in Addons');
} else if (currentURL.includes('/packager/dist/') || currentURL.includes('electramod-packager.vercel.app/')) {
setActivity('in Packager');
} else if (currentURL.includes('electramod-home.vercel.app')) {
if (currentURL.includes('profile')) {
if (currentURL.includes('profile?user=')) {
const url = new URL(currentURL);
const params = new URLSearchParams(url.search);
const user = params.get('user');
setActivity(`looking at ${user}'s profile`);
} else {
setActivity('looking at a profile');
}
} else {
setActivity('in Home');
}
}
previousURL = currentURL;
}
}
};
// Fonction pour configurer une fenêtre
const configureWindow = (win) => {
win.setMenuBarVisibility(false);
win.webContents.on('page-title-updated', (event, title) => {
let newTitle = title;
if (title.startsWith('ElectraMod -')) {
newTitle = title.replace('ElectraMod -', 'ElectraMod Desktop -');
} else if (title.endsWith('- ElectraMod')) {
newTitle = title.replace('- ElectraMod', '- ElectraMod Desktop');
} else if (title.startsWith('PenguinMod -')) {
newTitle = title.replace('PenguinMod -', 'ElectraMod Desktop -');
} else if (title.endsWith('- PenguinMod')) {
newTitle = title.replace('- PenguinMod', '- ElectraMod Desktop');
}
if (newTitle !== title) {
event.preventDefault(); // Prevent default title update
win.setTitle(newTitle); // Update with the new title
}
});
const checkURL = () => {
const currentURL = win.webContents.getURL();
if (currentURL && currentURL.includes('electramod-packager.vercel.app')) {
const url = new URL(currentURL);
const newURL = 'file://' + path.join(__dirname, 'packager', 'dist', 'index.html');
const finalURL = newURL + url.search + url.hash;
win.loadURL(finalURL);
} else if (currentURL && currentURL.includes('electramod.vercel.app')) {
const url = new URL(currentURL);
const newURL = 'file://' + path.join(__dirname, 'scratch-gui', 'build', 'index.html');
const finalURL = newURL + url.search + url.hash;
win.loadURL(finalURL);
}
};
// Vérifier l'URL de la fenêtre toutes les secondes
const intervalId = setInterval(checkURL, 1000);
// Arrêter l'intervalle lorsque la fenêtre est fermée
win.on('closed', () => {
clearInterval(intervalId);
});
win.webContents.on('did-navigate-in-page', () => {
updateActivity(win);
});
win.on('focus', () => {
updateActivity(win);
});
};
// Fonction pour créer la fenêtre principale
const createWindow = () => {
const mainWindow = new BrowserWindow({
width: 960,
height: 600,
icon: path.join(__dirname, 'assets', 'icon', 'icon.png'),
webPreferences: {
devTools: true,
}
});
mainWindow.loadFile('scratch-gui/build/index.html');
configureWindow(mainWindow);
// Gérer les nouvelles fenêtres (popups, etc.)
mainWindow.webContents.setWindowOpenHandler(({ url }) => {
createPopupWindow(url);
return { action: 'deny' }; // Empêcher le comportement par défaut (ouverture dans une nouvelle fenêtre de navigateur)
});
};
// Fonction pour créer une fenêtre popup
const createPopupWindow = (url) => {
const popupWindow = new BrowserWindow({
width: 800,
height: 600,
icon: path.join(__dirname, 'assets', 'icon', 'icon.png'),
webPreferences: {
devTools: true,
}
});
popupWindow.loadURL(url);
configureWindow(popupWindow);
};
// Evénements du cycle de vie de l'application
app.whenReady().then(() => {
createWindow();
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});