Remove preload.js in plugin uses and use front plugin injection

This commit is contained in:
TC
2021-05-04 21:29:39 +02:00
parent 8aeddcf8d8
commit 4cb658daca
4 changed files with 27 additions and 35 deletions

View File

@ -13,7 +13,6 @@ module.exports = (win) => {
// did-finish-load is called after all elements finished loading, including said listeners // did-finish-load is called after all elements finished loading, including said listeners
// Thats the reason the timing is controlled from main // Thats the reason the timing is controlled from main
win.webContents.once("did-finish-load", () => { win.webContents.once("did-finish-load", () => {
win.webContents.send("restoreAddEventListener");
win.webContents.send("setupVideoPlayerVolumeMousewheel", !isEnabled("hide-video-player")); win.webContents.send("setupVideoPlayerVolumeMousewheel", !isEnabled("hide-video-player"));
}); });
}; };

View File

@ -5,6 +5,7 @@ const { setOptions } = require("../../config/plugins");
function $(selector) { return document.querySelector(selector); } function $(selector) { return document.querySelector(selector); }
module.exports = (options) => { module.exports = (options) => {
overrideAddEventListener();
setupPlaybar(options); setupPlaybar(options);
@ -22,9 +23,35 @@ module.exports = (options) => {
ipcRenderer.once("setupVideoPlayerVolumeMousewheel", (_event, toEnable) => { ipcRenderer.once("setupVideoPlayerVolumeMousewheel", (_event, toEnable) => {
if (toEnable) if (toEnable)
setupVideoPlayerOnwheel(options); setupVideoPlayerOnwheel(options);
// Restore original function after did-finish-load to avoid keeping Element.prototype altered
Element.prototype.addEventListener = Element.prototype._addEventListener;
}); });
}; };
// 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);
};
}
/** Add onwheel event to video player */ /** Add onwheel event to video player */
function setupVideoPlayerOnwheel(options) { function setupVideoPlayerOnwheel(options) {
$("#main-panel").addEventListener("wheel", event => { $("#main-panel").addEventListener("wheel", event => {

View File

@ -1,28 +0,0 @@
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.tagName === "TP-YT-PAPER-SLIDER") { // tagName of #volume-slider
for (const eventType of nativeEvents) {
if (eventType === type) {
return;
}
}
}//else
this._addEventListener(type, listener, useCapture);
};
}
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
Element.prototype.addEventListener = Element.prototype._addEventListener;
});
};

View File

@ -8,12 +8,6 @@ const { fileExists } = require("./plugins/utils");
const plugins = config.plugins.getEnabled(); const plugins = config.plugins.getEnabled();
plugins.forEach(([plugin, options]) => { plugins.forEach(([plugin, options]) => {
const preloadPath = path.join(__dirname, "plugins", plugin, "preload.js");
fileExists(preloadPath, () => {
const run = require(preloadPath);
run(options);
});
const actionPath = path.join(__dirname, "plugins", plugin, "actions.js"); const actionPath = path.join(__dirname, "plugins", plugin, "actions.js");
fileExists(actionPath, () => { fileExists(actionPath, () => {
const actions = require(actionPath).actions || {}; const actions = require(actionPath).actions || {};