From 7cf78c6635919d561cc915077404592e132d7fc1 Mon Sep 17 00:00:00 2001 From: Araxeus Date: Mon, 10 May 2021 04:15:56 +0300 Subject: [PATCH 1/5] setup SongInfo **once** --- index.js | 2 ++ plugins/discord/back.js | 4 +--- plugins/downloader/back.js | 3 +-- plugins/last-fm/back.js | 6 ++---- plugins/notifications/back.js | 3 +-- plugins/taskbar-mediacontrol/back.js | 3 +-- plugins/touchbar/back.js | 3 +-- providers/song-info.js | 20 ++++++++++---------- 8 files changed, 19 insertions(+), 25 deletions(-) diff --git a/index.js b/index.js index 81fd45b505..f4e4212d0d 100644 --- a/index.js +++ b/index.js @@ -11,6 +11,7 @@ const { setApplicationMenu } = require("./menu"); const { fileExists, injectCSS } = require("./plugins/utils"); const { isTesting } = require("./utils/testing"); const { setUpTray } = require("./tray"); +const { setupSongInfo } = require("./providers/song-info"); // Catch errors and log them unhandled({ @@ -157,6 +158,7 @@ function createMainWindow() { } app.once("browser-window-created", (event, win) => { + setupSongInfo(win); loadPlugins(win); win.webContents.on("did-fail-load", ( diff --git a/plugins/discord/back.js b/plugins/discord/back.js index 33dbb38b7c..c6faadd7f1 100644 --- a/plugins/discord/back.js +++ b/plugins/discord/back.js @@ -1,6 +1,6 @@ const Discord = require("discord-rpc"); -const getSongInfo = require("../../providers/song-info"); +const registerCallback = require("../../providers/song-info"); const rpc = new Discord.Client({ transport: "ipc", @@ -12,8 +12,6 @@ const clientId = "790655993809338398"; let clearActivity; module.exports = (win, {activityTimoutEnabled, activityTimoutTime}) => { - const registerCallback = getSongInfo(win); - // If the page is ready, register the callback win.once("ready-to-show", () => { rpc.once("ready", () => { diff --git a/plugins/downloader/back.js b/plugins/downloader/back.js index 76a77b929f..ee345ab2f2 100644 --- a/plugins/downloader/back.js +++ b/plugins/downloader/back.js @@ -4,7 +4,7 @@ const { join } = require("path"); const ID3Writer = require("browser-id3-writer"); const { dialog, ipcMain } = require("electron"); -const getSongInfo = require("../../providers/song-info"); +const registerCallback = require("../../providers/song-info"); const { injectCSS, listenAction } = require("../utils"); const { cropMaxWidth } = require("./utils"); const { ACTIONS, CHANNEL } = require("./actions.js"); @@ -25,7 +25,6 @@ let nowPlayingMetadata = {}; function handle(win) { injectCSS(win.webContents, join(__dirname, "style.css")); - const registerCallback = getSongInfo(win); registerCallback((info) => { nowPlayingMetadata = info; }); diff --git a/plugins/last-fm/back.js b/plugins/last-fm/back.js index 5497510328..567dccc0d7 100644 --- a/plugins/last-fm/back.js +++ b/plugins/last-fm/back.js @@ -2,7 +2,7 @@ const fetch = require('node-fetch'); const md5 = require('md5'); const { shell } = require('electron'); const { setOptions } = require('../../config/plugins'); -const getSongInfo = require('../../providers/song-info'); +const registerCallback = require('../../providers/song-info'); const defaultConfig = require('../../config/defaults'); const createFormData = params => { @@ -128,9 +128,7 @@ const setNowPlaying = (songInfo, config) => { // this will store the timeout that will trigger addScrobble let scrobbleTimer = undefined; -const lastfm = async (win, config) => { - const registerCallback = getSongInfo(win); - +const lastfm = async (_win, config) => { if (!config.api_root) { // settings are not present, creating them with the default values config = defaultConfig.plugins['last-fm']; diff --git a/plugins/notifications/back.js b/plugins/notifications/back.js index d1ef31142d..91d3ea7288 100644 --- a/plugins/notifications/back.js +++ b/plugins/notifications/back.js @@ -1,6 +1,6 @@ const { Notification } = require("electron"); const is = require("electron-is"); -const getSongInfo = require("../../providers/song-info"); +const registerCallback = require("../../providers/song-info"); const { notificationImage } = require("./utils"); const { setupInteractive, notifyInteractive } = require("./interactive") @@ -29,7 +29,6 @@ module.exports = (win, options) => { if (isInteractive) { setupInteractive(win, options.unpauseNotification); } - const registerCallback = getSongInfo(win); let oldNotification; let oldURL = ""; win.once("ready-to-show", () => { diff --git a/plugins/taskbar-mediacontrol/back.js b/plugins/taskbar-mediacontrol/back.js index 46cb399e63..26a117320b 100644 --- a/plugins/taskbar-mediacontrol/back.js +++ b/plugins/taskbar-mediacontrol/back.js @@ -1,12 +1,11 @@ const getSongControls = require('../../providers/song-controls'); -const getSongInfo = require('../../providers/song-info'); +const registerCallback = require('../../providers/song-info'); const path = require('path'); let controls; let currentSongInfo; module.exports = win => { - const registerCallback = getSongInfo(win); const { playPause, next, previous } = getSongControls(win); controls = { playPause, next, previous }; diff --git a/plugins/touchbar/back.js b/plugins/touchbar/back.js index 0fca17a71c..acaee5833b 100644 --- a/plugins/touchbar/back.js +++ b/plugins/touchbar/back.js @@ -7,7 +7,7 @@ const { TouchBarScrubber, } = TouchBar; -const getSongInfo = require("../../providers/song-info"); +const registerCallback = require("../../providers/song-info"); const getSongControls = require("../../providers/song-controls"); // Songtitle label @@ -59,7 +59,6 @@ const touchBar = new TouchBar({ }); module.exports = (win) => { - const registerCallback = getSongInfo(win); const { playPause, next, previous, like, dislike } = getSongControls(win); // If the page is ready, register the callback diff --git a/providers/song-info.js b/providers/song-info.js index 5701d1f38c..97ddb0712d 100644 --- a/providers/song-info.js +++ b/providers/song-info.js @@ -51,6 +51,7 @@ const songInfo = { }; const handleData = async (responseText, win) => { + console.log("handling song-info") let data = JSON.parse(responseText); songInfo.title = data?.videoDetails?.title; songInfo.artist = await getArtist(win) || cleanupArtistName(data?.videoDetails?.author); @@ -64,15 +65,15 @@ const handleData = async (responseText, win) => { win.webContents.send("update-song-info", JSON.stringify(songInfo)); }; -const registerProvider = (win) => { - // This variable will be filled with the callbacks once they register - const callbacks = []; +// This variable will be filled with the callbacks once they register +const callbacks = []; - // This function will allow plugins to register callback that will be triggered when data changes - const registerCallback = (callback) => { - callbacks.push(callback); - }; +// This function will allow plugins to register callback that will be triggered when data changes +const registerCallback = (callback) => { + callbacks.push(callback); +}; +const registerProvider = (win) => { win.on("page-title-updated", async () => { // Get and set the new data songInfo.isPaused = await getPausedStatus(win); @@ -93,8 +94,6 @@ const registerProvider = (win) => { c(songInfo); }); }); - - return registerCallback; }; const suffixesToRemove = [' - Topic', 'VEVO']; @@ -110,7 +109,8 @@ function cleanupArtistName(artist) { return artist; } -module.exports = registerProvider; +module.exports = registerCallback; +module.exports.setupSongInfo = registerProvider; module.exports.getImage = getImage; module.exports.cleanupArtistName = cleanupArtistName; From cb743de7fdec4ebb932c2ad3d0aa2b529c6a71f2 Mon Sep 17 00:00:00 2001 From: Araxeus Date: Mon, 10 May 2021 05:00:58 +0300 Subject: [PATCH 2/5] refactor notifications plugin --- plugins/notifications/back.js | 49 ++++++++++------------------ plugins/notifications/interactive.js | 15 ++++++--- 2 files changed, 28 insertions(+), 36 deletions(-) diff --git a/plugins/notifications/back.js b/plugins/notifications/back.js index 91d3ea7288..59b934c949 100644 --- a/plugins/notifications/back.js +++ b/plugins/notifications/back.js @@ -3,7 +3,7 @@ const is = require("electron-is"); const registerCallback = require("../../providers/song-info"); const { notificationImage } = require("./utils"); -const { setupInteractive, notifyInteractive } = require("./interactive") +const setupInteractive = require("./interactive") const notify = (info, options) => { @@ -23,37 +23,22 @@ const notify = (info, options) => { return currentNotification; }; -module.exports = (win, options) => { - const isInteractive = is.windows() && options.interactive; - //setup interactive notifications for windows - if (isInteractive) { - setupInteractive(win, options.unpauseNotification); - } +const setup = (options) => { let oldNotification; - let oldURL = ""; - win.once("ready-to-show", () => { - // Register the callback for new song information - registerCallback(songInfo => { - // on pause - reset url? and skip notification - if (songInfo.isPaused) { - //reset oldURL if unpause notification option is on - if (options.unpauseNotification) { - oldURL = ""; - } - return; - } - // If url isn't the same as last one - send notification - if (songInfo.url !== oldURL) { - oldURL = songInfo.url; - if (isInteractive) { - notifyInteractive(songInfo); - } else { - // Close the old notification - oldNotification?.close(); - // This fixes a weird bug that would cause the notification to be updated instead of showing - setTimeout(() => { oldNotification = notify(songInfo, options) }, 10); - } - } - }); + + registerCallback(songInfo => { + if (!songInfo.isPaused || options.unpauseNotification) { + // Close the old notification + oldNotification?.close(); + // This fixes a weird bug that would cause the notification to be updated instead of showing + setTimeout(() => { oldNotification = notify(songInfo, options) }, 10); + } }); +} + +module.exports = (win, options) => { + // Register the callback for new song information + is.windows() && options.interactive ? + setupInteractive(win, options.unpauseNotification) : + setup(options); }; diff --git a/plugins/notifications/interactive.js b/plugins/notifications/interactive.js index cb487caea6..210402bf8f 100644 --- a/plugins/notifications/interactive.js +++ b/plugins/notifications/interactive.js @@ -1,18 +1,25 @@ const { notificationImage, icons } = require("./utils"); const getSongControls = require('../../providers/song-controls'); +const registerCallback = require("../../providers/song-info"); const notifier = require("node-notifier"); //store song controls reference on launch let controls; let notificationOnPause; -//Save controls and onPause option -module.exports.setupInteractive = (win, unpauseNotification) => { +module.exports = (win, unpauseNotification) => { + //Save controls and onPause option const { playPause, next, previous } = getSongControls(win); controls = { playPause, next, previous }; - notificationOnPause = unpauseNotification; + // Register songInfoCallback + registerCallback(songInfo => { + if (!songInfo.isPaused || notificationOnPause) { + sendToaster(songInfo); + } + }); + win.webContents.once("closed", () => { deleteNotification() }); @@ -33,7 +40,7 @@ function deleteNotification() { } //New notification -module.exports.notifyInteractive = function sendToaster(songInfo) { +function sendToaster(songInfo) { deleteNotification(); //download image and get path let imgSrc = notificationImage(songInfo, true); From b266037bb412e1d80dd496beb2c3007ecbab72b5 Mon Sep 17 00:00:00 2001 From: Araxeus Date: Mon, 10 May 2021 06:05:39 +0300 Subject: [PATCH 3/5] lint --- providers/song-info.js | 1 - 1 file changed, 1 deletion(-) diff --git a/providers/song-info.js b/providers/song-info.js index 97ddb0712d..1b119b827d 100644 --- a/providers/song-info.js +++ b/providers/song-info.js @@ -51,7 +51,6 @@ const songInfo = { }; const handleData = async (responseText, win) => { - console.log("handling song-info") let data = JSON.parse(responseText); songInfo.title = data?.videoDetails?.title; songInfo.artist = await getArtist(win) || cleanupArtistName(data?.videoDetails?.author); From 4fb0b1dd085a22f5c6bda67a000ad09071845a08 Mon Sep 17 00:00:00 2001 From: Araxeus Date: Wed, 19 May 2021 00:22:12 +0300 Subject: [PATCH 4/5] switch to `registerCallback()` on song info --- plugins/downloader/menu.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/plugins/downloader/menu.js b/plugins/downloader/menu.js index 9822c8ed43..ce7f56435b 100644 --- a/plugins/downloader/menu.js +++ b/plugins/downloader/menu.js @@ -2,13 +2,13 @@ const { existsSync, mkdirSync } = require("fs"); const { join } = require("path"); const { URL } = require("url"); -const { dialog, ipcMain } = require("electron"); +const { dialog } = require("electron"); const is = require("electron-is"); const ytpl = require("ytpl"); const chokidar = require('chokidar'); const { setOptions } = require("../../config/plugins"); -const getSongInfo = require("../../providers/song-info"); +const registerCallback = require("../../providers/song-info"); const { sendError } = require("./back"); const { defaultMenuDownloadLabel, getFolder } = require("./utils"); @@ -18,7 +18,6 @@ let callbackIsRegistered = false; module.exports = (win, options) => { if (!callbackIsRegistered) { - const registerCallback = getSongInfo(win); registerCallback((info) => { metadataURL = info.url; }); From 81246231426856b83d9bb1cfe90aba63d43ba10a Mon Sep 17 00:00:00 2001 From: Araxeus Date: Sat, 22 May 2021 18:38:36 +0300 Subject: [PATCH 5/5] fix notificationOnUnpause option --- plugins/notifications/back.js | 4 +++- plugins/notifications/interactive.js | 11 +++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/plugins/notifications/back.js b/plugins/notifications/back.js index 59b934c949..654e3b5499 100644 --- a/plugins/notifications/back.js +++ b/plugins/notifications/back.js @@ -25,11 +25,13 @@ const notify = (info, options) => { const setup = (options) => { let oldNotification; + let currentUrl; registerCallback(songInfo => { - if (!songInfo.isPaused || options.unpauseNotification) { + if (!songInfo.isPaused && (songInfo.url !== currentUrl || options.unpauseNotification)) { // Close the old notification oldNotification?.close(); + currentUrl = songInfo.url; // This fixes a weird bug that would cause the notification to be updated instead of showing setTimeout(() => { oldNotification = notify(songInfo, options) }, 10); } diff --git a/plugins/notifications/interactive.js b/plugins/notifications/interactive.js index 210402bf8f..c30c102070 100644 --- a/plugins/notifications/interactive.js +++ b/plugins/notifications/interactive.js @@ -5,17 +5,20 @@ const notifier = require("node-notifier"); //store song controls reference on launch let controls; -let notificationOnPause; +let notificationOnUnpause; module.exports = (win, unpauseNotification) => { //Save controls and onPause option const { playPause, next, previous } = getSongControls(win); controls = { playPause, next, previous }; - notificationOnPause = unpauseNotification; + notificationOnUnpause = unpauseNotification; + + let currentUrl; // Register songInfoCallback registerCallback(songInfo => { - if (!songInfo.isPaused || notificationOnPause) { + if (!songInfo.isPaused && (songInfo.url !== currentUrl || notificationOnUnpause)) { + currentUrl = songInfo.url; sendToaster(songInfo); } }); @@ -78,7 +81,7 @@ function sendToaster(songInfo) { // dont delete notification on play/pause toDelete = undefined; //manually send notification if not sending automatically - if (!notificationOnPause) { + if (!notificationOnUnpause) { songInfo.isPaused = false; sendToaster(songInfo); }