From a4a35641366867d689a3e817a4732bdcd49a7465 Mon Sep 17 00:00:00 2001 From: David Metzler Date: Sat, 28 Jan 2023 15:42:32 +0100 Subject: [PATCH 1/6] fixed 'repeatChanged' modes being different depending on selected youtube music language --- .gitignore | 1 + providers/song-info-front.js | 51 +++++++++++++++++++++++++++++++----- 2 files changed, 45 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index 543c5150..93f62fa7 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ node_modules /assets/generated electron-builder.yml .vscode/settings.json +.idea diff --git a/providers/song-info-front.js b/providers/song-info-front.js index 03cf2957..592af276 100644 --- a/providers/song-info-front.js +++ b/providers/song-info-front.js @@ -1,6 +1,6 @@ -const { ipcRenderer } = require("electron"); +const {ipcRenderer} = require("electron"); const is = require('electron-is'); -const { getImage } = require("./song-info"); +const {getImage} = require("./song-info"); const config = require("../config"); @@ -55,7 +55,7 @@ module.exports = () => { data.videoDetails.isPaused = false; ipcRenderer.send("video-src-changed", JSON.stringify(data)); } - }, { once: true, passive: true }); + }, {once: true, passive: true}); }; function setupTimeChangeListener() { @@ -63,17 +63,54 @@ function setupTimeChangeListener() { ipcRenderer.send('timeChanged', mutations[0].target.value); global.songInfo.elapsedSeconds = mutations[0].target.value; }); - progressObserver.observe($('#progress-bar'), { attributeFilter: ["value"] }) + progressObserver.observe($('#progress-bar'), {attributeFilter: ["value"]}) } function setupRepeatChangeListener() { const repeatObserver = new MutationObserver(mutations => { - ipcRenderer.send('repeatChanged', mutations[0].target.title); + document.cookie.split(';').forEach((cookie) => { + let cookieName = cookie.substring(0, cookie.indexOf("=")).replaceAll(" ", ""); + if (cookieName === 'PREF') { + let value = cookie.replace(cookieName + "=", "").replaceAll(" ", ""); + value.split('&').forEach((pair) => { + if (pair !== '') { + let splitpair = pair.split('='); + if (splitpair[0] === "repeat") { + if (splitpair[1] === "NONE") + ipcRenderer.send('repeatChanged', "Repeat off"); + else if (splitpair[1] === "ONE") //MPRIS Playlist and Track Codes are switched to look the same as yt-music icons + ipcRenderer.send('repeatChanged', "Repeat one"); + else if (splitpair[1] === "ALL") + ipcRenderer.send('repeatChanged', "Repeat all"); + } + } + }); + } + }); }); - repeatObserver.observe($('#right-controls .repeat'), { attributeFilter: ["title"] }); + repeatObserver.observe($('#right-controls .repeat'), {attributeFilter: ["title"]}); // Emit the initial value as well; as it's persistent between launches. - ipcRenderer.send('repeatChanged', $('#right-controls .repeat').title); + // ipcRenderer.send('repeatChanged', $('#right-controls .repeat').title); + document.cookie.split(';').forEach((cookie) => { + let cookieName = cookie.substring(0, cookie.indexOf("=")).replaceAll(" ", ""); + if (cookieName === 'PREF') { + let value = cookie.replace(cookieName + "=", "").replaceAll(" ", ""); + value.split('&').forEach((pair) => { + if (pair !== '') { + let splitpair = pair.split('='); + if (splitpair[0] === "repeat") { + if (splitpair[1] === "NONE") + ipcRenderer.send('repeatChanged', "Repeat off"); + else if (splitpair[1] === "ONE") //MPRIS Playlist and Track Codes are switched to look the same as yt-music icons + ipcRenderer.send('repeatChanged', "Repeat one"); + else if (splitpair[1] === "ALL") + ipcRenderer.send('repeatChanged', "Repeat all"); + } + } + }); + } + }); } function setupVolumeChangeListener(api) { From 317e3af41254010686f364c0aada29e2c7561079 Mon Sep 17 00:00:00 2001 From: David Metzler Date: Sat, 28 Jan 2023 15:43:16 +0100 Subject: [PATCH 2/6] connected mpris shuffle, fixed volume, mpris volumes allowed 0.0-1.0 --- plugins/shortcuts/mpris.js | 82 +++++++++++++++++++------------------- 1 file changed, 40 insertions(+), 42 deletions(-) diff --git a/plugins/shortcuts/mpris.js b/plugins/shortcuts/mpris.js index bf427b58..e481bfce 100644 --- a/plugins/shortcuts/mpris.js +++ b/plugins/shortcuts/mpris.js @@ -1,5 +1,5 @@ const mpris = require("mpris-service"); -const { ipcMain } = require("electron"); +const {ipcMain} = require("electron"); const registerCallback = require("../../providers/song-info"); const getSongControls = require("../../providers/song-controls"); const config = require("../../config"); @@ -20,7 +20,7 @@ function setupMPRIS() { function registerMPRIS(win) { const songControls = getSongControls(win); - const { playPause, next, previous, volumeMinus10, volumePlus10 } = songControls; + const {playPause, next, previous, volumeMinus10, volumePlus10, shuffle} = songControls; try { const secToMicro = n => Math.round(Number(n) * 1e6); const microToSec = n => Math.round(Number(n) / 1e6); @@ -35,33 +35,23 @@ function registerMPRIS(win) { let currentSeconds = 0; ipcMain.on('timeChanged', (_, t) => currentSeconds = t); - let currentLoopStatus = undefined; - let manuallySwitchingStatus = false; ipcMain.on("repeatChanged", (_, mode) => { - if (manuallySwitchingStatus) - return; - if (mode === "Repeat off") - currentLoopStatus = "None"; - else if (mode === "Repeat one") - currentLoopStatus = "Track"; + player.loopStatus = mpris.LOOP_STATUS_NONE; + else if (mode === "Repeat one") //MPRIS Playlist and Track Codes are switched to look the same as yt-music icons + player.loopStatus = mpris.LOOP_STATUS_PLAYLIST; else if (mode === "Repeat all") - currentLoopStatus = "Playlist"; - - player.loopStatus = currentLoopStatus; + player.loopStatus = mpris.LOOP_STATUS_TRACK; }); player.on("loopStatus", (status) => { // switchRepeat cycles between states in that order - const switches = ["None", "Playlist", "Track"]; - const currentIndex = switches.indexOf(currentLoopStatus); + const switches = [mpris.LOOP_STATUS_NONE, mpris.LOOP_STATUS_PLAYLIST, mpris.LOOP_STATUS_TRACK]; + const currentIndex = switches.indexOf(player.loopStatus); const targetIndex = switches.indexOf(status); // Get a delta in the range [0,2] const delta = (targetIndex - currentIndex + 3) % 3; - - manuallySwitchingStatus = true; songControls.switchRepeat(delta); - manuallySwitchingStatus = false; }) player.getPosition = () => secToMicro(currentSeconds) @@ -72,19 +62,19 @@ function registerMPRIS(win) { }); player.on("play", () => { - if (player.playbackStatus !== 'Playing') { - player.playbackStatus = 'Playing'; + if (player.playbackStatus !== mpris.PLAYBACK_STATUS_PLAYING) { + player.playbackStatus = mpris.PLAYBACK_STATUS_PLAYING; playPause() } }); player.on("pause", () => { - if (player.playbackStatus !== 'Paused') { - player.playbackStatus = 'Paused'; + if (player.playbackStatus !== mpris.PLAYBACK_STATUS_PAUSED) { + player.playbackStatus = mpris.PLAYBACK_STATUS_PAUSED; playPause() } }); player.on("playpause", () => { - player.playbackStatus = player.playbackStatus === 'Playing' ? "Paused" : "Playing"; + player.playbackStatus = player.playbackStatus === mpris.PLAYBACK_STATUS_PLAYING ? mpris.PLAYBACK_STATUS_PAUSED : mpris.PLAYBACK_STATUS_PLAYING; playPause(); }); @@ -94,40 +84,48 @@ function registerMPRIS(win) { player.on('seek', seekBy); player.on('position', seekTo); + player.on('shuffle', (enableShuffle) => { + shuffle(); + }); + ipcMain.on('volumeChanged', (_, value) => { - player.volume = value; + if (config.plugins.isEnabled('precise-volume')) { + player.volume = value / 100; + } }); player.on('volume', (newVolume) => { if (config.plugins.isEnabled('precise-volume')) { // With precise volume we can set the volume to the exact value. - win.webContents.send('setVolume', newVolume) + win.webContents.send('setVolume', newVolume * 100) } else { // With keyboard shortcuts we can only change the volume in increments of 10, so round it. - const deltaVolume = Math.round((newVolume - player.volume) / 10); - - if (deltaVolume > 0) { - for (let i = 0; i < deltaVolume; i++) - volumePlus10(); - } else { - for (let i = 0; i < -deltaVolume; i++) - volumeMinus10(); + let deltaVolume = Math.round((newVolume - player.volume) * 10); + while (deltaVolume !== 0 && deltaVolume > 0) { + volumePlus10(); + player.volume = player.volume + 0.1; + deltaVolume--; + } + while (deltaVolume !== 0 && deltaVolume < 0) { + volumeMinus10(); + player.volume = player.volume - 0.1; + deltaVolume++; } } }); - registerCallback(songInfo => { - if (player) { - const data = { - 'mpris:length': secToMicro(songInfo.songDuration), - 'mpris:artUrl': songInfo.imageSrc, - 'xesam:title': songInfo.title, - 'xesam:artist': [songInfo.artist], + registerCallback(songInfo => { + if (player) { + const data = { + 'mpris:length': secToMicro(songInfo.songDuration), + 'mpris:artUrl': songInfo.imageSrc, + 'xesam:title': songInfo.title, + 'xesam:artist': [songInfo.artist], 'mpris:trackid': '/' }; if (songInfo.album) data['xesam:album'] = songInfo.album; player.metadata = data; - player.seeked(secToMicro(songInfo.elapsedSeconds)) - player.playbackStatus = songInfo.isPaused ? "Paused" : "Playing" + player.seeked(secToMicro(songInfo.elapsedSeconds)); + player.playbackStatus = songInfo.isPaused ? mpris.PLAYBACK_STATUS_PAUSED : mpris.PLAYBACK_STATUS_PLAYING; } }) From 8bf2c8397ec8f6c20421d9a9de11ba906b3eb31a Mon Sep 17 00:00:00 2001 From: David Metzler Date: Sat, 28 Jan 2023 20:36:31 +0100 Subject: [PATCH 3/6] removed unnecessary if and used better Repeat change detection --- plugins/shortcuts/mpris.js | 5 ++--- providers/song-info-front.js | 42 +++--------------------------------- 2 files changed, 5 insertions(+), 42 deletions(-) diff --git a/plugins/shortcuts/mpris.js b/plugins/shortcuts/mpris.js index e481bfce..071b2855 100644 --- a/plugins/shortcuts/mpris.js +++ b/plugins/shortcuts/mpris.js @@ -89,10 +89,9 @@ function registerMPRIS(win) { }); ipcMain.on('volumeChanged', (_, value) => { - if (config.plugins.isEnabled('precise-volume')) { - player.volume = value / 100; - } + player.volume = value / 100; }); + player.on('volume', (newVolume) => { if (config.plugins.isEnabled('precise-volume')) { // With precise volume we can set the volume to the exact value. diff --git a/providers/song-info-front.js b/providers/song-info-front.js index 592af276..c6c65876 100644 --- a/providers/song-info-front.js +++ b/providers/song-info-front.js @@ -67,50 +67,14 @@ function setupTimeChangeListener() { } function setupRepeatChangeListener() { + const mp = { NONE: "Repeat off", ONE: "Repeat one", ALL: "Repeat all" } const repeatObserver = new MutationObserver(mutations => { - document.cookie.split(';').forEach((cookie) => { - let cookieName = cookie.substring(0, cookie.indexOf("=")).replaceAll(" ", ""); - if (cookieName === 'PREF') { - let value = cookie.replace(cookieName + "=", "").replaceAll(" ", ""); - value.split('&').forEach((pair) => { - if (pair !== '') { - let splitpair = pair.split('='); - if (splitpair[0] === "repeat") { - if (splitpair[1] === "NONE") - ipcRenderer.send('repeatChanged', "Repeat off"); - else if (splitpair[1] === "ONE") //MPRIS Playlist and Track Codes are switched to look the same as yt-music icons - ipcRenderer.send('repeatChanged', "Repeat one"); - else if (splitpair[1] === "ALL") - ipcRenderer.send('repeatChanged', "Repeat all"); - } - } - }); - } - }); + ipcRenderer.send('repeatChanged', mp[mutations[0].target.__dataHost.getState().queue.repeatMode]) }); repeatObserver.observe($('#right-controls .repeat'), {attributeFilter: ["title"]}); // Emit the initial value as well; as it's persistent between launches. - // ipcRenderer.send('repeatChanged', $('#right-controls .repeat').title); - document.cookie.split(';').forEach((cookie) => { - let cookieName = cookie.substring(0, cookie.indexOf("=")).replaceAll(" ", ""); - if (cookieName === 'PREF') { - let value = cookie.replace(cookieName + "=", "").replaceAll(" ", ""); - value.split('&').forEach((pair) => { - if (pair !== '') { - let splitpair = pair.split('='); - if (splitpair[0] === "repeat") { - if (splitpair[1] === "NONE") - ipcRenderer.send('repeatChanged', "Repeat off"); - else if (splitpair[1] === "ONE") //MPRIS Playlist and Track Codes are switched to look the same as yt-music icons - ipcRenderer.send('repeatChanged', "Repeat one"); - else if (splitpair[1] === "ALL") - ipcRenderer.send('repeatChanged', "Repeat all"); - } - } - }); - } - }); + ipcRenderer.send('repeatChanged', mp[$('ytmusic-player-bar').getState().queue.repeatMode]); } function setupVolumeChangeListener(api) { From 9da0e4305f078a081c5a828a3365194865157ede Mon Sep 17 00:00:00 2001 From: David Metzler Date: Fri, 3 Feb 2023 12:02:25 +0100 Subject: [PATCH 4/6] Fixed recursive volume changes that caused cpu spike, Switched Repeat Modes to NONE|ONE|ALL --- plugins/shortcuts/mpris.js | 31 +++++++++++++++++++++++++------ providers/song-info-front.js | 5 ++--- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/plugins/shortcuts/mpris.js b/plugins/shortcuts/mpris.js index 071b2855..90c1406a 100644 --- a/plugins/shortcuts/mpris.js +++ b/plugins/shortcuts/mpris.js @@ -36,11 +36,11 @@ function registerMPRIS(win) { ipcMain.on('timeChanged', (_, t) => currentSeconds = t); ipcMain.on("repeatChanged", (_, mode) => { - if (mode === "Repeat off") + if (mode === "NONE") player.loopStatus = mpris.LOOP_STATUS_NONE; - else if (mode === "Repeat one") //MPRIS Playlist and Track Codes are switched to look the same as yt-music icons + else if (mode === "ONE") //MPRIS Playlist and Track Codes are switched to look the same as yt-music icons player.loopStatus = mpris.LOOP_STATUS_PLAYLIST; - else if (mode === "Repeat all") + else if (mode === "ALL") player.loopStatus = mpris.LOOP_STATUS_TRACK; }); player.on("loopStatus", (status) => { @@ -88,14 +88,33 @@ function registerMPRIS(win) { shuffle(); }); - ipcMain.on('volumeChanged', (_, value) => { - player.volume = value / 100; + let mprisVolNewer = false; + let autoUpdate = false; + ipcMain.on('volumeChanged', (_, newVol) => { + if (parseInt(player.volume * 100) !== newVol) { + if (mprisVolNewer) { + mprisVolNewer = false; + autoUpdate = false; + } else { + autoUpdate = true; + player.volume = parseFloat((newVol / 100).toFixed(2)); + mprisVolNewer = false; + autoUpdate = false; + } + } }); player.on('volume', (newVolume) => { if (config.plugins.isEnabled('precise-volume')) { // With precise volume we can set the volume to the exact value. - win.webContents.send('setVolume', newVolume * 100) + let newVol = parseInt(newVolume * 100); + if (parseInt(player.volume * 100) !== newVol) { + if (!autoUpdate){ + mprisVolNewer = true; + autoUpdate = false; + win.webContents.send('setVolume', newVol); + } + } } else { // With keyboard shortcuts we can only change the volume in increments of 10, so round it. let deltaVolume = Math.round((newVolume - player.volume) * 10); diff --git a/providers/song-info-front.js b/providers/song-info-front.js index c6c65876..958bcd0a 100644 --- a/providers/song-info-front.js +++ b/providers/song-info-front.js @@ -67,14 +67,13 @@ function setupTimeChangeListener() { } function setupRepeatChangeListener() { - const mp = { NONE: "Repeat off", ONE: "Repeat one", ALL: "Repeat all" } const repeatObserver = new MutationObserver(mutations => { - ipcRenderer.send('repeatChanged', mp[mutations[0].target.__dataHost.getState().queue.repeatMode]) + ipcRenderer.send('repeatChanged', mutations[0].target.__dataHost.getState().queue.repeatMode) }); repeatObserver.observe($('#right-controls .repeat'), {attributeFilter: ["title"]}); // Emit the initial value as well; as it's persistent between launches. - ipcRenderer.send('repeatChanged', mp[$('ytmusic-player-bar').getState().queue.repeatMode]); + ipcRenderer.send('repeatChanged', $('ytmusic-player-bar').getState().queue.repeatMode); } function setupVolumeChangeListener(api) { From b9aa6ffdd4de9dd56810b527caf509ef28c2b6fc Mon Sep 17 00:00:00 2001 From: TC Date: Thu, 9 Feb 2023 22:08:40 +0100 Subject: [PATCH 5/6] Only build without release if it is a fork --- .github/workflows/build.yml | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index c6d384ff..b885ce89 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -65,27 +65,44 @@ jobs: with: run: yarn test - - name: Build on Mac - if: startsWith(matrix.os, 'macOS') + # Build and release if it's the main repository + - name: Build and release on Mac + if: startsWith(matrix.os, 'macOS') && github.repository == 'th-ch/youtube-music' env: GH_TOKEN: ${{ secrets.GH_TOKEN }} run: | yarn run release:mac - - name: Build on Linux - if: startsWith(matrix.os, 'ubuntu') + - name: Build and release on Linux + if: startsWith(matrix.os, 'ubuntu') && github.repository == 'th-ch/youtube-music' env: GH_TOKEN: ${{ secrets.GH_TOKEN }} run: | yarn run release:linux - - name: Build on Windows - if: startsWith(matrix.os, 'windows') + - name: Build and release on Windows + if: startsWith(matrix.os, 'windows') && github.repository == 'th-ch/youtube-music' env: GH_TOKEN: ${{ secrets.GH_TOKEN }} run: | yarn run release:win + # Only build without release if it is a fork + - name: Build on Mac + if: startsWith(matrix.os, 'macOS') && github.repository != 'th-ch/youtube-music' + run: | + yarn run build:mac + + - name: Build on Linux + if: startsWith(matrix.os, 'ubuntu') && github.repository != 'th-ch/youtube-music' + run: | + yarn run build:linux + + - name: Build on Windows + if: startsWith(matrix.os, 'windows') && github.repository != 'th-ch/youtube-music' + run: | + yarn run build:win + release: runs-on: ubuntu-latest name: Release YouTube Music From 9ec07b5fb79884a7408654103e589bb0dbda3bab Mon Sep 17 00:00:00 2001 From: TC Date: Thu, 9 Feb 2023 22:11:26 +0100 Subject: [PATCH 6/6] Only run the release stage if it is the main repo --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index b885ce89..e5c7e8a0 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -106,7 +106,7 @@ jobs: release: runs-on: ubuntu-latest name: Release YouTube Music - if: github.ref == 'refs/heads/master' + if: github.repository == 'th-ch/youtube-music' && github.ref == 'refs/heads/master' needs: build steps: - uses: actions/checkout@v3