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

Fix precise volume listener override #253

Merged
merged 4 commits into from
May 6, 2021
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
3 changes: 2 additions & 1 deletion plugins/precise-volume/front.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
29 changes: 17 additions & 12 deletions plugins/precise-volume/preload.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
const { ipcRenderer } = require("electron");
const is = require("electron-is");

let ignored = {
id: ["volume-slider", "expand-volume-slider"],
types: ["mousewheel", "keydown", "keyup"]
};

// 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.tagName === "TP-YT-PAPER-SLIDER") { // tagName of #volume-slider
for (const eventType of nativeEvents) {
if (eventType === type) {
return;
}
}
}//else
this._addEventListener(type, listener, useCapture);
if (!(
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}()"`);
}
};
}

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;
});
};