-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
565 lines (480 loc) · 16.9 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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
const { app, BrowserWindow, ipcMain, protocol } = require('electron');
const path = require('path');
const fs = require('fs');
const express = require('express');
const ejs = require('ejs');
const expressApp = express();
const RPC = require('discord-rpc');
let mainWindow;
let server;
// SetScript klasörü yolları
const documentsPath = app.getPath('documents');
const setScriptPath = path.join(documentsPath, 'SetScript');
const savesPath = path.join(setScriptPath, 'Saves');
const offlinePagesPath = path.join(setScriptPath, 'OfflinePages');
const settingsPath = path.join(setScriptPath, 'settings.json');
// Varsayılan ayarlar
const defaultSettings = {
isFullscreen: false,
isAlwaysOnTop: false
};
// Ayarları yükle
function loadSettings() {
try {
if (fs.existsSync(settingsPath)) {
const settings = JSON.parse(fs.readFileSync(settingsPath, 'utf8'));
console.log('Ayarlar yuklendi:', settings);
return { ...defaultSettings, ...settings };
}
} catch (error) {
console.error('Ayarlar yuklenirken hata:', error);
}
return defaultSettings;
}
// Ayarları kaydet
function saveSettings(settings) {
try {
if (!fs.existsSync(setScriptPath)) {
fs.mkdirSync(setScriptPath, { recursive: true });
}
fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 2));
console.log('Ayarlar kaydedildi:', settings);
return true;
} catch (error) {
console.error('Ayarlar kaydedilirken hata:', error);
return false;
}
}
// Ayarları uygula
function applySettings(settings) {
if (!mainWindow) return;
console.log('Ayarlar uygulaniyor:', settings);
// Tam ekran ayarı
mainWindow.setFullScreen(settings.isFullscreen);
// Her zaman üstte ayarı
mainWindow.setAlwaysOnTop(settings.isAlwaysOnTop);
// Ayarları kaydet
saveSettings(settings);
}
// Discord Presence
const clientId = '1334819590710231072';
DiscordRPC();
function DiscordRPC() {
const rpc = new RPC.Client({ transport: 'ipc' });
rpc.on('ready', () => {
console.log('Discord RPC baglaniyor!');
rpc.setActivity({
details: 'Türkiyenin En iyi yazılım Platformu',
state: 'Göz atıyor...',
startTimestamp: Date.now(),
largeImageKey: 'large',
largeImageText: 'SetScript',
smallImageKey: 'icon',
smallImageText: 'Çalışıyor',
instance: false
});
});
rpc.login({ clientId }).catch(console.error);
}
async function createWindow() {
// Klasörleri oluştur
if (!fs.existsSync(setScriptPath)) {
fs.mkdirSync(setScriptPath, { recursive: true });
}
if (!fs.existsSync(savesPath)) {
fs.mkdirSync(savesPath, { recursive: true });
}
if (!fs.existsSync(offlinePagesPath)) {
fs.mkdirSync(offlinePagesPath, { recursive: true });
}
const iconPath = path.join(__dirname, 'assets', 'icon.png');
// Ayarları yükle
const settings = loadSettings();
mainWindow = new BrowserWindow({
width: 1200,
height: 800,
icon: iconPath,
frame: true,
autoHideMenuBar: true,
fullscreen: settings.isFullscreen,
alwaysOnTop: settings.isAlwaysOnTop,
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
webviewTag: true,
preload: path.join(__dirname, 'preload.js'),
webSecurity: true,
sandbox: false,
devTools: true
}
});
const serverUrl = await startServer();
await mainWindow.loadURL(serverUrl);
// Sayfa yüklendikten sonra ayarları uygula
mainWindow.webContents.on('did-finish-load', () => {
applySettings(settings);
});
// Pencere boyutu değiştiğinde
mainWindow.on('resize', () => {
if (!mainWindow.isFullScreen()) {
const currentSettings = loadSettings();
if (currentSettings.isAlwaysOnTop) {
const [width, height] = mainWindow.getSize();
saveSettings({ ...currentSettings, width, height });
}
}
});
// Tam ekran değiştiğinde
mainWindow.on('enter-full-screen', () => {
const currentSettings = loadSettings();
currentSettings.isFullscreen = true;
saveSettings(currentSettings);
});
mainWindow.on('leave-full-screen', () => {
const currentSettings = loadSettings();
currentSettings.isFullscreen = false;
saveSettings(currentSettings);
});
// Pencere kapanmadan önce son ayarları kaydet
mainWindow.on('close', () => {
const currentSettings = loadSettings();
if (!mainWindow.isFullScreen()) {
const [width, height] = mainWindow.getSize();
saveSettings({ ...currentSettings, width, height });
}
});
mainWindow.on('closed', () => {
mainWindow = null;
if (server) {
server.close();
}
});
}
// Express.js ayarları
expressApp.set('view engine', 'ejs');
expressApp.set('views', path.join(__dirname, 'views'));
expressApp.use(express.static(path.join(__dirname, 'public')));
// Express route
expressApp.get('/', (req, res) => {
res.render('index');
});
// Express sunucusunu başlat
function startServer() {
return new Promise((resolve, reject) => {
server = expressApp.listen(0, () => {
const port = server.address().port;
console.log(`Express server calisiyor: http://localhost:${port}`);
resolve(`http://localhost:${port}`);
});
});
}
// Ayarlar event handler'ları
ipcMain.on('save-settings', (event, settings) => {
try {
// Ayarları kaydet
if (saveSettings(settings)) {
// Ayarları uygula
applySettings(settings);
// Başarılı mesajı gönder
event.sender.send('settings-saved', { success: true });
} else {
event.sender.send('settings-saved', { success: false, error: 'Ayarlar kaydedilemedi' });
}
} catch (error) {
console.error('Ayarlar kaydedilirken hata:', error);
event.sender.send('settings-saved', { success: false, error: error.message });
}
});
ipcMain.on('get-settings', (event) => {
try {
const settings = loadSettings();
event.sender.send('settings-loaded', settings);
} catch (error) {
console.error('Ayarlar yuklenirken hata:', error);
event.sender.send('settings-loaded', defaultSettings);
}
});
// Sayfa kaydetme işlemi
ipcMain.handle('save-page', async (event, pageData) => {
try {
// SetScript klasörünü kontrol et
if (!fs.existsSync(setScriptPath)) {
fs.mkdirSync(setScriptPath, { recursive: true });
}
// bookmarks.json dosyasını oku veya oluştur
const bookmarksPath = path.join(setScriptPath, 'bookmarks.json');
let bookmarks = [];
if (fs.existsSync(bookmarksPath)) {
const data = fs.readFileSync(bookmarksPath, 'utf8');
bookmarks = JSON.parse(data);
}
// Yeni kayıt için ID oluştur
const newBookmark = {
id: Date.now().toString(),
name: pageData.name,
url: pageData.url,
desc: pageData.desc || '',
icon: pageData.icon || '',
createdAt: new Date().toISOString()
};
// Yeni kaydı listeye ekle
bookmarks.push(newBookmark);
// Dosyaya kaydet
fs.writeFileSync(bookmarksPath, JSON.stringify(bookmarks, null, 2));
// Tüm pencerelere güncel listeyi gönder
BrowserWindow.getAllWindows().forEach(window => {
window.webContents.send('saved-pages', bookmarks);
});
return { success: true, bookmark: newBookmark };
} catch (error) {
console.error('Sayfa kaydetme hatasi:', error);
throw error;
}
});
// Kaydedilen sayfaları getir
ipcMain.handle('get-saved-pages', async () => {
try {
const bookmarksPath = path.join(setScriptPath, 'bookmarks.json');
if (fs.existsSync(bookmarksPath)) {
const data = fs.readFileSync(bookmarksPath, 'utf8');
return JSON.parse(data);
}
return [];
} catch (error) {
console.error('Kaydedilen sayfalar getirilirken hata:', error);
throw error;
}
});
// Belirli bir sayfayı getir
ipcMain.handle('get-page', async (event, pageId) => {
try {
const bookmarksPath = path.join(setScriptPath, 'bookmarks.json');
if (fs.existsSync(bookmarksPath)) {
const data = fs.readFileSync(bookmarksPath, 'utf8');
const pages = JSON.parse(data);
return pages.find(page => page.id === pageId);
}
return null;
} catch (error) {
console.error('Sayfa getirilirken hata:', error);
throw error;
}
});
// Sayfa silme işlemi
ipcMain.handle('delete-page', async (event, pageId) => {
try {
const bookmarksPath = path.join(setScriptPath, 'bookmarks.json');
if (fs.existsSync(bookmarksPath)) {
let bookmarks = JSON.parse(fs.readFileSync(bookmarksPath, 'utf8'));
bookmarks = bookmarks.filter(page => page.id !== pageId);
fs.writeFileSync(bookmarksPath, JSON.stringify(bookmarks, null, 2));
// Tüm pencerelere güncel listeyi gönder
BrowserWindow.getAllWindows().forEach(window => {
window.webContents.send('saved-pages', bookmarks);
});
return { success: true };
}
return { success: false, error: 'Bookmarks dosyası bulunamadı' };
} catch (error) {
console.error('Sayfa silme hatasi:', error);
throw error;
}
});
// WebView URL'ini al
ipcMain.handle('get-webview-url', async (event) => {
const webContents = event.sender;
return webContents.getURL();
});
// WebView güvenlik ayarları
app.on('web-contents-created', (event, contents) => {
if (contents.getType() === 'webview') {
// Güvenlik ayarları
contents.on('will-navigate', (event, url) => {
console.log('WebView navigasyon:', url);
const parsedUrl = new URL(url);
// Sadece http ve https protokollerine izin ver
if (!['http:', 'https:'].includes(parsedUrl.protocol)) {
event.preventDefault();
}
});
// Yeni pencere açılmasını engelle
contents.setWindowOpenHandler(({ url }) => {
console.log('Yeni pencere engellendi:', url);
return { action: 'deny' };
});
// Hata durumunda
contents.on('did-fail-load', (event, errorCode, errorDescription) => {
console.error('WebView yukleme hatasi:', errorCode, errorDescription);
});
}
});
// Sayfayı çevrimdışı kaydet
async function savePageOffline(url, id) {
try {
const settings = loadSettings();
if (!settings.isAlwaysOnTop) return;
const response = await fetch(url);
const html = await response.text();
const pagePath = path.join(offlinePagesPath, `${id}.html`);
fs.writeFileSync(pagePath, html);
// Eğer otomatik önizleme açıksa
if (settings.isAlwaysOnTop) {
// Webview kullanarak sayfanın ekran göruntüsünü al
const view = new BrowserView({
webPreferences: {
offscreen: true
}
});
mainWindow.setBrowserView(view);
view.setBounds({ x: 0, y: 0, width: 1200, height: 800 });
view.webContents.loadURL(url);
// Sayfa yüklendiğinde ekran görüntüsü al
view.webContents.on('did-finish-load', async () => {
const image = await view.webContents.capturePage();
const previewPath = path.join(offlinePagesPath, `${id}.png`);
fs.writeFileSync(previewPath, image.toPNG());
// Temizlik
mainWindow.removeBrowserView(view);
});
}
return true;
} catch (error) {
console.error('Sayfa cevrimdisi kaydedilirken hata:', error);
return false;
}
}
// Çevrimdışı sayfayı yükle
function loadOfflinePage(id) {
try {
const pagePath = path.join(offlinePagesPath, `${id}.html`);
if (fs.existsSync(pagePath)) {
return fs.readFileSync(pagePath, 'utf8');
}
return null;
} catch (error) {
console.error('Cevrimdisi sayfa yuklenirken hata:', error);
return null;
}
}
// Düzenleme işlemi
ipcMain.on('edit-bookmark', async (event, data) => {
try {
console.log('Duzenleme baslatildi:', data);
const jsonPath = path.join(setScriptPath, 'bookmarks.json');
// JSON dosyasını oku
let bookmarks = [];
if (fs.existsSync(jsonPath)) {
const content = fs.readFileSync(jsonPath, 'utf8');
if (content) {
bookmarks = JSON.parse(content);
}
}
// Düzenlenecek kaydı bul
const index = bookmarks.findIndex(bookmark => bookmark.id.toString() === data.id.toString());
if (index === -1) {
throw new Error('Kayıt bulunamadı');
}
// Kaydı güncelle
bookmarks[index] = {
...bookmarks[index],
name: data.name,
description: data.description
};
// JSON dosyasını güncelle
fs.writeFileSync(jsonPath, JSON.stringify(bookmarks, null, 2));
// Başarılı mesajı gönder
event.reply('bookmark-edited', bookmarks[index]);
// Tüm kayıtları yeniden yükle
mainWindow.webContents.send('saved-pages', bookmarks);
} catch (error) {
console.error('Duzenleme hatasi:', error);
event.reply('bookmark-edited-error', error.message);
}
});
// Silme işlemi
ipcMain.on('delete-bookmark', async (event, id) => {
try {
console.log('Silme baslatildi:', id);
const jsonPath = path.join(setScriptPath, 'bookmarks.json');
// JSON dosyasını oku
let bookmarks = [];
if (fs.existsSync(jsonPath)) {
const content = fs.readFileSync(jsonPath, 'utf8');
if (content) {
bookmarks = JSON.parse(content);
}
}
// Silinecek kaydı bul
const bookmark = bookmarks.find(b => b.id.toString() === id.toString());
if (!bookmark) {
throw new Error('Kayıt bulunamadı');
}
// İkon dosyasını sil
if (bookmark.icon && bookmark.icon.includes('icon_')) {
const iconPath = bookmark.icon.replace('local-file://', '');
if (fs.existsSync(iconPath)) {
fs.unlinkSync(iconPath);
}
}
// Kaydı sil
bookmarks = bookmarks.filter(b => b.id.toString() !== id.toString());
// JSON dosyasını güncelle
fs.writeFileSync(jsonPath, JSON.stringify(bookmarks, null, 2));
// Başarılı mesajı gönder
event.reply('bookmark-deleted', id);
// Tüm kayıtları yeniden yükle
mainWindow.webContents.send('saved-pages', bookmarks);
} catch (error) {
console.error('Silme hatasi:', error);
event.reply('bookmark-deleted-error', error.message);
}
});
// Sayfa güncelleme işlemi
ipcMain.handle('update-page', async (event, pageData) => {
try {
const bookmarksPath = path.join(setScriptPath, 'bookmarks.json');
let bookmarks = [];
if (fs.existsSync(bookmarksPath)) {
const data = fs.readFileSync(bookmarksPath, 'utf8');
bookmarks = JSON.parse(data);
}
const pageIndex = bookmarks.findIndex(page => page.id === pageData.id);
if (pageIndex !== -1) {
// Mevcut sayfayı güncelle
bookmarks[pageIndex] = {
...bookmarks[pageIndex],
name: pageData.name,
description: pageData.description,
updatedAt: new Date().toISOString()
};
fs.writeFileSync(bookmarksPath, JSON.stringify(bookmarks, null, 2));
return { success: true };
} else {
throw new Error('Sayfa bulunamadı');
}
} catch (error) {
console.error('Sayfa guncelleme hatasi:', error);
throw error;
}
});
// Uygulama başlatıldığında
app.whenReady().then(async () => {
try {
await createWindow();
} catch (error) {
console.error('Uygulama baslatma hatasi:', error);
}
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (mainWindow === null) {
createWindow();
}
});
app.on('ready', () => {
DiscordRPC();
});