diff --git a/index.ts b/index.ts index 70db77af..573207b4 100644 --- a/index.ts +++ b/index.ts @@ -144,7 +144,7 @@ if (is.windows()) { ipcMain.handle('get-main-plugin-names', () => Object.keys(mainPlugins)); -function loadPlugins(win: BrowserWindow) { +async function loadPlugins(win: BrowserWindow) { injectCSS(win.webContents, youtubeMusicCSS); // Load user CSS const themes: string[] = config.get('options.themes'); @@ -175,7 +175,7 @@ function loadPlugins(win: BrowserWindow) { console.log('Loaded plugin - ' + plugin); const handler = mainPlugins[plugin as keyof typeof mainPlugins]; if (handler) { - handler(win, options as never); + await handler(win, options as never); } } } catch (e) { @@ -184,7 +184,7 @@ function loadPlugins(win: BrowserWindow) { } } -function createMainWindow() { +async function createMainWindow() { const windowSize = config.get('window-size'); const windowMaximized = config.get('window-maximized'); const windowPosition: Electron.Point = config.get('window-position'); @@ -223,7 +223,7 @@ function createMainWindow() { : 'default'), autoHideMenuBar: config.get('options.hideMenu'), }); - loadPlugins(win); + await loadPlugins(win); if (windowPosition) { const { x: windowX, y: windowY } = windowPosition; @@ -258,7 +258,6 @@ function createMainWindow() { const urlToLoad = config.get('options.resumeOnStart') ? config.get('url') : config.defaultConfig.url; - win.webContents.loadURL(urlToLoad); win.on('closed', onClosed); type PiPOptions = typeof config.defaultConfig.plugins['picture-in-picture']; @@ -338,6 +337,8 @@ function createMainWindow() { removeContentSecurityPolicy(); + await win.webContents.loadURL(urlToLoad); + return win; } @@ -414,17 +415,17 @@ app.on('window-all-closed', () => { globalShortcut.unregisterAll(); }); -app.on('activate', () => { +app.on('activate', async () => { // On OS X it's common to re-create a window in the app when the // dock icon is clicked and there are no other windows open. if (mainWindow === null) { - mainWindow = createMainWindow(); + mainWindow = await createMainWindow(); } else if (!mainWindow.isVisible()) { mainWindow.show(); } }); -app.on('ready', () => { +app.on('ready', async () => { if (config.get('options.autoResetAppCache')) { // Clear cache after 20s const clearCacheTimeout = setTimeout(() => { @@ -469,7 +470,7 @@ app.on('ready', () => { } } - mainWindow = createMainWindow(); + mainWindow = await createMainWindow(); setApplicationMenu(mainWindow); setUpTray(app, mainWindow); diff --git a/plugins/adblocker/back.ts b/plugins/adblocker/back.ts index d33771ae..ef05889c 100644 --- a/plugins/adblocker/back.ts +++ b/plugins/adblocker/back.ts @@ -8,8 +8,8 @@ import type { ConfigType } from '../../config/dynamic'; type AdBlockOptions = ConfigType<'adblocker'>; export default async (win: BrowserWindow, options: AdBlockOptions) => { - if (await shouldUseBlocklists()) { - loadAdBlockerEngine( + if (shouldUseBlocklists()) { + await loadAdBlockerEngine( win.webContents.session, options.cache, options.additionalBlockLists, diff --git a/plugins/adblocker/blocker.ts b/plugins/adblocker/blocker.ts index dac06555..39ceb8cf 100644 --- a/plugins/adblocker/blocker.ts +++ b/plugins/adblocker/blocker.ts @@ -3,7 +3,7 @@ import path from 'node:path'; import fs, { promises } from 'node:fs'; import { ElectronBlocker } from '@cliqz/adblocker-electron'; -import { app } from 'electron'; +import { app, net } from 'electron'; const SOURCES = [ 'https://raw.githubusercontent.com/kbinani/adblock-youtube-ads/master/signed.txt', @@ -17,7 +17,7 @@ const SOURCES = [ 'https://secure.fanboy.co.nz/fanboy-annoyance_ubo.txt', ]; -export const loadAdBlockerEngine = ( +export const loadAdBlockerEngine = async ( session: Electron.Session | undefined = undefined, cache = true, additionalBlockLists = [], @@ -49,25 +49,24 @@ export const loadAdBlockerEngine = ( ...additionalBlockLists, ]; - ElectronBlocker.fromLists( - fetch, - lists, - { - // When generating the engine for caching, do not load network filters - // So that enhancing the session works as expected - // Allowing to define multiple webRequest listeners - loadNetworkFilters: session !== undefined, - }, - cachingOptions, - ) - .then((blocker) => { - if (session) { - blocker.enableBlockingInSession(session); - } else { - console.log('Successfully generated adBlocker engine.'); - } - }) - .catch((error) => console.log('Error loading adBlocker engine', error)); + try { + const blocker = await ElectronBlocker.fromLists( + (url: string) => net.fetch(url), + lists, + { + // When generating the engine for caching, do not load network filters + // So that enhancing the session works as expected + // Allowing to define multiple webRequest listeners + loadNetworkFilters: session !== undefined, + }, + cachingOptions, + ); + if (session) { + blocker.enableBlockingInSession(session); + } + } catch (error) { + console.log('Error loading adBlocker engine', error); + } }; export default { loadAdBlockerEngine }; diff --git a/plugins/adblocker/config.ts b/plugins/adblocker/config.ts index 869aedc6..d646b0e2 100644 --- a/plugins/adblocker/config.ts +++ b/plugins/adblocker/config.ts @@ -7,7 +7,7 @@ import { PluginConfig } from '../../config/dynamic'; const config = new PluginConfig('adblocker', { enableFront: true }); -export const shouldUseBlocklists = async () => await config.get('blocker') !== blockers.InPlayer; +export const shouldUseBlocklists = () => config.get('blocker') !== blockers.InPlayer; export default Object.assign(config, { shouldUseBlocklists,