From 25e9f44260ec015e92776d1f38cdc3d3d89f3904 Mon Sep 17 00:00:00 2001 From: Araxeus Date: Thu, 6 May 2021 04:56:06 +0300 Subject: [PATCH 1/4] fix listener override condition --- plugins/precise-volume/preload.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/precise-volume/preload.js b/plugins/precise-volume/preload.js index bfd6a01c0c..5c2298dd95 100644 --- a/plugins/precise-volume/preload.js +++ b/plugins/precise-volume/preload.js @@ -8,7 +8,7 @@ function overrideAddEventListener() { Element.prototype._addEventListener = Element.prototype.addEventListener; // Override addEventListener to Ignore specific events in volume-slider Element.prototype.addEventListener = function (type, listener, useCapture = false) { - if (this.tagName === "TP-YT-PAPER-SLIDER") { // tagName of #volume-slider + if (this.id === "volume-slider" || this.id === "expand-volume-slider") { // id of both volume sliders for (const eventType of nativeEvents) { if (eventType === type) { return; From ef0813e63858b5aef78d09453a63ecdf4ab49265 Mon Sep 17 00:00:00 2001 From: Araxeus Date: Thu, 6 May 2021 04:58:05 +0300 Subject: [PATCH 2/4] fix `preventDefault()` being called on *all* keys --- plugins/precise-volume/front.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plugins/precise-volume/front.js b/plugins/precise-volume/front.js index a1b30fe473..7195b5bf33 100644 --- a/plugins/precise-volume/front.js +++ b/plugins/precise-volume/front.js @@ -194,12 +194,13 @@ function setupLocalArrowShortcuts(options) { } function callback(event) { - event.preventDefault(); switch (event.code) { case "ArrowUp": + event.preventDefault(); changeVolume(true, options); break; case "ArrowDown": + event.preventDefault(); changeVolume(false, options); break; } From c0a185ba6832f05a45cbb23e9e50737001cf870e Mon Sep 17 00:00:00 2001 From: Araxeus Date: Thu, 6 May 2021 05:18:06 +0300 Subject: [PATCH 3/4] refactor addEventListener --- plugins/precise-volume/preload.js | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/plugins/precise-volume/preload.js b/plugins/precise-volume/preload.js index 5c2298dd95..7979def770 100644 --- a/plugins/precise-volume/preload.js +++ b/plugins/precise-volume/preload.js @@ -1,21 +1,16 @@ const { ipcRenderer } = require("electron"); -// Override specific listeners of volume-slider by modifying Element.prototype function overrideAddEventListener() { - // Events to ignore - const nativeEvents = ["mousewheel", "keydown", "keyup"]; // Save native addEventListener Element.prototype._addEventListener = Element.prototype.addEventListener; // Override addEventListener to Ignore specific events in volume-slider Element.prototype.addEventListener = function (type, listener, useCapture = false) { - if (this.id === "volume-slider" || this.id === "expand-volume-slider") { // id of both volume sliders - for (const eventType of nativeEvents) { - if (eventType === type) { - return; - } - } - }//else - this._addEventListener(type, listener, useCapture); + if (!( + ["volume-slider", "expand-volume-slider"].includes(this.id) && + ["mousewheel", "keydown", "keyup"].includes(type) + )) { + this._addEventListener(type, listener, useCapture); + } }; } @@ -24,5 +19,6 @@ module.exports = () => { // Restore original function after did-finish-load to avoid keeping Element.prototype altered ipcRenderer.once("restoreAddEventListener", () => { //called from Main to make sure page is completly loaded Element.prototype.addEventListener = Element.prototype._addEventListener; + Element.prototype._addEventListener = undefined; }); }; From d000c03fcaef7f60ffa26e0fa1399cd25057553d Mon Sep 17 00:00:00 2001 From: Araxeus Date: Thu, 6 May 2021 05:40:31 +0300 Subject: [PATCH 4/4] lint --- plugins/precise-volume/preload.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/plugins/precise-volume/preload.js b/plugins/precise-volume/preload.js index 7979def770..edc5f20cfd 100644 --- a/plugins/precise-volume/preload.js +++ b/plugins/precise-volume/preload.js @@ -1,4 +1,10 @@ const { ipcRenderer } = require("electron"); +const is = require("electron-is"); + +let ignored = { + id: ["volume-slider", "expand-volume-slider"], + types: ["mousewheel", "keydown", "keyup"] +}; function overrideAddEventListener() { // Save native addEventListener @@ -6,10 +12,12 @@ function overrideAddEventListener() { // Override addEventListener to Ignore specific events in volume-slider Element.prototype.addEventListener = function (type, listener, useCapture = false) { if (!( - ["volume-slider", "expand-volume-slider"].includes(this.id) && - ["mousewheel", "keydown", "keyup"].includes(type) + ignored.id.includes(this.id) && + ignored.types.includes(type) )) { this._addEventListener(type, listener, useCapture); + } else if (is.dev()) { + console.log(`Ignoring event: "${this.id}.${type}()"`); } }; } @@ -17,8 +25,9 @@ function overrideAddEventListener() { module.exports = () => { overrideAddEventListener(); // Restore original function after did-finish-load to avoid keeping Element.prototype altered - ipcRenderer.once("restoreAddEventListener", () => { //called from Main to make sure page is completly loaded + ipcRenderer.once("restoreAddEventListener", () => { // Called from main to make sure page is completly loaded Element.prototype.addEventListener = Element.prototype._addEventListener; Element.prototype._addEventListener = undefined; + ignored = undefined; }); };