fix(precise-volume): fix precise-volume plugin

This commit is contained in:
JellyBrick
2023-11-08 02:59:20 +09:00
parent adc1f6822b
commit f560b62de0
3 changed files with 9 additions and 10 deletions

View File

@ -1,7 +1,8 @@
import { overrideListener } from './override';
import { debounce } from '../../providers/decorators'; import { debounce } from '../../providers/decorators';
import { YoutubePlayer } from '../../types/youtube-player'; import type { YoutubePlayer } from '../../types/youtube-player';
import type { ConfigType } from '../../config/dynamic'; import type { ConfigType } from '../../config/dynamic';
function $<E extends Element = Element>(selector: string) { function $<E extends Element = Element>(selector: string) {
@ -12,6 +13,8 @@ let api: YoutubePlayer;
let options: ConfigType<'precise-volume'>; let options: ConfigType<'precise-volume'>;
export default (_options: ConfigType<'precise-volume'>) => { export default (_options: ConfigType<'precise-volume'>) => {
overrideListener();
options = _options; options = _options;
document.addEventListener('apiLoaded', (e) => { document.addEventListener('apiLoaded', (e) => {
api = e.detail; api = e.detail;

View File

@ -1,12 +1,10 @@
/* what */ /* what */
/* eslint-disable @typescript-eslint/ban-ts-comment */ /* eslint-disable @typescript-eslint/ban-ts-comment */
import is from 'electron-is';
const ignored = { const ignored = {
id: ['volume-slider', 'expand-volume-slider'], id: ['volume-slider', 'expand-volume-slider'],
types: ['mousewheel', 'keydown', 'keyup'], types: ['mousewheel', 'keydown', 'keyup'],
}; } as const;
function overrideAddEventListener() { function overrideAddEventListener() {
// YO WHAT ARE YOU DOING NOW?!?! // YO WHAT ARE YOU DOING NOW?!?!
@ -15,20 +13,20 @@ function overrideAddEventListener() {
// eslint-disable-next-line @typescript-eslint/unbound-method // eslint-disable-next-line @typescript-eslint/unbound-method
Element.prototype._addEventListener = Element.prototype.addEventListener; Element.prototype._addEventListener = Element.prototype.addEventListener;
// Override addEventListener to Ignore specific events in volume-slider // Override addEventListener to Ignore specific events in volume-slider
Element.prototype.addEventListener = function (type: string, listener: (event: Event) => void, useCapture = false) { Element.prototype.addEventListener = function(type: string, listener: (event: Event) => void, useCapture = false) {
if (!( if (!(
ignored.id.includes(this.id) ignored.id.includes(this.id)
&& ignored.types.includes(type) && ignored.types.includes(type)
)) { )) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any,@typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access // eslint-disable-next-line @typescript-eslint/no-explicit-any,@typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access
(this as any)._addEventListener(type, listener, useCapture); (this as any)._addEventListener(type, listener, useCapture);
} else if (is.dev()) { } else if (window.electronIs.dev()) {
console.log(`Ignoring event: "${this.id}.${type}()"`); console.log(`Ignoring event: "${this.id}.${type}()"`);
} }
}; };
} }
export default () => { export const overrideListener = () => {
overrideAddEventListener(); overrideAddEventListener();
// Restore original function after finished loading to avoid keeping Element.prototype altered // Restore original function after finished loading to avoid keeping Element.prototype altered
window.addEventListener('load', () => { window.addEventListener('load', () => {

View File

@ -4,7 +4,6 @@ import is from 'electron-is';
import config from './config'; import config from './config';
import adblockerPreload from './plugins/adblocker/preload'; import adblockerPreload from './plugins/adblocker/preload';
import preciseVolumePreload from './plugins/precise-volume/preload';
import type { ConfigType, OneOfDefaultConfigKey } from './config/dynamic'; import type { ConfigType, OneOfDefaultConfigKey } from './config/dynamic';
@ -18,7 +17,6 @@ export type PluginMapper<Type extends 'renderer' | 'preload' | 'backend'> = {
const preloadPlugins: PluginMapper<'preload'> = { const preloadPlugins: PluginMapper<'preload'> = {
'adblocker': adblockerPreload, 'adblocker': adblockerPreload,
'precise-volume': preciseVolumePreload,
}; };
const enabledPluginNameAndOptions = config.plugins.getEnabled(); const enabledPluginNameAndOptions = config.plugins.getEnabled();