-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(plugins): restore and fix adblocker plugin #4212
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
base: master
Are you sure you want to change the base?
Changes from all commits
69229da
3eeddfb
2421ff2
5aed127
e490c46
f9613aa
f104f3b
dce57d4
f3e462c
0dfa52d
6b1b336
240b098
be2ae28
b92d44a
5e0375f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| /ad-blocker-engine.bin |
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,80 @@ | ||||||||||
| // Used for caching | ||||||||||
| import path from 'node:path'; | ||||||||||
| import fs, { promises } from 'node:fs'; | ||||||||||
|
|
||||||||||
| import { ElectronBlocker } from '@ghostery/adblocker-electron'; | ||||||||||
| import { app, net } from 'electron'; | ||||||||||
|
|
||||||||||
| const SOURCES = [ | ||||||||||
| 'https://raw.githubusercontent.com/kbinani/adblock-youtube-ads/master/signed.txt', | ||||||||||
| // UBlock Origin | ||||||||||
| 'https://raw.githubusercontent.com/ghostery/adblocker/master/packages/adblocker/assets/ublock-origin/filters.txt', | ||||||||||
| 'https://raw.githubusercontent.com/ghostery/adblocker/master/packages/adblocker/assets/ublock-origin/quick-fixes.txt', | ||||||||||
| 'https://raw.githubusercontent.com/ghostery/adblocker/master/packages/adblocker/assets/ublock-origin/unbreak.txt', | ||||||||||
| 'https://raw.githubusercontent.com/ghostery/adblocker/master/packages/adblocker/assets/ublock-origin/filters-2020.txt', | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Suggested change
|
||||||||||
| 'https://raw.githubusercontent.com/ghostery/adblocker/master/packages/adblocker/assets/ublock-origin/filters-2021.txt', | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Suggested change
|
||||||||||
| 'https://raw.githubusercontent.com/ghostery/adblocker/master/packages/adblocker/assets/ublock-origin/filters-2022.txt', | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Suggested change
|
||||||||||
| 'https://raw.githubusercontent.com/ghostery/adblocker/master/packages/adblocker/assets/ublock-origin/filters-2023.txt', | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Suggested change
|
||||||||||
| // Fanboy Annoyances | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Suggested change
|
||||||||||
| 'https://secure.fanboy.co.nz/fanboy-annoyance_ubo.txt', | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Suggested change
|
||||||||||
| // AdGuard | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Suggested change
|
||||||||||
| 'https://filters.adtidy.org/extension/ublock/filters/122_optimized.txt', | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Suggested change
|
||||||||||
| ]; | ||||||||||
|
|
||||||||||
| let blocker: ElectronBlocker | undefined; | ||||||||||
|
|
||||||||||
| export const loadAdBlockerEngine = async ( | ||||||||||
| session: Electron.Session | undefined = undefined, | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Suggested change
|
||||||||||
| cache: boolean = true, | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Suggested change
|
||||||||||
| additionalBlockLists: string[] = [], | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Suggested change
|
||||||||||
| disableDefaultLists: boolean | unknown[] = false, | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Suggested change
|
||||||||||
| ) => { | ||||||||||
| // Only use cache if no additional blocklists are passed | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Suggested change
|
||||||||||
| const cacheDirectory = path.join(app.getPath('userData'), 'adblock_cache'); | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Suggested change
|
||||||||||
| if (!fs.existsSync(cacheDirectory)) { | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Suggested change
|
||||||||||
| fs.mkdirSync(cacheDirectory); | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Suggested change
|
||||||||||
| } | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Suggested change
|
||||||||||
| const cachingOptions = | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Suggested change
|
||||||||||
| cache && additionalBlockLists.length === 0 | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Suggested change
|
||||||||||
| ? { | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Suggested change
|
||||||||||
| path: path.join(cacheDirectory, 'adblocker-engine.bin'), | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Suggested change
|
||||||||||
| read: promises.readFile, | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Suggested change
|
||||||||||
| write: promises.writeFile, | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Suggested change
|
||||||||||
| } | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Suggested change
|
||||||||||
| : undefined; | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Suggested change
|
||||||||||
| const lists = [ | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Suggested change
|
||||||||||
| ...((disableDefaultLists && !Array.isArray(disableDefaultLists)) || | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Suggested change
|
||||||||||
| (Array.isArray(disableDefaultLists) && disableDefaultLists.length > 0) | ||||||||||
| ? [] | ||||||||||
| : SOURCES), | ||||||||||
| ...additionalBlockLists, | ||||||||||
| ]; | ||||||||||
|
|
||||||||||
| try { | ||||||||||
| 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 const unloadAdBlockerEngine = (session: Electron.Session) => { | ||||||||||
| if (blocker) { | ||||||||||
| blocker.disableBlockingInSession(session); | ||||||||||
| } | ||||||||||
| }; | ||||||||||
|
|
||||||||||
| export const isBlockerEnabled = (session: Electron.Session) => | ||||||||||
| blocker !== undefined && blocker.isBlockingEnabled(session); | ||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| import { contextBridge, webFrame } from 'electron'; | ||
|
|
||
| import { blockers } from './types'; | ||
| import { createPlugin } from '@/utils'; | ||
| import { | ||
| isBlockerEnabled, | ||
| loadAdBlockerEngine, | ||
| unloadAdBlockerEngine, | ||
| } from './blocker'; | ||
|
|
||
| import injectCliqzPreload from './injectors/inject-cliqz-preload'; | ||
| import { inject, isInjected } from './injectors/inject'; | ||
|
|
||
| import { t } from '@/i18n'; | ||
|
|
||
| import type { BrowserWindow } from 'electron'; | ||
|
|
||
| interface AdblockerConfig { | ||
| /** | ||
| * Whether to enable the adblocker. | ||
| * @default true | ||
| */ | ||
| enabled: boolean; | ||
| /** | ||
| * When enabled, the adblocker will cache the blocklists. | ||
| * @default true | ||
| */ | ||
| cache: boolean; | ||
| /** | ||
| * Which adblocker to use. | ||
| * @default blockers.InPlayer | ||
| */ | ||
| blocker: (typeof blockers)[keyof typeof blockers]; | ||
| /** | ||
| * Additional list of filters to use. | ||
| * @example ["https://raw.githubusercontent.com/uBlockOrigin/uAssets/master/filters/filters.txt"] | ||
| * @default [] | ||
| */ | ||
| additionalBlockLists: string[]; | ||
| /** | ||
| * Disable the default blocklists. | ||
| * @default false | ||
| */ | ||
| disableDefaultLists: boolean; | ||
| } | ||
|
|
||
| export default createPlugin({ | ||
| name: () => t('plugins.adblocker.name'), | ||
| description: () => t('plugins.adblocker.description'), | ||
| restartNeeded: false, | ||
| config: { | ||
| enabled: true, | ||
| cache: true, | ||
| blocker: blockers.InPlayer, | ||
| additionalBlockLists: [], | ||
| disableDefaultLists: false, | ||
| } as AdblockerConfig, | ||
| menu: async ({ getConfig, setConfig }) => { | ||
| const config = await getConfig(); | ||
|
|
||
| return [ | ||
| { | ||
| label: t('plugins.adblocker.menu.blocker'), | ||
| submenu: Object.values(blockers).map((blocker) => ({ | ||
| label: blocker, | ||
| type: 'radio', | ||
| checked: (config.blocker || blockers.WithBlocklists) === blocker, | ||
| click() { | ||
| setConfig({ blocker }); | ||
| }, | ||
| })), | ||
| }, | ||
| ]; | ||
| }, | ||
| backend: { | ||
| mainWindow: null as BrowserWindow | null, | ||
| async start({ getConfig, window }) { | ||
| const config = await getConfig(); | ||
| this.mainWindow = window; | ||
|
|
||
| if (config.blocker === blockers.WithBlocklists) { | ||
| await loadAdBlockerEngine( | ||
| window.webContents.session, | ||
| config.cache, | ||
| config.additionalBlockLists, | ||
| config.disableDefaultLists, | ||
| ); | ||
| } | ||
| }, | ||
| stop({ window }) { | ||
| if (isBlockerEnabled(window.webContents.session)) { | ||
| unloadAdBlockerEngine(window.webContents.session); | ||
| } | ||
| }, | ||
| async onConfigChange(newConfig) { | ||
| if (this.mainWindow) { | ||
| if ( | ||
| newConfig.blocker === blockers.WithBlocklists && | ||
| !isBlockerEnabled(this.mainWindow.webContents.session) | ||
| ) { | ||
| await loadAdBlockerEngine( | ||
| this.mainWindow.webContents.session, | ||
| newConfig.cache, | ||
| newConfig.additionalBlockLists, | ||
| newConfig.disableDefaultLists, | ||
| ); | ||
| } | ||
| } | ||
| }, | ||
| }, | ||
| preload: { | ||
| script: 'window.JSON.parse = window._proxyJsonParse; window._proxyJsonParse = undefined; window.Response.prototype.json = window._proxyResponseJson; window._proxyResponseJson = undefined; 0', | ||
| async start({ getConfig }) { | ||
| const config = await getConfig(); | ||
|
|
||
| if (config.blocker === blockers.WithBlocklists) { | ||
| // Preload adblocker to inject scripts/styles | ||
| await injectCliqzPreload(); | ||
| } else if (config.blocker === blockers.InPlayer && !isInjected()) { | ||
| inject(contextBridge); | ||
| await webFrame.executeJavaScript(this.script); | ||
| } | ||
| }, | ||
| async onConfigChange(newConfig) { | ||
| if (newConfig.blocker === blockers.WithBlocklists) { | ||
| await injectCliqzPreload(); | ||
| } else if (newConfig.blocker === blockers.InPlayer && !isInjected()) { | ||
| inject(contextBridge); | ||
| await webFrame.executeJavaScript(this.script); | ||
| } | ||
| }, | ||
| }, | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| export default async () => { | ||
| await import('@ghostery/adblocker-electron-preload'); | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import type { ContextBridge } from 'electron'; | ||
|
|
||
| export const inject: (contextBridge: ContextBridge) => void; | ||
|
|
||
| export const isInjected: () => boolean; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Delete
··