Skip to content
This repository has been archived by the owner on Jan 6, 2022. It is now read-only.

Commit

Permalink
Fixed Discord RPC (1.5.6)
Browse files Browse the repository at this point in the history
  • Loading branch information
cryptofyre committed Apr 27, 2021
1 parent 9081f71 commit 8e4bff7
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 103 deletions.
2 changes: 2 additions & 0 deletions assets/MusicKitInterop.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ const MusicKitInterop = {

getAttributes: function() {
const nowPlayingItem = MusicKit.getInstance().nowPlayingItem;
const isplaying = MusicKit.getInstance().isPlaying;
let attributes = {};

if (nowPlayingItem != null){
attributes = nowPlayingItem.attributes;
}
attributes.status = isplaying ? isplaying : false;
attributes.name = attributes.name ? attributes.name : 'No Title Found';
attributes.durationInMillis = attributes.durationInMillis ? attributes.durationInMillis : 0;
attributes.artwork = attributes.artwork ? attributes.artwork : {url: ''};
Expand Down
172 changes: 75 additions & 97 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,27 @@

const { app, BrowserWindow, globalShortcut, Tray, Menu} = require('electron')
require('v8-compile-cache');
const { app, BrowserWindow, Tray, Menu} = require('electron')
const electron = require('electron');
const path = require('path')
const fs = require('fs')
const nativeTheme = electron.nativeTheme;
const client = require('discord-rich-presence')('749317071145533440');
const { session } = require('electron')
const isReachable = require('is-reachable');
require('v8-compile-cache');
let currentPlayBackProgress
let isQuiting
let isMaximized

const filter = {
urls: ['https://beta.music.apple.com/','https://beta.music.apple.com/us/browse','https://music.apple.com/','https://music.apple.com/us/browse']
}

function createWindow () {
const win = new BrowserWindow({
icon: path.join(__dirname, './assets/icon.png'),
width: 1024,
height: 600,
minWidth: 300,
minHeight: 300,
frame: false,
frame: true,
title: "Apple Music",
// Enables DRM
// Enables DRM
webPreferences: {
plugins: true,
preload: path.join(__dirname, './assets/MusicKitInterop.js'),
allowRunningInsecureContent: true,
allowRunningInsecureContent: true
}
})

Expand All @@ -38,19 +30,18 @@ function createWindow () {

// Check for Apple Music Sites
async function betaOnline() {
return isReachable('https://beta.music.apple.com');
return isReachable('https://beta.music.apple.com');
}

var appleMusicUrl = "https://music.apple.com"
let appleMusicUrl = "https://music.apple.com";

if (betaOnline().catch == true)
{
var appleMusicUrl = "https://beta.music.apple.com"
}
if (betaOnline().catch === true) {
appleMusicUrl = "https://beta.music.apple.com";
}

win.loadURL(appleMusicUrl);

win.on('page-title-updated', function(e) {
win.on('page-title-updated', function (e) {
e.preventDefault()
});

Expand All @@ -63,12 +54,11 @@ function createWindow () {
}
});

// Hide iTunes prompt and other external buttons by Apple. Ensure deletion. Create Draggable div to act as title bar. Create close, min, and max buttons. (OSX style since this is *Apple* Music)
win.webContents.on('did-stop-loading', function() {
// Hide iTunes prompt and other external buttons by Apple. Ensure deletion. OPTIONAL: Create Draggable div to act as title bar. Create close, min, and max buttons. (OSX style since this is *Apple* Music)
win.webContents.on('did-stop-loading', function () {
win.webContents.executeJavaScript("const elements = document.getElementsByClassName('web-navigation__native-upsell'); while (elements.length > 0) elements[0].remove();");
win.webContents.executeJavaScript("while (elements.length > 0) elements[0].remove();");
win.webContents.executeJavaScript("if(document.getElementsByClassName('web-navigation')[0] && !(document.getElementsByClassName('web-navigation')[0].style.height == 'calc(100vh - 32px)')){ let dragDiv = document.createElement('div'); dragDiv.style.width = '100%'; dragDiv.style.height = '32px'; dragDiv.style.position = 'absolute'; dragDiv.style.top = dragDiv.style.left = 0; dragDiv.style.webkitAppRegion = 'drag'; document.body.appendChild(dragDiv); var closeButton = document.createElement('span'); document.getElementsByClassName('web-navigation')[0].style.height = 'calc(100vh - 32px)'; document.getElementsByClassName('web-navigation')[0].style.bottom = 0; document.getElementsByClassName('web-navigation')[0].style.position = 'absolute'; document.getElementsByClassName('web-chrome')[0].style.top = '32px'; var minimizeButton = document.createElement('span'); var maximizeButton = document.createElement('span'); document.getElementsByClassName('web-navigation')[0].style.height = 'calc(100vh - 32px)'; closeButton.style = 'height: 11px; width: 11px; background-color: rgb(255, 92, 92); border-radius: 50%; display: inline-block; left: 0px; top: 0px; margin: 10px 4px 10px 10px; color: rgb(130, 0, 5); fill: rgb(130, 0, 5); -webkit-app-region: no-drag; '; minimizeButton.style = 'height: 11px; width: 11px; background-color: rgb(255, 189, 76); border-radius: 50%; display: inline-block; left: 0px; top: 0px; margin: 10px 4px; color: rgb(130, 0, 5); fill: rgb(130, 0, 5); -webkit-app-region: no-drag;'; maximizeButton.style = 'height: 11px; width: 11px; background-color: rgb(0, 202, 86); border-radius: 50%; display: inline-block; left: 0px; top: 0px; margin: 10px 10px 10px 4px; color: rgb(130, 0, 5); fill: rgb(130, 0, 5); -webkit-app-region: no-drag;'; closeButton.onclick= window.close; minimizeButton.onclick = ()=>{ipcRenderer.send('minimize')}; maximizeButton.onclick = ()=>{ipcRenderer.send('maximize')}; dragDiv.appendChild(closeButton); dragDiv.appendChild(minimizeButton); dragDiv.appendChild(maximizeButton); closeButton.onmouseenter = ()=>{closeButton.style.filter = 'brightness(50%)'}; minimizeButton.onmouseenter = ()=>{minimizeButton.style.filter = 'brightness(50%)'}; maximizeButton.onmouseenter = ()=>{maximizeButton.style.filter = 'brightness(50%)'}; closeButton.onmouseleave = ()=>{closeButton.style.filter = 'brightness(100%)'}; minimizeButton.onmouseleave = ()=>{minimizeButton.style.filter = 'brightness(100%)'}; maximizeButton.onmouseleave = ()=>{maximizeButton.style.filter = 'brightness(100%)'};}")

// win.webContents.executeJavaScript("if(document.getElementsByClassName('web-navigation')[0] && !(document.getElementsByClassName('web-navigation')[0].style.height == 'calc(100vh - 32px)')){ let dragDiv = document.createElement('div'); dragDiv.style.width = '100%'; dragDiv.style.height = '32px'; dragDiv.style.position = 'absolute'; dragDiv.style.top = dragDiv.style.left = 0; dragDiv.style.webkitAppRegion = 'drag'; document.body.appendChild(dragDiv); var closeButton = document.createElement('span'); document.getElementsByClassName('web-navigation')[0].style.height = 'calc(100vh - 32px)'; document.getElementsByClassName('web-navigation')[0].style.bottom = 0; document.getElementsByClassName('web-navigation')[0].style.position = 'absolute'; document.getElementsByClassName('web-chrome')[0].style.top = '32px'; var minimizeButton = document.createElement('span'); var maximizeButton = document.createElement('span'); document.getElementsByClassName('web-navigation')[0].style.height = 'calc(100vh - 32px)'; closeButton.style = 'height: 11px; width: 11px; background-color: rgb(255, 92, 92); border-radius: 50%; display: inline-block; left: 0px; top: 0px; margin: 10px 4px 10px 10px; color: rgb(130, 0, 5); fill: rgb(130, 0, 5); -webkit-app-region: no-drag; '; minimizeButton.style = 'height: 11px; width: 11px; background-color: rgb(255, 189, 76); border-radius: 50%; display: inline-block; left: 0px; top: 0px; margin: 10px 4px; color: rgb(130, 0, 5); fill: rgb(130, 0, 5); -webkit-app-region: no-drag;'; maximizeButton.style = 'height: 11px; width: 11px; background-color: rgb(0, 202, 86); border-radius: 50%; display: inline-block; left: 0px; top: 0px; margin: 10px 10px 10px 4px; color: rgb(130, 0, 5); fill: rgb(130, 0, 5); -webkit-app-region: no-drag;'; closeButton.onclick= window.close; minimizeButton.onclick = ()=>{ipcRenderer.send('minimize')}; maximizeButton.onclick = ()=>{ipcRenderer.send('maximize')}; dragDiv.appendChild(closeButton); dragDiv.appendChild(minimizeButton); dragDiv.appendChild(maximizeButton); closeButton.onmouseenter = ()=>{closeButton.style.filter = 'brightness(50%)'}; minimizeButton.onmouseenter = ()=>{minimizeButton.style.filter = 'brightness(50%)'}; maximizeButton.onmouseenter = ()=>{maximizeButton.style.filter = 'brightness(50%)'}; closeButton.onmouseleave = ()=>{closeButton.style.filter = 'brightness(100%)'}; minimizeButton.onmouseleave = ()=>{minimizeButton.style.filter = 'brightness(100%)'}; maximizeButton.onmouseleave = ()=>{maximizeButton.style.filter = 'brightness(100%)'};}")
});

// Fix those ugly scrollbars and also execute MusicKitInterop.
Expand All @@ -78,114 +68,102 @@ function createWindow () {
})

// create system tray icon
if(process.platform == 'win32') trayIcon = new Tray(path.join(__dirname, './assets/icon.ico'))
if (process.platform === 'win32') var trayIcon = new Tray(path.join(__dirname, './assets/icon.ico'))
else trayIcon = new Tray(path.join(__dirname, './assets/icon.png'))

// right click menu to quit and show app
var contextMenu = Menu.buildFromTemplate([
{ label: 'Show Window', click: function(){
const contextMenu = Menu.buildFromTemplate([
{
label: 'Show Window', click: function () {
win.show();
} },
{ label: 'Quit Apple Music', click: function(){
}
},
{
label: 'Quit Apple Music', click: function () {
app.isQuiting = true;
app.quit();
} }
}
}
]);
trayIcon.setContextMenu(contextMenu);

// restore app on normal click
trayIcon.on('click', ()=>{
trayIcon.on('click', () => {
win.show()
})

// listen for minimize event
electron.ipcMain.on('minimize', ()=>{
electron.ipcMain.on('minimize', () => {
win.minimize()
})

// listen for maximize event and perform restore/maximize depending on window state
electron.ipcMain.on('maximize', ()=>{
if(isMaximized){
electron.ipcMain.on('maximize', () => {
if (isMaximized) {
win.restore()
isMaximized = false
}
else{
win.maximize()
isMaximized = true
} else {
win.maximize()
isMaximized = true
}
})

// Insert Jarek Toros amazing work with MusicKit and Mpris (https://github.com/JarekToro/Apple-Music-Mpris/) (NOTE: Mpris is not enabled in this branch. See mpris-enabled)!
electron.ipcMain.on('mediaItemStateDidChange', (item, a) => {
updateMetaData(a)
updateTooltip(a)
})

async function updateTooltip(attributes){
electron.ipcMain.on('mediaItemStateDidChange', (item, a) => {
updateMetaData(a)
updateTooltip(a)
})

// Update tooltip when audio is playing.
win.webContents.on('media-started-playing',()=> {
var tooltip = `Playing ${attributes.name} - ${attributes.albumName} by ${attributes.artistName}`
trayIcon.setToolTip(tooltip);
})
async function updateTooltip(attributes) {

// Update tooltip when audio is paused
win.webContents.on('media-paused', () => {
var tooltip = `Paused ${attributes.name} on ${attributes.albumName} by ${attributes.artistName}`
trayIcon.setToolTip(tooltip)
})
// Update tooltip when audio is playing.
win.webContents.on('media-started-playing', () => {
const tooltip = `Playing ${attributes.name} - ${attributes.albumName} by ${attributes.artistName}`;
trayIcon.setToolTip(tooltip);
})

// Start tooltip with idle name
var tooltip = `Nothing's playing right now`
trayIcon.setToolTip(tooltip);
}
// Update tooltip when audio is paused
win.webContents.on('media-paused', () => {
const tooltip = `Paused ${attributes.name} on ${attributes.albumName} by ${attributes.artistName}`;
trayIcon.setToolTip(tooltip)
})

async function getMusicKitAttributes() {
return await win.webContents.executeJavaScript(`MusicKitInterop.getAttributes()`);
}
// Start tooltip with idle name
const tooltip = `Nothing's playing right now`;
trayIcon.setToolTip(tooltip);
}

async function updateMetaData(attributes) {

var songlengthstring = Math.round(attributes.durationInMillis + Date.now())
var songlength = Number(songlengthstring)
// Update rich presence when audio is playing.
win.webContents.on('media-started-playing', function() {
client.updatePresence({
state: `${attributes.albumName} - ${attributes.artistName}`,
details: `${attributes.name}`,
startTimestamp: Date.now(),
endTimestamp: songlength,
largeImageKey: 'apple',
smallImageKey: 'play',
instance: true,
});
});

// Update rich presence when audio is paused or turned off.
win.webContents.on('media-paused', function() {
client.updatePresence({
state: "(Paused)",
details: `${attributes.name}`,
startTimestamp: Date.now(),
endTimestamp: Date.now(),
largeImageKey: 'apple',
smallImageKey: 'pause',
instance: true,
});
});

// Start rich presence service into idle mode.
async function updateMetaData(attributes) {

var songlengthstring = Math.round(attributes.durationInMillis + Date.now());
var songlength = Number(songlengthstring);

// Update rich presence when audio is playing
win.webContents.on('media-started-playing', () => {
client.updatePresence({
state: `${attributes.albumName} - ${attributes.artistName}`,
details: `${attributes.name}`,
startTimestamp: Date.now(),
endTimestamp: songlength,
largeImageKey: 'apple',
smallImageKey: 'play',
instance: true,
});
});
win.webContents.on('media-paused', () => {
if (attributes.status === false) {
client.updatePresence({
state: '(Nothing has played)',
details: 'Music Stopped',
state: "(Paused)",
details: `${attributes.name}`,
startTimestamp: Date.now(),
endTimestamp: Date.now(),
largeImageKey: 'apple',
smallImageKey: 'stop',
smallImageKey: 'pause',
instance: true,
});

}
});
}
}

// This argument is for Linux operating systems that dont support the CSS theme preference.
Expand All @@ -194,7 +172,7 @@ nativeTheme.themeSource = 'dark';
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(createWindow)
app.on('widevine-ready', createWindow)

app.on('window-all-closed', () => {
app.quit()
Expand Down
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"homepage": "https://github.com/iiFir3z/Apple-Music-Electron",
"name": "apple-music-electron",
"productName": "Apple Music",
"version": "1.5.5",
"version": "1.5.6",
"description": "Unofficial Electron wrapper for Apple Music created by @cryptofyre",
"license": "MIT",
"buildResources": "assets",
Expand All @@ -18,20 +18,21 @@
"discord-rich-presence": "^0.0.8",
"discord-rpc": "^3.1.4",
"electron-updater": "^4.3.5",
"is-reachable": "^5.0.0",
"node-fetch": "^2.6.1",
"source-map-support": "^0.5.16",
"v8-compile-cache": "^2.1.1",
"is-reachable": "^5.0.0"
"v8-compile-cache": "^2.1.1"
},
"devDependencies": {
"electron": "https://github.com/castlabs/electron-releases.git",
"electron": "https://github.com/cryptofyre/castlabs-electron12.git",
"electron-builder": "^22.7.0",
"electron-webpack": "^2.8.2",
"electron-webpack": "^2.7.4",
"musickit-typescript": "^1.2.4",
"webpack": "~4.42.1"
},
"build": {
"electronDownload": {
"mirror": "https://github.com/castlabs/electron-releases/releases/download/v"
"mirror": "https://github.com/cryptofyre/castlabs-electron12/releases/download/v"
},
"appId": "apple-music",
"extends": null,
Expand Down

0 comments on commit 8e4bff7

Please sign in to comment.