From dbfddebbc20b8e94824cfe6691c0f70dd08fdb42 Mon Sep 17 00:00:00 2001 From: xhayper Date: Thu, 12 Jan 2023 01:41:50 +0700 Subject: [PATCH] feat: auto reconnect rpc and CSP fix --- config/defaults.js | 3 +- index.js | 11 +++++- plugins/discord/back.js | 75 +++++++++++++++++++++++++++-------------- plugins/discord/menu.js | 9 +++++ 4 files changed, 71 insertions(+), 27 deletions(-) diff --git a/config/defaults.js b/config/defaults.js index 91bddf24..329f3851 100644 --- a/config/defaults.js +++ b/config/defaults.js @@ -46,6 +46,7 @@ const defaultConfig = { }, discord: { enabled: false, + autoReconnect: true, // if enabled, will try to reconnect to discord every 5 seconds after disconnecting or failing to connect activityTimoutEnabled: true, // if enabled, the discord rich presence gets cleared when music paused after the time specified below activityTimoutTime: 10 * 60 * 1000, // 10 minutes listenAlong: true, // add a "listen along" button to rich presence @@ -54,7 +55,7 @@ const defaultConfig = { notifications: { enabled: false, unpauseNotification: false, - urgency: "normal", //has effect only on Linux + urgency: "normal", //has effect only on Linux interactive: false //has effect only on Windows }, "precise-volume": { diff --git a/index.js b/index.js index 7e4e5980..d59d5648 100644 --- a/index.js +++ b/index.js @@ -189,7 +189,7 @@ function createMainWindow() { win.webContents.loadURL(urlToLoad); win.on("closed", onClosed); - const setPiPOptions = config.plugins.isEnabled("picture-in-picture") + const setPiPOptions = config.plugins.isEnabled("picture-in-picture") ? (key, value) => require("./plugins/picture-in-picture/back").setOptions({ [key]: value }) : () => {}; @@ -258,6 +258,15 @@ function createMainWindow() { } app.once("browser-window-created", (event, win) => { + electron.session.defaultSession.webRequest.onHeadersReceived((details, callback) => { + callback({ + responseHeaders: { + ...details.responseHeaders, + "Content-Security-Policy": ["default-src 'self' music.youtube.com youtube.com accounts.google.com google.com"] + } + }) + }) + if (config.get("options.overrideUserAgent")) { // User agents are from https://developers.whatismybrowser.com/useragents/explore/ const originalUserAgent = win.webContents.userAgent; diff --git a/plugins/discord/back.js b/plugins/discord/back.js index d1ff3a01..1afbd09b 100644 --- a/plugins/discord/back.js +++ b/plugins/discord/back.js @@ -1,4 +1,4 @@ -const Discord = require("discord-rpc"); +const Discord = require("@xhayper/discord-rpc"); const { dev } = require("electron-is"); const { dialog, app } = require("electron"); @@ -9,58 +9,81 @@ const clientId = "1043858434585526382"; /** * @typedef {Object} Info - * @property {import('discord-rpc').Client} rpc + * @property {import('@xhayper/discord-rpc').Client} rpc * @property {boolean} ready + * @property {boolean} autoReconnect * @property {import('../../providers/song-info').SongInfo} lastSongInfo */ /** * @type {Info} */ const info = { - rpc: null, + rpc: new Discord.Client({ + clientId + }), ready: false, + autoReconnect: true, lastSongInfo: null, }; + /** * @type {(() => void)[]} */ const refreshCallbacks = []; + const resetInfo = () => { - info.rpc = null; info.ready = false; clearTimeout(clearActivity); if (dev()) console.log("discord disconnected"); refreshCallbacks.forEach(cb => cb()); }; +info.rpc.on("connected", () => { + if (dev()) console.log("discord connected"); + refreshCallbacks.forEach(cb => cb()); +}); + +info.rpc.on("ready", () => { + info.ready = true; + if (info.lastSongInfo) updateActivity(info.lastSongInfo) +}); + +info.rpc.on("disconnected", () => { + resetInfo(); + + if (info.autoReconnect) { + connectTimeout(); + } +}); + +const connectTimeout = () => new Promise((resolve, reject) => setTimeout(() => { + if (!info.autoReconnect || info.rpc.isConnected) return; + info.rpc.login().then(resolve).catch(reject); +}, 5000)); + +const connectRecursive = () => { + if (!info.autoReconnect || info.rpc.isConnected) return; + connectTimeout().catch(connectRecursive); +} + let window; const connect = (showErr = false) => { - if (info.rpc) { + if (info.rpc.isConnected) { if (dev()) - console.log('Attempted to connect with active RPC object'); + console.log('Attempted to connect with active connection'); return; } - info.rpc = new Discord.Client({ - transport: "ipc", - }); info.ready = false; - info.rpc.once("connected", () => { - if (dev()) console.log("discord connected"); - refreshCallbacks.forEach(cb => cb()); - }); - info.rpc.once("ready", () => { - info.ready = true; - if (info.lastSongInfo) updateActivity(info.lastSongInfo) - }); - info.rpc.once("disconnected", resetInfo); - // Startup the rpc client info.rpc.login({ clientId }).catch(err => { resetInfo(); if (dev()) console.error(err); - if (showErr) dialog.showMessageBox(window, { title: 'Connection failed', message: err.message || String(err), type: 'error' }); + if (info.autoReconnect) { + connectRecursive(); + } + else if (showErr) dialog.showMessageBox(window, { title: 'Connection failed', message: err.message || String(err), type: 'error' }); }); }; @@ -70,7 +93,9 @@ let clearActivity; */ let updateActivity; -module.exports = (win, { activityTimoutEnabled, activityTimoutTime, listenAlong, hideDurationLeft }) => { +module.exports = (win, { autoReconnect, activityTimoutEnabled, activityTimoutTime, listenAlong, hideDurationLeft }) => { + info.autoReconnect = autoReconnect; + window = win; // We get multiple events // Next song: PAUSE(n), PAUSE(n+1), PLAY(n+1) @@ -92,7 +117,7 @@ module.exports = (win, { activityTimoutEnabled, activityTimoutTime, listenAlong, // clear directly if timeout is 0 if (songInfo.isPaused && activityTimoutEnabled && activityTimoutTime === 0) { - info.rpc.clearActivity().catch(console.error); + info.rpc.user?.clearActivity().catch(console.error); return; } @@ -100,7 +125,6 @@ module.exports = (win, { activityTimoutEnabled, activityTimoutTime, listenAlong, // @see https://discord.com/developers/docs/topics/gateway#activity-object // not all options are transfered through https://github.com/discordjs/RPC/blob/6f83d8d812c87cb7ae22064acd132600407d7d05/src/client.js#L518-530 const activityInfo = { - type: 2, // Listening, addressed in https://github.com/discordjs/RPC/pull/149 details: songInfo.title, state: songInfo.artist, largeImageKey: songInfo.imageSrc, @@ -116,7 +140,7 @@ module.exports = (win, { activityTimoutEnabled, activityTimoutTime, listenAlong, activityInfo.smallImageText = "Paused"; // Set start the timer so the activity gets cleared after a while if enabled if (activityTimoutEnabled) - clearActivity = setTimeout(() => info.rpc.clearActivity().catch(console.error), activityTimoutTime ?? 10000); + clearActivity = setTimeout(() => info.rpc.user?.clearActivity().catch(console.error), activityTimoutTime ?? 10000); } else if (!hideDurationLeft) { // Add the start and end time of the song const songStartTime = Date.now() - songInfo.elapsedSeconds * 1000; @@ -125,7 +149,7 @@ module.exports = (win, { activityTimoutEnabled, activityTimoutTime, listenAlong, songStartTime + songInfo.songDuration * 1000; } - info.rpc.setActivity(activityInfo).catch(console.error); + info.rpc.user?.setActivity(activityInfo).catch(console.error); }; // If the page is ready, register the callback @@ -140,6 +164,7 @@ module.exports.clear = () => { if (info.rpc) info.rpc.clearActivity(); clearTimeout(clearActivity); }; + module.exports.connect = connect; module.exports.registerRefresh = (cb) => refreshCallbacks.push(cb); module.exports.isConnected = () => info.rpc !== null; diff --git a/plugins/discord/menu.js b/plugins/discord/menu.js index 5ce72704..2853a502 100644 --- a/plugins/discord/menu.js +++ b/plugins/discord/menu.js @@ -18,6 +18,15 @@ module.exports = (win, options, refreshMenu) => { enabled: !isConnected(), click: connect, }, + { + label: "Auto reconnect", + type: "checkbox", + checked: options.autoReconnect, + click: (item) => { + options.autoReconnect = item.checked; + setMenuOptions('discord', options); + }, + }, { label: "Clear activity", click: clear,