Skip to content

Commit f08f003

Browse files
authored
Merge pull request #1301 from th-ch/fix/1300
hotfix(adblocker): fix `ipcRenderer.sendSync() with ...`
2 parents 9f99ede + c512f13 commit f08f003

File tree

4 files changed

+33
-33
lines changed

4 files changed

+33
-33
lines changed

index.ts

+10-9
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ if (is.windows()) {
144144

145145
ipcMain.handle('get-main-plugin-names', () => Object.keys(mainPlugins));
146146

147-
function loadPlugins(win: BrowserWindow) {
147+
async function loadPlugins(win: BrowserWindow) {
148148
injectCSS(win.webContents, youtubeMusicCSS);
149149
// Load user CSS
150150
const themes: string[] = config.get('options.themes');
@@ -175,7 +175,7 @@ function loadPlugins(win: BrowserWindow) {
175175
console.log('Loaded plugin - ' + plugin);
176176
const handler = mainPlugins[plugin as keyof typeof mainPlugins];
177177
if (handler) {
178-
handler(win, options as never);
178+
await handler(win, options as never);
179179
}
180180
}
181181
} catch (e) {
@@ -184,7 +184,7 @@ function loadPlugins(win: BrowserWindow) {
184184
}
185185
}
186186

187-
function createMainWindow() {
187+
async function createMainWindow() {
188188
const windowSize = config.get('window-size');
189189
const windowMaximized = config.get('window-maximized');
190190
const windowPosition: Electron.Point = config.get('window-position');
@@ -223,7 +223,7 @@ function createMainWindow() {
223223
: 'default'),
224224
autoHideMenuBar: config.get('options.hideMenu'),
225225
});
226-
loadPlugins(win);
226+
await loadPlugins(win);
227227

228228
if (windowPosition) {
229229
const { x: windowX, y: windowY } = windowPosition;
@@ -258,7 +258,6 @@ function createMainWindow() {
258258
const urlToLoad = config.get('options.resumeOnStart')
259259
? config.get('url')
260260
: config.defaultConfig.url;
261-
win.webContents.loadURL(urlToLoad);
262261
win.on('closed', onClosed);
263262

264263
type PiPOptions = typeof config.defaultConfig.plugins['picture-in-picture'];
@@ -338,6 +337,8 @@ function createMainWindow() {
338337

339338
removeContentSecurityPolicy();
340339

340+
await win.webContents.loadURL(urlToLoad);
341+
341342
return win;
342343
}
343344

@@ -414,17 +415,17 @@ app.on('window-all-closed', () => {
414415
globalShortcut.unregisterAll();
415416
});
416417

417-
app.on('activate', () => {
418+
app.on('activate', async () => {
418419
// On OS X it's common to re-create a window in the app when the
419420
// dock icon is clicked and there are no other windows open.
420421
if (mainWindow === null) {
421-
mainWindow = createMainWindow();
422+
mainWindow = await createMainWindow();
422423
} else if (!mainWindow.isVisible()) {
423424
mainWindow.show();
424425
}
425426
});
426427

427-
app.on('ready', () => {
428+
app.on('ready', async () => {
428429
if (config.get('options.autoResetAppCache')) {
429430
// Clear cache after 20s
430431
const clearCacheTimeout = setTimeout(() => {
@@ -469,7 +470,7 @@ app.on('ready', () => {
469470
}
470471
}
471472

472-
mainWindow = createMainWindow();
473+
mainWindow = await createMainWindow();
473474
setApplicationMenu(mainWindow);
474475
setUpTray(app, mainWindow);
475476

plugins/adblocker/back.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import type { ConfigType } from '../../config/dynamic';
88
type AdBlockOptions = ConfigType<'adblocker'>;
99

1010
export default async (win: BrowserWindow, options: AdBlockOptions) => {
11-
if (await shouldUseBlocklists()) {
12-
loadAdBlockerEngine(
11+
if (shouldUseBlocklists()) {
12+
await loadAdBlockerEngine(
1313
win.webContents.session,
1414
options.cache,
1515
options.additionalBlockLists,

plugins/adblocker/blocker.ts

+20-21
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import path from 'node:path';
33
import fs, { promises } from 'node:fs';
44

55
import { ElectronBlocker } from '@cliqz/adblocker-electron';
6-
import { app } from 'electron';
6+
import { app, net } from 'electron';
77

88
const SOURCES = [
99
'https://raw.githubusercontent.com/kbinani/adblock-youtube-ads/master/signed.txt',
@@ -17,7 +17,7 @@ const SOURCES = [
1717
'https://secure.fanboy.co.nz/fanboy-annoyance_ubo.txt',
1818
];
1919

20-
export const loadAdBlockerEngine = (
20+
export const loadAdBlockerEngine = async (
2121
session: Electron.Session | undefined = undefined,
2222
cache = true,
2323
additionalBlockLists = [],
@@ -49,25 +49,24 @@ export const loadAdBlockerEngine = (
4949
...additionalBlockLists,
5050
];
5151

52-
ElectronBlocker.fromLists(
53-
fetch,
54-
lists,
55-
{
56-
// When generating the engine for caching, do not load network filters
57-
// So that enhancing the session works as expected
58-
// Allowing to define multiple webRequest listeners
59-
loadNetworkFilters: session !== undefined,
60-
},
61-
cachingOptions,
62-
)
63-
.then((blocker) => {
64-
if (session) {
65-
blocker.enableBlockingInSession(session);
66-
} else {
67-
console.log('Successfully generated adBlocker engine.');
68-
}
69-
})
70-
.catch((error) => console.log('Error loading adBlocker engine', error));
52+
try {
53+
const blocker = await ElectronBlocker.fromLists(
54+
(url: string) => net.fetch(url),
55+
lists,
56+
{
57+
// When generating the engine for caching, do not load network filters
58+
// So that enhancing the session works as expected
59+
// Allowing to define multiple webRequest listeners
60+
loadNetworkFilters: session !== undefined,
61+
},
62+
cachingOptions,
63+
);
64+
if (session) {
65+
blocker.enableBlockingInSession(session);
66+
}
67+
} catch (error) {
68+
console.log('Error loading adBlocker engine', error);
69+
}
7170
};
7271

7372
export default { loadAdBlockerEngine };

plugins/adblocker/config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { PluginConfig } from '../../config/dynamic';
77

88
const config = new PluginConfig('adblocker', { enableFront: true });
99

10-
export const shouldUseBlocklists = async () => await config.get('blocker') !== blockers.InPlayer;
10+
export const shouldUseBlocklists = () => config.get('blocker') !== blockers.InPlayer;
1111

1212
export default Object.assign(config, {
1313
shouldUseBlocklists,

0 commit comments

Comments
 (0)