Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hotfix(adblocker): fix ipcRenderer.sendSync() with ... #1301

Merged
merged 1 commit into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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) {
Expand All @@ -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');
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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'];
Expand Down Expand Up @@ -338,6 +337,8 @@ function createMainWindow() {

removeContentSecurityPolicy();

await win.webContents.loadURL(urlToLoad);

return win;
}

Expand Down Expand Up @@ -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(() => {
Expand Down Expand Up @@ -469,7 +470,7 @@ app.on('ready', () => {
}
}

mainWindow = createMainWindow();
mainWindow = await createMainWindow();
setApplicationMenu(mainWindow);
setUpTray(app, mainWindow);

Expand Down
4 changes: 2 additions & 2 deletions plugins/adblocker/back.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
41 changes: 20 additions & 21 deletions plugins/adblocker/blocker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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 = [],
Expand Down Expand Up @@ -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 };
2 changes: 1 addition & 1 deletion plugins/adblocker/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down