diff --git a/index.js b/index.js index 40d8fe37..9de11d37 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,5 @@ require('v8-compile-cache'); -const { app, screen } = require('electron'); +const { app } = require('electron'); // Analytics for debugging. const ElectronSentry = require("@sentry/electron"); @@ -72,6 +72,10 @@ function CreateWindow() { /** CIDER **/ } +if (process.platform === "linux") { + app.commandLine.appendSwitch('disable-features', 'MediaSessionService'); +} + /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * App Event Handlers diff --git a/src/main/cider-base.js b/src/main/cider-base.js index 37950369..f8613d86 100644 --- a/src/main/cider-base.js +++ b/src/main/cider-base.js @@ -5,10 +5,9 @@ const express = require("express"); const path = require("path"); const windowStateKeeper = require("electron-window-state"); const os = require('os'); -const Store = require("electron-store"); -const store = new Store(); const yt = require('youtube-search-without-api-key'); const discord = require('./discordrpc'); +const mpris = require('./mpris'); // Analytics for debugging. const ElectronSentry = require("@sentry/electron"); @@ -16,7 +15,7 @@ ElectronSentry.init({dsn: "https://68c422bfaaf44dea880b86aad5a820d2@o954055.inge const CiderBase = { async Start() { - this.clientPort = await getPort({port : 9000}); + this.clientPort = await getPort({port: 9000}); this.CreateBrowserWindow() }, clientPort: 0, @@ -39,7 +38,7 @@ const CiderBase = { frame: false, title: "Cider", vibrancy: 'dark', - // transparent: true, + // transparent: true, hasShadow: false, webPreferences: { webviewTag: true, @@ -84,15 +83,15 @@ const CiderBase = { } ) - win.webContents.session.webRequest.onBeforeSendHeaders(async (details, callback) => { - if(details.url == "https://buy.itunes.apple.com/account/web/info"){ + win.webContents.session.webRequest.onBeforeSendHeaders(async (details, callback) => { + if (details.url === "https://buy.itunes.apple.com/account/web/info") { details.requestHeaders['sec-fetch-site'] = 'same-site'; details.requestHeaders['DNT'] = '1'; - let itspod = await win.webContents.executeJavaScript(`window.localStorage.getItem("music.ampwebplay.itspod")`) + let itspod = await win.webContents.executeJavaScript(`window.localStorage.getItem("music.ampwebplay.itspod")`) if (itspod != null) - details.requestHeaders['Cookie'] = `itspod=${itspod}` + details.requestHeaders['Cookie'] = `itspod=${itspod}` } - callback({ requestHeaders: details.requestHeaders }) + callback({requestHeaders: details.requestHeaders}) }) let location = `http://localhost:${CiderBase.clientPort}/` @@ -124,7 +123,7 @@ const CiderBase = { ipcMain.handle('getYTLyrics', async (event, track, artist) => { var u = track + " " + artist + " official video"; - const videos = await yt.search(u); + const videos = await yt.search(u); return videos }) @@ -187,9 +186,10 @@ const CiderBase = { // Set window Handler win.webContents.setWindowOpenHandler(({url}) => { if (url.includes("apple") || url.includes("localhost")) { - return { action: "allow"} + return {action: "allow"} } - shell.openExternal(url).catch(() => {}) + shell.openExternal(url).catch(() => { + }) return { action: 'deny' } @@ -198,17 +198,21 @@ const CiderBase = { // Set scale ipcMain.on('setScreenScale', (event, scale) => { win.webContents.setZoomFactor(parseFloat(scale)) - }) + }) win.webContents.setZoomFactor(screen.getPrimaryDisplay().scaleFactor) + mpris.connect(win) + // Discord - discord.connect('911790844204437504'); + discord.connect('911790844204437504'); ipcMain.on('playbackStateDidChange', (_event, a) => { discord.updateActivity(a) + mpris.updateState(a) }); ipcMain.on('nowPlayingItemDidChange', (_event, a) => { - discord.updateActivity(a) + discord.updateActivity(a) + mpris.updateAttributes(a) }); return win diff --git a/src/main/discordrpc.js b/src/main/discordrpc.js index f346d9f5..bb084b12 100644 --- a/src/main/discordrpc.js +++ b/src/main/discordrpc.js @@ -2,16 +2,21 @@ const {app} = require('electron'), DiscordRPC = require('discord-rpc') module.exports = { + + /** + * Connects to Discord RPC + * @param {string} clientId + */ connect: function (clientId) { app.discord = {isConnected: false}; if (!app.cfg.get('general.discord_rpc')) return; DiscordRPC.register(clientId) // Apparently needed for ask to join, join, spectate etc. - const client = new DiscordRPC.Client({ transport: "ipc" }); - app.discord = Object.assign(client,{error: false, activityCache: null, isConnected: false}); + const client = new DiscordRPC.Client({transport: "ipc"}); + app.discord = Object.assign(client, {error: false, activityCache: null, isConnected: false}); // Login to Discord - app.discord.login({ clientId }) + app.discord.login({clientId}) .then(() => { app.discord.isConnected = true; }) @@ -34,6 +39,9 @@ module.exports = { }); }, + /** + * Disconnects from Discord RPC + */ disconnect: function () { if (!app.cfg.get('general.discord_rpc') || !app.discord.isConnected) return; @@ -47,6 +55,10 @@ module.exports = { } }, + /** + * Sets the activity of the client + * @param {object} attributes + */ updateActivity: function (attributes) { if (!app.cfg.get('general.discord_rpc')) return; @@ -65,16 +77,18 @@ module.exports = { state: `by ${attributes.artistName}`, startTimestamp: attributes.startTime, endTimestamp: attributes.endTime, - largeImageKey: (attributes.artwork.url.replace('{w}', '512').replace('{h}', '512') ) ?? 'cider', + largeImageKey: (attributes.artwork.url.replace('{w}', '512').replace('{h}', '512')) ?? 'cider', largeImageText: attributes.albumName, smallImageKey: (attributes.status ? 'play' : 'pause'), - smallImageText: (attributes.status ? 'Playing': 'Paused'), + smallImageText: (attributes.status ? 'Playing' : 'Paused'), instance: true, buttons: [ {label: "Listen on Cider", url: listenURL}, ] }; - if (ActivityObject.largeImageKey == "" || ActivityObject.largeImageKey == null) {ActivityObject.largeImageKey = "cider"} + if (ActivityObject.largeImageKey == "" || ActivityObject.largeImageKey == null) { + ActivityObject.largeImageKey = "cider" + } //console.log(`[LinkHandler] Listening URL has been set to: ${listenURL}`); // if (app.cfg.get('general.discordClearActivityOnPause')) { @@ -104,16 +118,16 @@ module.exports = { // ActivityObject = null // } else // { - delete ActivityObject.startTimestamp - delete ActivityObject.endTimestamp - ActivityObject.smallImageKey = 'pause' - ActivityObject.smallImageText = 'Paused' + delete ActivityObject.startTimestamp + delete ActivityObject.endTimestamp + ActivityObject.smallImageKey = 'pause' + ActivityObject.smallImageText = 'Paused' //} } if (ActivityObject) { try { - // console.log(`[DiscordRPC][setActivity] Setting activity to ${JSON.stringify(ActivityObject)}`); + // console.log(`[DiscordRPC][setActivity] Setting activity to ${JSON.stringify(ActivityObject)}`); app.discord.setActivity(ActivityObject) } catch (err) { console.error(`[DiscordRPC][setActivity] ${err}`) diff --git a/src/main/init.js b/src/main/init.js deleted file mode 100644 index 69f2a796..00000000 --- a/src/main/init.js +++ /dev/null @@ -1,5 +0,0 @@ -const {app} = require('electron'); - -module.export = () => { - if (process.platform === "linux") app.commandLine.appendSwitch('disable-features', 'MediaSessionService'); -} \ No newline at end of file diff --git a/src/main/mpris.js b/src/main/mpris.js index cd7d2a73..3c92e322 100644 --- a/src/main/mpris.js +++ b/src/main/mpris.js @@ -1,16 +1,16 @@ -const {app} = require('electron'), - Player = require('mpris-service'); - -// Remember to use playerctl when debugging this. -// I'm just putting this here as I keep forgetting the command. -// Copied from AME - -let mediaPlayer; +let mediaPlayer = null; module.exports = { + + /** + * Connects to the MPRIS interface. + * @param {Object} win - The BrowserWindow. + */ connect: (win) => { if (process.platform !== "linux") return; + const Player = require('mpris-service'); + mediaPlayer = Player({ name: 'Cider', identity: 'Cider', @@ -24,7 +24,7 @@ module.exports = { let pos_atr = {durationInMillis: 0}; mediaPlayer.getPosition = function () { const durationInMicro = pos_atr.durationInMillis * 1000; - const percentage = parseFloat(0) || 0; + const percentage = parseFloat("0") || 0; return durationInMicro * percentage; } @@ -51,6 +51,10 @@ module.exports = { }); }, + /** + * Updates the MPRIS interface. + * @param {Object} attributes - The attributes of the track. + */ updateAttributes: (attributes) => { if (process.platform !== "linux") return; @@ -71,6 +75,10 @@ module.exports = { mediaPlayer.metadata = MetaData }, + /** + * Updates the playback state of the MPRIS interface. + * @param {Object} attributes - The attributes of the track. + */ updateState: (attributes) => { if (process.platform !== "linux") return; @@ -94,6 +102,9 @@ module.exports = { } }, + /** + * Closes the MPRIS interface. + */ clearActivity: () => { if (process.platform !== "linux") return; mediaPlayer.metadata = {'mpris:trackid': '/org/mpris/MediaPlayer2/TrackList/NoTrack'}