This commit is contained in:
Core 2021-12-28 20:50:31 +00:00
parent a04cbccc05
commit 22462e4d0e
7 changed files with 344 additions and 304 deletions

48
src/main/base/store.js Normal file
View file

@ -0,0 +1,48 @@
const Store = require("electron-store"),
{app} = require("electron");
module.exports = {
defaults: {
"general": {
"close_behavior": 0, // 0 = close, 1 = minimize, 2 = minimize to tray
"startup_behavior": 0, // 0 = nothing, 1 = open on startup
"discord_rpc": 1, // 0 = disabled, 1 = enabled as Cider, 2 = enabled as Apple Music
"discordClearActivityOnPause" : 0, // 0 = disabled, 1 = enabled
"volume": 1
},
"audio": {
"quality": "extreme",
"seamless_audio": true
},
"visual": {
"theme": "",
"scrollbars": 0, // 0 = show on hover, 2 = always hide, 3 = always show
"refresh_rate": 0,
"animated_artwork": "always", // 0 = always, 1 = limited, 2 = never
"animated_artwork_qualityLevel": 1,
"hw_acceleration": "default", // default, webgpu, disabled
"window_transparency": "default"
},
"lyrics": {
"enable_mxm": false,
"mxm_karaoke" : false,
"mxm_language": "en",
"enable_yt": false,
},
"lastfm": {
"enabled": false,
"scrobble_after": 30,
"auth_token": "",
"enabledRemoveFeaturingArtists" : true,
"NowPlaying": "true"
}
},
init() {
app.cfg = new Store({
defaults: this.defaults,
});
}
}

257
src/main/base/win.js Normal file
View file

@ -0,0 +1,257 @@
const {join} = require("path"),
{ipcMain, app, shell, screen} = require("electron"),
express = require("express"),
path = require("path"),
getPort = require("get-port"),
yt = require("youtube-search-without-api-key"),
os = require("os");
module.exports = {
browserWindow: {},
clientPort: await getPort({port: 9000}),
EnvironmentVariables: {
"env": {
platform: os.platform(),
dev: app.isPackaged
}
},
//-------------------------------------------------------------------------------
// Public Methods
//-------------------------------------------------------------------------------
/**
* Creates the BrowserWindow for the application.
* @return {object} Window
*/
createBrowserWindow() {
const windowStateKeeper = require("electron-window-state"),
BrowserWindow = require((process.platform === "win32") ? "electron-acrylic-window" : "electron").BrowserWindow;
const windowState = windowStateKeeper({
defaultWidth: 1024,
defaultHeight: 600
});
this.browserWindow = new BrowserWindow({
icon: join(__dirname, `../../../resources/icons/icon.ico`),
width: windowState.width,
height: windowState.height,
x: windowState.x,
y: windowState.y,
minWidth: 844,
minHeight: 410,
frame: false,
title: "Cider",
vibrancy: 'dark',
transparent: process.platform === "darwin",
hasShadow: false,
webPreferences: {
webviewTag: true,
plugins: true,
nodeIntegration: true,
nodeIntegrationInWorker: false,
webSecurity: false,
allowRunningInsecureContent: true,
enableRemoteModule: true,
sandbox: true,
nativeWindowOpen: true,
contextIsolation: false,
preload: join(__dirname, '../../preload/cider-preload.js')
}
})
this.initializeWebServer();
this.initializeSession();
this.initializeHandlers();
windowState.manage(this.browserWindow);
this.browserWindow.webContents.setZoomFactor(screen.getPrimaryDisplay().scaleFactor)
return this.browserWindow
},
/**
* Initializes the BrowserWindow handlers for the application.
*/
initializeHandlers() {
const self = this;
this.browserWindow.on('closed', () => {
this.browserWindow = null;
});
if (process.platform === "win32") {
let WND_STATE = {
MINIMIZED: 0,
NORMAL: 1,
MAXIMIZED: 2,
FULL_SCREEN: 3
}
let wndState = WND_STATE.NORMAL
self.browserWindow.on("resize", (_event) => {
const isMaximized = self.browserWindow.isMaximized()
const isMinimized = self.browserWindow.isMinimized()
const isFullScreen = self.browserWindow.isFullScreen()
const state = wndState;
if (isMinimized && state !== WND_STATE.MINIMIZED) {
wndState = WND_STATE.MINIMIZED
} else if (isFullScreen && state !== WND_STATE.FULL_SCREEN) {
wndState = WND_STATE.FULL_SCREEN
} else if (isMaximized && state !== WND_STATE.MAXIMIZED) {
wndState = WND_STATE.MAXIMIZED
self.browserWindow.webContents.executeJavaScript(`app.chrome.maximized = true`)
} else if (state !== WND_STATE.NORMAL) {
wndState = WND_STATE.NORMAL
self.browserWindow.webContents.executeJavaScript(`app.chrome.maximized = false`)
}
})
}
// Set window Handler
this.browserWindow.webContents.setWindowOpenHandler(({url}) => {
if (url.includes("apple") || url.includes("localhost")) {
return {action: "allow"}
}
shell.openExternal(url).catch(() => {
})
return {
action: 'deny'
}
})
//-------------------------------------------------------------------------------
// Renderer IPC Listeners
//-------------------------------------------------------------------------------
ipcMain.on("cider-platform", (event) => {
event.returnValue = process.platform
})
ipcMain.on("get-gpu-mode", (event) => {
event.returnValue = process.platform
})
ipcMain.on("is-dev", (event) => {
event.returnValue = !app.isPackaged
})
// IPC stuff (listeners)
ipcMain.on('close', () => { // listen for close event
self.browserWindow.close();
})
ipcMain.handle('getYTLyrics', async (event, track, artist) => {
const u = track + " " + artist + " official video";
const videos = await yt.search(u);
return videos
})
ipcMain.handle('getStoreValue', (event, key, defaultValue) => {
return (defaultValue ? app.cfg.get(key, true) : app.cfg.get(key));
});
ipcMain.handle('setStoreValue', (event, key, value) => {
app.cfg.set(key, value);
});
ipcMain.on('getStore', (event) => {
event.returnValue = app.cfg.store
})
ipcMain.on('setStore', (event, store) => {
app.cfg.store = store
})
ipcMain.on('maximize', () => { // listen for maximize event
if (self.browserWindow.isMaximized()) {
self.browserWindow.unmaximize()
} else {
self.browserWindow.maximize()
}
})
ipcMain.on('minimize', () => { // listen for minimize event
self.browserWindow.minimize();
})
// Set scale
ipcMain.on('setScreenScale', (event, scale) => {
self.browserWindow.webContents.setZoomFactor(parseFloat(scale))
})
},
/**
* Starts the webserver
*/
initializeWebServer() {
const self = this;
const webapp = express(),
webRemotePath = path.join(__dirname, '../../renderer/');
webapp.set("views", path.join(webRemotePath, "views"));
webapp.set("view engine", "ejs");
webapp.use(function (req, res, next) {
// if not localhost
if (req.headers.host.includes("localhost") && req.headers["user-agent"].includes("Cider")) {
next();
}
});
webapp.use(express.static(webRemotePath));
webapp.get('/', function (req, res) {
//res.sendFile(path.join(webRemotePath, 'index_old.html'));
res.render("main", self.EnvironmentVariables)
});
webapp.listen(this.clientPort, function () {
console.log(`Cider client port: ${self.clientPort}`);
});
},
/**
* Initializes the application session.
*/
initializeSession() {
const self = this;
// intercept "https://js-cdn.music.apple.com/hls.js/2.141.0/hls.js/hls.js" and redirect to local file "./apple-hls.js" instead
this.browserWindow.webContents.session.webRequest.onBeforeRequest(
{
urls: ["https://*/*.js"]
},
(details, callback) => {
if (details.url.includes("hls.js")) {
callback({
redirectURL: `http://localhost:${self.clientPort}/apple-hls.js`
})
} else {
callback({
cancel: false
})
}
}
)
this.browserWindow.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 this.browserWindow.webContents.executeJavaScript(`window.localStorage.getItem("music.ampwebplay.itspod")`)
if (itspod != null)
details.requestHeaders['Cookie'] = `itspod=${itspod}`
}
callback({requestHeaders: details.requestHeaders})
})
let location = `http://localhost:${this.clientPort}/`
this.browserWindow.loadURL(location).catch(err => {
console.log(err)
})
}
}

View file

@ -6,243 +6,50 @@ const path = require("path");
const windowStateKeeper = require("electron-window-state");
const os = require('os');
const yt = require('youtube-search-without-api-key');
const discord = require('./discordrpc');
const lastfm = require('./lastfm');
const mpris = require('./mpris');
const discord = require('./plugins/discordrpc');
const lastfm = require('./plugins/lastfm');
const mpris = require('./plugins/mpris');
// Analytics for debugging.
const ElectronSentry = require("@sentry/electron");
ElectronSentry.init({dsn: "https://68c422bfaaf44dea880b86aad5a820d2@o954055.ingest.sentry.io/6112214"});
const CiderBase = {
win: null,
async Start() {
this.clientPort = await getPort({port: 9000});
this.win = this.CreateBrowserWindow()
},
clientPort: 0,
CreateBrowserWindow() {
// Set default window sizes
const mainWindowState = windowStateKeeper({
defaultWidth: 1024,
defaultHeight: 600
});
module.exports = {
let win = null
const options = {
icon: join(__dirname, `../../resources/icons/icon.ico`),
width: mainWindowState.width,
height: mainWindowState.height,
x: mainWindowState.x,
y: mainWindowState.y,
minWidth: 844,
minHeight: 410,
frame: false,
title: "Cider",
vibrancy: 'dark',
// transparent: true,
hasShadow: false,
webPreferences: {
webviewTag: true,
plugins: true,
nodeIntegration: true,
nodeIntegrationInWorker: false,
webSecurity: false,
allowRunningInsecureContent: true,
enableRemoteModule: true,
sandbox: true,
nativeWindowOpen: true,
contextIsolation: false,
preload: join(__dirname, '../preload/cider-preload.js')
}
}
//-------------------------------------------------------------------------------
// Public Methods
//-------------------------------------------------------------------------------
CiderBase.InitWebServer()
// Create the BrowserWindow
if (process.platform === "darwin" || process.platform === "linux") {
win = new BrowserWindow(options)
} else {
const {BrowserWindow} = require("electron-acrylic-window");
win = new BrowserWindow(options)
}
// intercept "https://js-cdn.music.apple.com/hls.js/2.141.0/hls.js/hls.js" and redirect to local file "./apple-hls.js" instead
win.webContents.session.webRequest.onBeforeRequest(
{
urls: ["https://*/*.js"]
},
(details, callback) => {
if (details.url.includes("hls.js")) {
callback({
redirectURL: `http://localhost:${CiderBase.clientPort}/apple-hls.js`
})
} else {
callback({
cancel: false
})
}
}
)
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")`)
if (itspod != null)
details.requestHeaders['Cookie'] = `itspod=${itspod}`
}
callback({requestHeaders: details.requestHeaders})
})
let location = `http://localhost:${CiderBase.clientPort}/`
win.loadURL(location)
win.on("closed", () => {
win = null
})
// Register listeners on Window to track size and position of the Window.
mainWindowState.manage(win);
// IPC stuff (senders)
ipcMain.on("cider-platform", (event) => {
event.returnValue = process.platform
})
ipcMain.on("get-gpu-mode", (event) => {
event.returnValue = process.platform
})
ipcMain.on("is-dev", (event) => {
event.returnValue = !app.isPackaged
})
// IPC stuff (listeners)
ipcMain.on('close', () => { // listen for close event
win.close();
})
ipcMain.handle('getYTLyrics', async (event, track, artist) => {
var u = track + " " + artist + " official video";
const videos = await yt.search(u);
return videos
})
ipcMain.handle('getStoreValue', (event, key, defaultValue) => {
return (defaultValue ? app.cfg.get(key, true) : app.cfg.get(key));
});
ipcMain.handle('setStoreValue', (event, key, value) => {
app.cfg.set(key, value);
});
ipcMain.on('getStore', (event) => {
event.returnValue = app.cfg.store
})
ipcMain.on('setStore', (event, store) => {
app.cfg.store = store
})
ipcMain.on('maximize', () => { // listen for maximize event
if (win.isMaximized()) {
win.unmaximize()
} else {
win.maximize()
}
})
ipcMain.on('minimize', () => { // listen for minimize event
win.minimize();
})
if (process.platform === "win32") {
let WND_STATE = {
MINIMIZED: 0,
NORMAL: 1,
MAXIMIZED: 2,
FULL_SCREEN: 3
}
let wndState = WND_STATE.NORMAL
win.on("resize", (_event) => {
const isMaximized = win.isMaximized()
const isMinimized = win.isMinimized()
const isFullScreen = win.isFullScreen()
const state = wndState;
if (isMinimized && state !== WND_STATE.MINIMIZED) {
wndState = WND_STATE.MINIMIZED
} else if (isFullScreen && state !== WND_STATE.FULL_SCREEN) {
wndState = WND_STATE.FULL_SCREEN
} else if (isMaximized && state !== WND_STATE.MAXIMIZED) {
wndState = WND_STATE.MAXIMIZED
win.webContents.executeJavaScript(`app.chrome.maximized = true`)
} else if (state !== WND_STATE.NORMAL) {
wndState = WND_STATE.NORMAL
win.webContents.executeJavaScript(`app.chrome.maximized = false`)
}
})
}
// Set window Handler
win.webContents.setWindowOpenHandler(({url}) => {
if (url.includes("apple") || url.includes("localhost")) {
return {action: "allow"}
}
shell.openExternal(url).catch(() => {
})
return {
action: 'deny'
}
})
// Set scale
ipcMain.on('setScreenScale', (event, scale) => {
win.webContents.setZoomFactor(parseFloat(scale))
})
win.webContents.setZoomFactor(screen.getPrimaryDisplay().scaleFactor)
mpris.connect(win)
lastfm.authenticate()
// Discord
discord.connect((app.cfg.get("general.discord_rpc") == 1) ? '911790844204437504' : '886578863147192350');
ipcMain.on('playbackStateDidChange', (_event, a) => {
app.media = a;
discord.updateActivity(a)
mpris.updateState(a)
lastfm.scrobbleSong(a)
lastfm.updateNowPlayingSong(a)
});
ipcMain.on('nowPlayingItemDidChange', (_event, a) => {
app.media = a;
discord.updateActivity(a)
mpris.updateAttributes(a)
lastfm.scrobbleSong(a)
lastfm.updateNowPlayingSong(a)
});
return win
/**
* Starts the application (called on on-ready). - Starts BrowserWindow and WebServer
*/
Start() {
const {createBrowserWindow} = require("./base/win");
app.win = createBrowserWindow()
},
EnvironmentVariables: {
"env": {
platform: os.platform(),
dev: app.isPackaged
}
/**
* Initializes the main application (run before on-ready)
*/
Init() {
// Initialize the config.
const {init} = require("./base/store");
init()
},
LinkHandler: (startArgs) => {
/**
* Handles all links being opened in the application.
*/
LinkHandler(startArgs) {
if (!startArgs) return;
console.log("lfmtoken",String(startArgs))
console.log("lfmtoken", String(startArgs))
if (String(startArgs).includes('auth')) {
let authURI = String(startArgs).split('/auth/')[1]
if (authURI.startsWith('lastfm')) { // If we wanted more auth options
const authKey = authURI.split('lastfm?token=')[1];
app.cfg.set('lastfm.enabled', true);
app.cfg.set('lastfm.auth_token', authKey);
CiderBase.win.webContents.send('LastfmAuthenticated', authKey);
app.win.webContents.send('LastfmAuthenticated', authKey);
lastfm.authenticate()
}
} else {
@ -258,30 +65,4 @@ const CiderBase = {
}
},
async InitWebServer() {
const webapp = express();
const webRemotePath = path.join(__dirname, '../renderer/');
webapp.set("views", path.join(webRemotePath, "views"));
webapp.set("view engine", "ejs");
webapp.use(function (req, res, next) {
// if not localhost
if (req.headers.host.includes("localhost") && req.headers["user-agent"].includes("Cider")) {
next();
}
});
webapp.use(express.static(webRemotePath));
webapp.get('/', function (req, res) {
//res.sendFile(path.join(webRemotePath, 'index_old.html'));
res.render("main", CiderBase.EnvironmentVariables)
});
webapp.listen(CiderBase.clientPort, function () {
console.log(`Cider client port: ${CiderBase.clientPort}`);
});
},
}
module.exports = CiderBase;
}

View file

@ -2,7 +2,7 @@ const {app, Notification} = require('electron'),
fs = require('fs'),
{resolve} = require('path'),
sessionPath = resolve(app.getPath('userData'), 'session.json'),
apiCredentials = require('../../resources/lfmApiCredentials.json'),
apiCredentials = require('../../../resources/lfmApiCredentials.json'),
LastfmAPI = require('lastfmapi');
const lfm = {