From 5aef18e4b33483f048c3ca1b74ebaee8b56b6799 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Opekar?= Date: Wed, 19 Apr 2023 15:05:43 +0200 Subject: [PATCH 1/2] Feature: Adds integration of topik/youtube-music-obs-widget as plugin --- plugins/obs-widget/back.js | 56 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 plugins/obs-widget/back.js diff --git a/plugins/obs-widget/back.js b/plugins/obs-widget/back.js new file mode 100644 index 0000000000..d721d58b10 --- /dev/null +++ b/plugins/obs-widget/back.js @@ -0,0 +1,56 @@ +const {ipcMain} = require("electron"); +const http = require("http"); +const registerCallback = require("../../providers/song-info"); + +let currentSongInfo; +const data = { + player: { + hasSong: false, + isPaused: true, + seekbarCurrentPosition: 0, + }, + track: { + author: "", + title: "", + album: "", + cover: "", + duration: 0, + } +}; + +/** @param {Electron.BrowserWindow} win */ +module.exports = async (win) => { + const requestListener = function (req, res) { + if(req.url !== '/query'){ + res.writeHead(404); + res.end("404 Not found!"); + } + res.setHeader("Access-Control-Allow-Origin", "*"); + res.writeHead(200); + if (currentSongInfo !== undefined && !currentSongInfo.title && !currentSongInfo.artist) { + res.end(JSON.stringify({})); + return; + } + data.player.hasSong = true; + data.player.isPaused = currentSongInfo.isPaused; + data.player.seekbarCurrentPosition = currentSongInfo.elapsedSeconds; + data.track.author = [currentSongInfo.artist]; + data.track.title = currentSongInfo.title; + data.track.album = currentSongInfo.album; + data.track.cover = currentSongInfo.imageSrc; + data.track.duration = currentSongInfo.songDuration; + res.end(JSON.stringify(data)); + }; + const server = http.createServer(requestListener); + server.listen(9863, "localhost", () => { + }); + ipcMain.on('apiLoaded', () => win.webContents.send('setupTimeChangedListener')); + ipcMain.on('timeChanged', async (_, t) => { + if (!currentSongInfo.title || currentSongInfo.isPaused) return; + currentSongInfo.elapsedSeconds = t; + }); + + registerCallback((songInfo) => { + currentSongInfo = songInfo; + }); +}; From 18d6b3b1ff9fa3982d4d412e9363e3a9b29b7c82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Opekar?= Date: Fri, 21 Jul 2023 16:43:22 +0200 Subject: [PATCH 2/2] Apply suggestions from code review by @ArjixWasTaken Co-authored-by: Angelos Bouklis <53124886+ArjixWasTaken@users.noreply.github.com> --- plugins/obs-widget/back.js | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/plugins/obs-widget/back.js b/plugins/obs-widget/back.js index d721d58b10..1282c9edb0 100644 --- a/plugins/obs-widget/back.js +++ b/plugins/obs-widget/back.js @@ -10,7 +10,7 @@ const data = { seekbarCurrentPosition: 0, }, track: { - author: "", + author: [], title: "", album: "", cover: "", @@ -31,14 +31,20 @@ module.exports = async (win) => { res.end(JSON.stringify({})); return; } - data.player.hasSong = true; - data.player.isPaused = currentSongInfo.isPaused; - data.player.seekbarCurrentPosition = currentSongInfo.elapsedSeconds; - data.track.author = [currentSongInfo.artist]; - data.track.title = currentSongInfo.title; - data.track.album = currentSongInfo.album; - data.track.cover = currentSongInfo.imageSrc; - data.track.duration = currentSongInfo.songDuration; + Object.assign(data, { + player: { + hasSong: true, + isPaused: currentSongInfo.isPaused, + seekbarCurrentPosition: currentSongInfo.elapsedSeconds + }, + track: { + author: [currentSongInfo.artist], + title: currentSongInfo.title, + album: currentSongInfo.album, + cover: currentSongInfo.cover, + duration: currentSongInfo.duration + } + }); res.end(JSON.stringify(data)); }; const server = http.createServer(requestListener);