mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-12 11:01:45 +00:00
feat: migrate to new plugin api
Co-authored-by: Su-Yong <simssy2205@gmail.com>
This commit is contained in:
@ -17,10 +17,12 @@ const SOURCES = [
|
||||
'https://secure.fanboy.co.nz/fanboy-annoyance_ubo.txt',
|
||||
];
|
||||
|
||||
let blocker: ElectronBlocker | undefined;
|
||||
|
||||
export const loadAdBlockerEngine = async (
|
||||
session: Electron.Session | undefined = undefined,
|
||||
cache = true,
|
||||
additionalBlockLists = [],
|
||||
cache: boolean = true,
|
||||
additionalBlockLists: string[] = [],
|
||||
disableDefaultLists: boolean | unknown[] = false,
|
||||
) => {
|
||||
// Only use cache if no additional blocklists are passed
|
||||
@ -45,7 +47,7 @@ export const loadAdBlockerEngine = async (
|
||||
];
|
||||
|
||||
try {
|
||||
const blocker = await ElectronBlocker.fromLists(
|
||||
blocker = await ElectronBlocker.fromLists(
|
||||
(url: string) => net.fetch(url),
|
||||
lists,
|
||||
{
|
||||
@ -64,4 +66,10 @@ export const loadAdBlockerEngine = async (
|
||||
}
|
||||
};
|
||||
|
||||
export default { loadAdBlockerEngine };
|
||||
export const unloadAdBlockerEngine = (session: Electron.Session) => {
|
||||
if (blocker) {
|
||||
blocker.disableBlockingInSession(session);
|
||||
}
|
||||
};
|
||||
|
||||
export const isBlockerEnabled = (session: Electron.Session) => blocker !== undefined && blocker.isBlockingEnabled(session);
|
||||
|
||||
@ -1,14 +0,0 @@
|
||||
/* renderer */
|
||||
|
||||
import { blockers } from './blocker-types';
|
||||
|
||||
import { PluginConfig } from '../../config/dynamic';
|
||||
|
||||
const config = new PluginConfig('adblocker', { enableFront: true });
|
||||
|
||||
export const shouldUseBlocklists = () => config.get('blocker') !== blockers.InPlayer;
|
||||
|
||||
export default Object.assign(config, {
|
||||
shouldUseBlocklists,
|
||||
blockers,
|
||||
});
|
||||
52
src/plugins/adblocker/index.ts
Normal file
52
src/plugins/adblocker/index.ts
Normal file
@ -0,0 +1,52 @@
|
||||
import { blockers } from './types';
|
||||
|
||||
import { createPluginBuilder } from '../utils/builder';
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
const builder = createPluginBuilder('adblocker', {
|
||||
name: 'Adblocker',
|
||||
restartNeeded: false,
|
||||
config: {
|
||||
enabled: true,
|
||||
cache: true,
|
||||
blocker: blockers.InPlayer,
|
||||
additionalBlockLists: [],
|
||||
disableDefaultLists: false,
|
||||
} as AdblockerConfig,
|
||||
});
|
||||
|
||||
export default builder;
|
||||
|
||||
declare global {
|
||||
interface PluginBuilderList {
|
||||
[builder.id]: typeof builder;
|
||||
}
|
||||
}
|
||||
1
src/plugins/adblocker/inject.d.ts
vendored
1
src/plugins/adblocker/inject.d.ts
vendored
@ -1 +0,0 @@
|
||||
export const inject: () => void;
|
||||
3
src/plugins/adblocker/injectors/inject.d.ts
vendored
Normal file
3
src/plugins/adblocker/injectors/inject.d.ts
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
export const inject: () => void;
|
||||
|
||||
export const isInjected: () => boolean;
|
||||
@ -7,7 +7,13 @@
|
||||
Parts of this code is derived from set-constant.js:
|
||||
https://github.com/gorhill/uBlock/blob/5de0ce975753b7565759ac40983d31978d1f84ca/assets/resources/scriptlets.js#L704
|
||||
*/
|
||||
|
||||
let injected = false;
|
||||
|
||||
export const isInjected = () => isInjected;
|
||||
|
||||
export const inject = () => {
|
||||
injected = true;
|
||||
{
|
||||
const pruner = function (o) {
|
||||
delete o.playerAds;
|
||||
@ -1,19 +1,43 @@
|
||||
import { BrowserWindow } from 'electron';
|
||||
|
||||
import { loadAdBlockerEngine } from './blocker';
|
||||
import { shouldUseBlocklists } from './config';
|
||||
import { isBlockerEnabled, loadAdBlockerEngine, unloadAdBlockerEngine } from './blocker';
|
||||
|
||||
import type { ConfigType } from '../../config/dynamic';
|
||||
import builder from './index';
|
||||
import { blockers } from './types';
|
||||
|
||||
type AdBlockOptions = ConfigType<'adblocker'>;
|
||||
export default builder.createMain(({ getConfig }) => {
|
||||
let mainWindow: BrowserWindow | undefined;
|
||||
|
||||
export default async (win: BrowserWindow, options: AdBlockOptions) => {
|
||||
if (shouldUseBlocklists()) {
|
||||
await loadAdBlockerEngine(
|
||||
win.webContents.session,
|
||||
options.cache,
|
||||
options.additionalBlockLists,
|
||||
options.disableDefaultLists,
|
||||
);
|
||||
}
|
||||
};
|
||||
return {
|
||||
async onLoad(window) {
|
||||
const config = await getConfig();
|
||||
mainWindow = window;
|
||||
|
||||
if (config.blocker === blockers.WithBlocklists) {
|
||||
await loadAdBlockerEngine(
|
||||
window.webContents.session,
|
||||
config.cache,
|
||||
config.additionalBlockLists,
|
||||
config.disableDefaultLists,
|
||||
);
|
||||
}
|
||||
},
|
||||
onUnload(window) {
|
||||
if (isBlockerEnabled(window.webContents.session)) {
|
||||
unloadAdBlockerEngine(window.webContents.session);
|
||||
}
|
||||
},
|
||||
async onConfigChange(newConfig) {
|
||||
if (mainWindow) {
|
||||
if (newConfig.blocker === blockers.WithBlocklists && !isBlockerEnabled(mainWindow.webContents.session)) {
|
||||
await loadAdBlockerEngine(
|
||||
mainWindow.webContents.session,
|
||||
newConfig.cache,
|
||||
newConfig.additionalBlockLists,
|
||||
newConfig.disableDefaultLists,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
@ -1,21 +1,22 @@
|
||||
import config from './config';
|
||||
import { blockers } from './types';
|
||||
import builder from './index';
|
||||
|
||||
import { blockers } from './blocker-types';
|
||||
import type { MenuTemplate } from '../../menu';
|
||||
|
||||
import { MenuTemplate } from '../../menu';
|
||||
export default builder.createMenu(async ({ getConfig, setConfig }): Promise<MenuTemplate> => {
|
||||
const config = await getConfig();
|
||||
|
||||
export default (): MenuTemplate => {
|
||||
return [
|
||||
{
|
||||
label: 'Blocker',
|
||||
submenu: Object.values(blockers).map((blocker: string) => ({
|
||||
submenu: Object.values(blockers).map((blocker) => ({
|
||||
label: blocker,
|
||||
type: 'radio',
|
||||
checked: (config.get('blocker') || blockers.WithBlocklists) === blocker,
|
||||
checked: (config.blocker || blockers.WithBlocklists) === blocker,
|
||||
click() {
|
||||
config.set('blocker', blocker);
|
||||
setConfig({ blocker });
|
||||
},
|
||||
})),
|
||||
},
|
||||
];
|
||||
};
|
||||
});
|
||||
|
||||
@ -1,15 +1,27 @@
|
||||
import config, { shouldUseBlocklists } from './config';
|
||||
import { inject } from './inject';
|
||||
import injectCliqzPreload from './inject-cliqz-preload';
|
||||
import { inject, isInjected } from './injectors/inject';
|
||||
import injectCliqzPreload from './injectors/inject-cliqz-preload';
|
||||
|
||||
import { blockers } from './blocker-types';
|
||||
import { blockers } from './types';
|
||||
import builder from './index';
|
||||
|
||||
export default async () => {
|
||||
if (shouldUseBlocklists()) {
|
||||
// Preload adblocker to inject scripts/styles
|
||||
await injectCliqzPreload();
|
||||
// eslint-disable-next-line @typescript-eslint/await-thenable
|
||||
} else if ((config.get('blocker')) === blockers.InPlayer) {
|
||||
inject();
|
||||
export default builder.createPreload(({ getConfig }) => ({
|
||||
async onLoad() {
|
||||
const config = await getConfig();
|
||||
|
||||
if (config.blocker === blockers.WithBlocklists) {
|
||||
// Preload adblocker to inject scripts/styles
|
||||
await injectCliqzPreload();
|
||||
} else if (config.blocker === blockers.InPlayer) {
|
||||
inject();
|
||||
}
|
||||
},
|
||||
async onConfigChange(newConfig) {
|
||||
if (newConfig.blocker === blockers.WithBlocklists) {
|
||||
await injectCliqzPreload();
|
||||
} else if (newConfig.blocker === blockers.InPlayer) {
|
||||
if (!isInjected()) {
|
||||
inject();
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}));
|
||||
|
||||
Reference in New Issue
Block a user