From 6b72599f80a13627b70f5c7db03301caa4928fd6 Mon Sep 17 00:00:00 2001 From: Araxeus Date: Thu, 6 May 2021 21:20:42 +0300 Subject: [PATCH 1/5] directly playPause video element --- preload.js | 8 ++++++-- providers/song-controls-front.js | 18 ++++++++++++++++++ providers/song-controls.js | 2 +- 3 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 providers/song-controls-front.js diff --git a/preload.js b/preload.js index 6cdaebdd10..5a09c84548 100644 --- a/preload.js +++ b/preload.js @@ -5,6 +5,8 @@ const { remote } = require("electron"); const config = require("./config"); const { fileExists } = require("./plugins/utils"); const setupFrontLogger = require("./providers/front-logger"); +const setupSongControl = require("./providers/song-controls-front"); +const setupSongInfo = require("./providers/song-info-front"); const plugins = config.plugins.getEnabled(); @@ -37,8 +39,10 @@ document.addEventListener("DOMContentLoaded", () => { }); // inject song-info provider - const songInfoProviderPath = path.join(__dirname, "providers", "song-info-front.js") - fileExists(songInfoProviderPath, require(songInfoProviderPath)); + setupSongInfo(); + + // inject song-control provider + setupSongControl(); // inject front logger setupFrontLogger(); diff --git a/providers/song-controls-front.js b/providers/song-controls-front.js new file mode 100644 index 0000000000..c620203fe4 --- /dev/null +++ b/providers/song-controls-front.js @@ -0,0 +1,18 @@ +const { ipcRenderer } = require("electron"); + +let videoStream; +module.exports = () => { + videoStream = document.querySelector(".video-stream"); + + ipcRenderer.on("playPause", () => { + if (!videoStream) { + videoStream = document.querySelector(".video-stream"); + } + + if (videoStream.paused) { + videoStream.play(); + } else { + videoStream.yns_pause(); + } + }); +} \ No newline at end of file diff --git a/providers/song-controls.js b/providers/song-controls.js index 7f43df11bb..5d3ad953d2 100644 --- a/providers/song-controls.js +++ b/providers/song-controls.js @@ -12,7 +12,7 @@ module.exports = (win) => { // Playback previous: () => pressKey(win, "k"), next: () => pressKey(win, "j"), - playPause: () => pressKey(win, "space"), + playPause: () => win.webContents.send("playPause"), like: () => pressKey(win, "_"), dislike: () => pressKey(win, "+"), go10sBack: () => pressKey(win, "h"), From a98b8945fba9a1001624acdfb0cb4c74afb6ebb4 Mon Sep 17 00:00:00 2001 From: Araxeus Date: Thu, 6 May 2021 21:32:44 +0300 Subject: [PATCH 2/5] lint --- providers/song-controls-front.js | 2 +- providers/song-info-front.js | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/providers/song-controls-front.js b/providers/song-controls-front.js index c620203fe4..094d9a9366 100644 --- a/providers/song-controls-front.js +++ b/providers/song-controls-front.js @@ -15,4 +15,4 @@ module.exports = () => { videoStream.yns_pause(); } }); -} \ No newline at end of file +}; diff --git a/providers/song-info-front.js b/providers/song-info-front.js index fbc98b993a..fccc54a8f0 100644 --- a/providers/song-info-front.js +++ b/providers/song-info-front.js @@ -10,17 +10,16 @@ ipcRenderer.on("update-song-info", async (_, extractedSongInfo) => { }); const injectListener = () => { - var oldXHR = window.XMLHttpRequest; + const oldXHR = window.XMLHttpRequest; function newXHR() { - var realXHR = new oldXHR(); + const realXHR = new oldXHR(); realXHR.addEventListener( "readystatechange", () => { - if (realXHR.readyState == 4 && realXHR.status == 200) { - if (realXHR.responseURL.includes("/player")) { + if (realXHR.readyState === 4 && realXHR.status === 200 + && realXHR.responseURL.includes("/player")) { // if the request contains the song info, send the response to ipcMain ipcRenderer.send("song-info-request", realXHR.responseText); - } } }, false From d274e80f755841c0c9b837ff98981e0a8bcd9976 Mon Sep 17 00:00:00 2001 From: Araxeus Date: Thu, 6 May 2021 21:41:08 +0300 Subject: [PATCH 3/5] minify --- providers/song-controls-front.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/providers/song-controls-front.js b/providers/song-controls-front.js index 094d9a9366..c91bb3c060 100644 --- a/providers/song-controls-front.js +++ b/providers/song-controls-front.js @@ -1,9 +1,7 @@ const { ipcRenderer } = require("electron"); -let videoStream; +let videoStream = document.querySelector(".video-stream"); module.exports = () => { - videoStream = document.querySelector(".video-stream"); - ipcRenderer.on("playPause", () => { if (!videoStream) { videoStream = document.querySelector(".video-stream"); From 88e738c79626c932ae9f36772a927fb72990bf5f Mon Sep 17 00:00:00 2001 From: Araxeus Date: Fri, 7 May 2021 04:17:17 +0300 Subject: [PATCH 4/5] check if `yns_pause` exists --- providers/song-controls-front.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/providers/song-controls-front.js b/providers/song-controls-front.js index c91bb3c060..841f7cb68b 100644 --- a/providers/song-controls-front.js +++ b/providers/song-controls-front.js @@ -10,7 +10,11 @@ module.exports = () => { if (videoStream.paused) { videoStream.play(); } else { - videoStream.yns_pause(); + if (videoStream.yns_pause) { + videoStream.yns_pause(); + } else { + videoStream.pause(); + } } }); }; From e00be8f01024a21ad7fe453aeb6ff4e09cde40aa Mon Sep 17 00:00:00 2001 From: Araxeus Date: Fri, 7 May 2021 05:43:26 +0300 Subject: [PATCH 5/5] lint --- providers/song-controls-front.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/providers/song-controls-front.js b/providers/song-controls-front.js index 841f7cb68b..1860e7614e 100644 --- a/providers/song-controls-front.js +++ b/providers/song-controls-front.js @@ -10,11 +10,9 @@ module.exports = () => { if (videoStream.paused) { videoStream.play(); } else { - if (videoStream.yns_pause) { - videoStream.yns_pause(); - } else { + videoStream.yns_pause ? + videoStream.yns_pause() : videoStream.pause(); - } } }); };