|
| 1 | +const { app, BrowserWindow, ipcMain, session } = require('electron'); |
| 2 | +const path = require('path'); |
| 3 | +const Store = require('electron-store'); |
| 4 | +const { ElectronBlocker } = require('@ghostery/adblocker-electron'); |
| 5 | +const fetch = require('cross-fetch'); |
| 6 | + |
| 7 | +const store = new Store(); |
| 8 | + |
| 9 | +// Configs |
| 10 | +const URL_TO_LOAD = 'https://streamingcommunityz.cc/'; |
| 11 | +const USER_AGENT = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"; |
| 12 | +let mainWindow = null; |
| 13 | +let blocker = null; |
| 14 | + |
| 15 | +// Adblock |
| 16 | +async function initAdBlocker() { |
| 17 | + blocker = await ElectronBlocker.fromPrebuiltAdsAndTracking(fetch); |
| 18 | + blocker.enableBlockingInSession(session.defaultSession); |
| 19 | +} |
| 20 | + |
| 21 | +// Window |
| 22 | +function createWindow() { |
| 23 | + const windowBounds = store.get('windowBounds', { width: 1280, height: 720 }); |
| 24 | + |
| 25 | + mainWindow = new BrowserWindow({ |
| 26 | + ...windowBounds, |
| 27 | + minWidth: 800, |
| 28 | + minHeight: 600, |
| 29 | + icon: path.join(__dirname, 'assets', 'icon.png'), |
| 30 | + titleBarStyle: 'default', |
| 31 | + show: false, |
| 32 | + webPreferences: { |
| 33 | + nodeIntegration: false, |
| 34 | + contextIsolation: true, |
| 35 | + enableRemoteModule: false, |
| 36 | + webSecurity: true, |
| 37 | + allowRunningInsecureContent: false, |
| 38 | + experimentalFeatures: true, |
| 39 | + }, |
| 40 | + }); |
| 41 | + |
| 42 | + // Set UserAgent |
| 43 | + mainWindow.webContents.setUserAgent(USER_AGENT); |
| 44 | + |
| 45 | + // Load Site |
| 46 | + const loadSite = async (url = URL_TO_LOAD) => { |
| 47 | + try { |
| 48 | + await mainWindow.loadURL(url); |
| 49 | + } catch (error) { |
| 50 | + console.error('[Load Error]', error); |
| 51 | + } |
| 52 | + }; |
| 53 | + loadSite(); |
| 54 | + |
| 55 | + // Show it when ready |
| 56 | + mainWindow.once('ready-to-show', () => mainWindow.show()); |
| 57 | + |
| 58 | + // Save window |
| 59 | + let saveTimeout; |
| 60 | + const saveBounds = () => { |
| 61 | + clearTimeout(saveTimeout); |
| 62 | + saveTimeout = setTimeout(() => { |
| 63 | + if (mainWindow) { |
| 64 | + store.set('windowBounds', mainWindow.getBounds()); |
| 65 | + } |
| 66 | + }, 300); |
| 67 | + }; |
| 68 | + |
| 69 | + mainWindow.on('resize', saveBounds); |
| 70 | + mainWindow.on('moved', saveBounds); |
| 71 | + |
| 72 | + mainWindow.once('closed', () => { |
| 73 | + mainWindow = null; |
| 74 | + }); |
| 75 | + |
| 76 | + // Block new windows |
| 77 | + mainWindow.webContents.setWindowOpenHandler(() => ({ action: 'deny' })); |
| 78 | + |
| 79 | + // Block unauthorized domains |
| 80 | + mainWindow.webContents.on('will-navigate', (event, navigationUrl) => { |
| 81 | + const allowedDomains = [URL_TO_LOAD]; |
| 82 | + if (!allowedDomains.some(domain => navigationUrl.startsWith(domain))) { |
| 83 | + console.warn('[Blocked Navigation]', navigationUrl); |
| 84 | + event.preventDefault(); |
| 85 | + } |
| 86 | + }); |
| 87 | +} |
| 88 | + |
| 89 | +// Events |
| 90 | +app.whenReady().then(async () => { |
| 91 | + await initAdBlocker(); |
| 92 | + createWindow(); |
| 93 | + |
| 94 | + app.on('activate', () => { |
| 95 | + if (BrowserWindow.getAllWindows().length === 0) createWindow(); |
| 96 | + }); |
| 97 | +}); |
| 98 | + |
| 99 | +app.on('window-all-closed', () => { |
| 100 | + if (process.platform !== 'darwin') app.quit(); |
| 101 | +}); |
| 102 | + |
| 103 | +// IPC |
| 104 | +ipcMain.handle('get-store-value', (_, key, def) => store.get(key, def)); |
| 105 | +ipcMain.handle('set-store-value', (_, key, val) => store.set(key, val)); |
| 106 | +ipcMain.handle('load-url', (_, url) => mainWindow?.loadURL(url)); |
| 107 | +ipcMain.handle('go-home', () => mainWindow?.loadURL(URL_TO_LOAD)); |
| 108 | +ipcMain.handle('reload-page', () => mainWindow?.reload()); |
| 109 | + |
| 110 | +// Error handling |
| 111 | +process.on('uncaughtException', err => console.error('[Uncaught Exception]', err)); |
| 112 | +process.on('unhandledRejection', (reason, p) => |
| 113 | + console.error('[Unhandled Rejection]', p, 'Reason:', reason) |
| 114 | +); |
0 commit comments