this might not be happening

This commit is contained in:
Core 2022-01-07 14:50:06 +00:00
parent d15ee4de30
commit 8e1d2dc96b
No known key found for this signature in database
GPG key ID: 1B77805746C47C28
3 changed files with 385 additions and 241 deletions

View file

@ -1,55 +1,54 @@
const {join} = require("path"), const { BrowserWindow, ipcMain, shell, app, screen } = require("electron")
{ipcMain, app, shell, screen} = require("electron"), const { join } = require("path")
express = require("express"), const getPort = require("get-port");
path = require("path"), const express = require("express");
getPort = require("get-port"), const path = require("path");
yt = require("youtube-search-without-api-key"), const windowStateKeeper = require("electron-window-state");
os = require("os"); const os = require('os');
const yt = require('youtube-search-without-api-key');
const discord = require('./discordrpc');
const lastfm = require('./lastfm');
const { writeFile, writeFileSync, existsSync, mkdirSync } = require('fs');
const fs = require('fs');
const mpris = require('./mpris');
const mm = require('music-metadata');
const fetch = require('electron-fetch').default;
const { Stream } = require('stream');
module.exports = { // Analytics for debugging.
const ElectronSentry = require("@sentry/electron");
ElectronSentry.init({ dsn: "https://68c422bfaaf44dea880b86aad5a820d2@o954055.ingest.sentry.io/6112214" });
browserWindow: {}, const CiderBase = {
win: null,
clientPort: 0, requests: [],
audiostream: new Stream.PassThrough(),
EnvironmentVariables: { async Start() {
"env": {
platform: os.platform(),
dev: app.isPackaged
}
},
//-------------------------------------------------------------------------------
// Public Methods
//-------------------------------------------------------------------------------
/**
* Creates the BrowserWindow for the application.
* @return {object} BrowserWindow
*/
async createBrowserWindow() {
this.clientPort = await getPort({ port: 9000 }); this.clientPort = await getPort({ port: 9000 });
this.win = this.CreateBrowserWindow()
const windowStateKeeper = require("electron-window-state"), },
BrowserWindow = require((process.platform === "win32") ? "electron-acrylic-window" : "electron").BrowserWindow; clientPort: 0,
CreateBrowserWindow() {
const windowState = windowStateKeeper({ this.VerifyFiles()
// Set default window sizes
const mainWindowState = windowStateKeeper({
defaultWidth: 1024, defaultWidth: 1024,
defaultHeight: 600 defaultHeight: 600
}); });
this.browserWindow = new BrowserWindow({ let win = null
icon: join(__dirname, `../../../resources/icons/icon.ico`), const options = {
width: windowState.width, icon: join(__dirname, `../../resources/icons/icon.ico`),
height: windowState.height, width: mainWindowState.width,
x: windowState.x, height: mainWindowState.height,
y: windowState.y, x: mainWindowState.x,
y: mainWindowState.y,
minWidth: 844, minWidth: 844,
minHeight: 410, minHeight: 410,
frame: false, frame: false,
title: "Cider", title: "Cider",
vibrancy: 'dark', vibrancy: 'dark',
transparent: process.platform === "darwin", // transparent: true,
hasShadow: false, hasShadow: false,
webPreferences: { webPreferences: {
webviewTag: true, webviewTag: true,
@ -62,74 +61,62 @@ module.exports = {
sandbox: true, sandbox: true,
nativeWindowOpen: true, nativeWindowOpen: true,
contextIsolation: false, contextIsolation: false,
preload: join(__dirname, '../../preload/cider-preload.js') preload: join(__dirname, '../preload/cider-preload.js')
}
} }
})
this.initializeWebServer(); CiderBase.InitWebServer()
this.initializeSession();
this.initializeHandlers();
windowState.manage(this.browserWindow); // Create the BrowserWindow
this.browserWindow.webContents.setZoomFactor(screen.getPrimaryDisplay().scaleFactor) if (process.platform === "darwin" || process.platform === "linux") {
win = new BrowserWindow(options)
} else {
if (app.cfg.get("visual.window_transparency") !== "disabled") {
const { BrowserWindow } = require("electron-acrylic-window");
}
win = new BrowserWindow(options)
win.setVibrancy("dark")
}
return this.browserWindow // 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")) {
* Initializes the BrowserWindow handlers for the application. callback({
*/ redirectURL: `http://localhost:${CiderBase.clientPort}/apple-hls.js`
initializeHandlers() { })
const self = this; } else {
callback({
this.browserWindow.on('closed', () => { cancel: false
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 win.webContents.session.webRequest.onBeforeSendHeaders(async (details, callback) => {
this.browserWindow.webContents.setWindowOpenHandler(({url}) => { if (details.url === "https://buy.itunes.apple.com/account/web/info") {
if (url.includes("apple") || url.includes("localhost")) { details.requestHeaders['sec-fetch-site'] = 'same-site';
return {action: "allow"} details.requestHeaders['DNT'] = '1';
} let itspod = await win.webContents.executeJavaScript(`window.localStorage.getItem("music.ampwebplay.itspod")`)
shell.openExternal(url).catch(() => { if (itspod != null)
}) details.requestHeaders['Cookie'] = `itspod=${itspod}`
return {
action: 'deny'
} }
callback({ requestHeaders: details.requestHeaders })
}) })
//------------------------------------------------------------------------------- let location = `http://localhost:${CiderBase.clientPort}/`
// Renderer IPC Listeners 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) => { ipcMain.on("cider-platform", (event) => {
event.returnValue = process.platform event.returnValue = process.platform
}) })
@ -144,11 +131,56 @@ module.exports = {
// IPC stuff (listeners) // IPC stuff (listeners)
ipcMain.on('close', () => { // listen for close event ipcMain.on('close', () => { // listen for close event
self.browserWindow.close(); win.close();
})
ipcMain.on('put-library-songs', (event, arg) => {
fs.writeFileSync(join(app.paths.ciderCache, "library-songs.json"), JSON.stringify(arg))
})
ipcMain.on('put-library-artists', (event, arg) => {
fs.writeFileSync(join(app.paths.ciderCache, "library-artists.json"), JSON.stringify(arg))
})
ipcMain.on('put-library-albums', (event, arg) => {
fs.writeFileSync(join(app.paths.ciderCache, "library-albums.json"), JSON.stringify(arg))
})
ipcMain.on('put-library-playlists', (event, arg) => {
fs.writeFileSync(join(app.paths.ciderCache, "library-playlists.json"), JSON.stringify(arg))
})
ipcMain.on('put-library-recentlyAdded', (event, arg) => {
fs.writeFileSync(join(app.paths.ciderCache, "library-recentlyAdded.json"), JSON.stringify(arg))
})
ipcMain.on('get-library-songs', (event) => {
let librarySongs = fs.readFileSync(join(app.paths.ciderCache, "library-songs.json"), "utf8")
event.returnValue = JSON.parse(librarySongs)
})
ipcMain.on('get-library-artists', (event) => {
let libraryArtists = fs.readFileSync(join(app.paths.ciderCache, "library-artists.json"), "utf8")
event.returnValue = JSON.parse(libraryArtists)
})
ipcMain.on('get-library-albums', (event) => {
let libraryAlbums = fs.readFileSync(join(app.paths.ciderCache, "library-albums.json"), "utf8")
event.returnValue = JSON.parse(libraryAlbums)
})
ipcMain.on('get-library-playlists', (event) => {
let libraryPlaylists = fs.readFileSync(join(app.paths.ciderCache, "library-playlists.json"), "utf8")
event.returnValue = JSON.parse(libraryPlaylists)
})
ipcMain.on('get-library-recentlyAdded', (event) => {
let libraryRecentlyAdded = fs.readFileSync(join(app.paths.ciderCache, "library-recentlyAdded.json"), "utf8")
event.returnValue = JSON.parse(libraryRecentlyAdded)
}) })
ipcMain.handle('getYTLyrics', async (event, track, artist) => { ipcMain.handle('getYTLyrics', async (event, track, artist) => {
const u = track + " " + artist + " official video"; var u = track + " " + artist + " official video";
const videos = await yt.search(u); const videos = await yt.search(u);
return videos return videos
}) })
@ -169,38 +201,174 @@ module.exports = {
app.cfg.store = store app.cfg.store = store
}) })
ipcMain.handle('setVibrancy', (event, key, value) => {
win.setVibrancy(value)
});
ipcMain.on('maximize', () => { // listen for maximize event ipcMain.on('maximize', () => { // listen for maximize event
if (self.browserWindow.isMaximized()) { if (win.isMaximized()) {
self.browserWindow.unmaximize() win.unmaximize()
} else { } else {
self.browserWindow.maximize() win.maximize()
} }
}) })
ipcMain.on('minimize', () => { // listen for minimize event ipcMain.on('minimize', () => { // listen for minimize event
self.browserWindow.minimize(); 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 // Set scale
ipcMain.on('setScreenScale', (event, scale) => { ipcMain.on('setScreenScale', (event, scale) => {
self.browserWindow.webContents.setZoomFactor(parseFloat(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)
});
ipcMain.on("getPreviewURL", (_event, url) => {
fetch(url)
.then(res => res.buffer())
.then(async (buffer) => {
try {
const metadata = await mm.parseBuffer(buffer, 'audio/x-m4a');
SoundCheckTag = metadata.native.iTunes[1].value
win.webContents.send('SoundCheckTag', SoundCheckTag)
} catch (error) {
console.error(error.message);
}
})
});
ipcMain.on('writeAudio', function (event, buffer) {
CiderBase.audiostream.write(Buffer.from(buffer));
})
return win
},
VerifyFiles() {
const expectedDirectories = [
"CiderCache"
]
const expectedFiles = [
"library-songs.json",
"library-artists.json",
"library-albums.json",
"library-playlists.json",
"library-recentlyAdded.json",
]
for (let i = 0; i < expectedDirectories.length; i++) {
if (!existsSync(path.join(app.getPath("userData"), expectedDirectories[i]))) {
mkdirSync(path.join(app.getPath("userData"), expectedDirectories[i]))
}
}
for (let i = 0; i < expectedFiles.length; i++) {
const file = path.join(app.paths.ciderCache, expectedFiles[i])
if (!existsSync(file)) {
writeFileSync(file, JSON.stringify([]))
}
}
},
EnvironmentVariables: {
"env": {
platform: os.platform(),
dev: app.isPackaged
}
},
LinkHandler: (startArgs) => {
if (!startArgs) return;
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);
lastfm.authenticate()
}
} else {
const formattedSongID = startArgs.replace('ame://', '').replace('/', '');
console.warn(`[LinkHandler] Attempting to load song id: ${formattedSongID}`);
// setQueue can be done with album, song, url, playlist id
this.win.webContents.executeJavaScript(`
MusicKit.getInstance().setQueue({ song: '${formattedSongID}'}).then(function(queue) {
MusicKit.getInstance().play();
});
`).catch((err) => console.error(err));
}
}, },
/** async InitWebServer() {
* Starts the webserver const webapp = express();
*/ const webRemotePath = path.join(__dirname, '../renderer/');
initializeWebServer() {
const self = this;
const webapp = express(),
webRemotePath = path.join(__dirname, '../../renderer/');
webapp.set("views", path.join(webRemotePath, "views")); webapp.set("views", path.join(webRemotePath, "views"));
webapp.set("view engine", "ejs"); webapp.set("view engine", "ejs");
webapp.use(function (req, res, next) { webapp.use(function (req, res, next) {
// if not localhost // if not localhost
if (req.headers.host.includes("localhost") && req.headers["user-agent"].includes("Cider")) { if (req.url.includes("audio.webm") || (req.headers.host.includes("localhost") && req.headers["user-agent"].includes("Cider"))) {
next(); next();
} }
}); });
@ -208,52 +376,32 @@ module.exports = {
webapp.use(express.static(webRemotePath)); webapp.use(express.static(webRemotePath));
webapp.get('/', function (req, res) { webapp.get('/', function (req, res) {
//res.sendFile(path.join(webRemotePath, 'index_old.html')); //res.sendFile(path.join(webRemotePath, 'index_old.html'));
res.render("main", self.EnvironmentVariables) res.render("main", CiderBase.EnvironmentVariables)
}); });
webapp.listen(this.clientPort, function () { webapp.get('/audio.webm', function (req, res) {
console.log(`Cider client port: ${self.clientPort}`); try {
req.connection.setTimeout(Number.MAX_SAFE_INTEGER);
// CiderBase.requests.push({req: req, res: res});
// var pos = CiderBase.requests.length - 1;
// req.on("close", () => {
// console.info("CLOSED", CiderBase.requests.length);
// requests.splice(pos, 1);
// console.info("CLOSED", CiderBase.requests.length);
// });
CiderBase.audiostream.on('data', (data) => {
try {
res.write(data);
} catch (ex) {
console.log(ex)
}
})
} catch (ex) { console.log(ex) }
});
webapp.listen(CiderBase.clientPort, function () {
console.log(`Cider client port: ${CiderBase.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)
})
} }
} module.exports = CiderBase;

View file

@ -1,25 +1,26 @@
import * as path from "path"; import * as path from "path";
import {app, ipcMain, shell} from "electron"; import * as electron from "electron";
import * as electronAcrylic from "electron-acrylic-window"
import * as windowStateKeeper from "electron-window-state"; import * as windowStateKeeper from "electron-window-state";
import * as express from "express"; import * as express from "express";
import * as getPort from "get-port"; import * as getPort from "get-port";
import * as yt from "youtube-search-without-api-key"; import * as yt from "youtube-search-without-api-key";
export default class Win { export class Win {
public win: null | undefined; win: any | undefined;
public app: Electron.App | undefined; app: electron.App | undefined;
private srcPath: string = path.join(__dirname, "../../src");
private resourcePath: string = path.join(__dirname, "../../resources");
private clientPort: number = 0; private clientPort: number = 0;
private static envVars: object = { private EnvironmentVariables: object = {
"env": { "env": {
// @ts-ignore
platform: process.platform, platform: process.platform,
// @ts-ignore dev: electron.app.isPackaged
dev: app.isPackaged
} }
}; };
private static options: any = { private options: any = {
icon: path.join(__dirname, `../../../resources/icons/icon.ico`), icon: path.join(this.resourcePath, `icons/icon.ico`),
width: 1024, width: 1024,
height: 600, height: 600,
x: undefined, x: undefined,
@ -28,7 +29,6 @@ export default class Win {
minHeight: 400, minHeight: 400,
frame: false, frame: false,
title: "Cider", title: "Cider",
vibrancy: 'dark',
transparent: process.platform === "darwin", transparent: process.platform === "darwin",
hasShadow: false, hasShadow: false,
webPreferences: { webPreferences: {
@ -42,27 +42,16 @@ export default class Win {
sandbox: true, sandbox: true,
nativeWindowOpen: true, nativeWindowOpen: true,
contextIsolation: false, contextIsolation: false,
preload: path.join(__dirname, '../../preload/cider-preload.js') preload: path.join(this.srcPath, 'preload/cider-preload.js')
} }
}; };
get options() {
return Win.options;
}
/** /**
* Creates the browser window * Creates the browser window
*/ */
public async createWindow(): Promise<Electron.BrowserWindow> { async createWindow(): Promise<any | undefined> {
this.clientPort = await getPort({port: 9000}); this.clientPort = await getPort({port: 9000});
let BrowserWindow;
if (process.platform === "win32") {
BrowserWindow = require("electron-acrylic-window").BrowserWindow;
} else {
BrowserWindow = require("electron").BrowserWindow;
}
// Load the previous state with fallback to defaults // Load the previous state with fallback to defaults
const windowState = windowStateKeeper({ const windowState = windowStateKeeper({
defaultWidth: 1024, defaultWidth: 1024,
@ -71,63 +60,79 @@ export default class Win {
this.options.width = windowState.width; this.options.width = windowState.width;
this.options.height = windowState.height; this.options.height = windowState.height;
this.startWebServer()
if (process.platform === "win32") {
this.win = new electronAcrylic.BrowserWindow(this.options);
} else {
this.win = new electron.BrowserWindow(this.options);
}
// Create the browser window. // Create the browser window.
const win = new BrowserWindow(this.options);
console.debug('Browser window created'); console.debug('Browser window created');
this.win = win;
// and load the renderer. // and load the renderer.
this.startWebServer(win) this.startSession(this.win);
this.startSession(win); this.startHandlers(this.win);
this.startHandlers(win);
return this.win;
return win;
} }
/** /**
* Starts the webserver for the renderer process. * Starts the webserver for the renderer process.
* @param win The BrowserWindow
*/ */
private startWebServer(win: Electron.BrowserWindow): void { private startWebServer(): void {
const webapp = express(), const webapp = express();
webRemotePath = path.join(__dirname, '../../renderer/'); const webRemotePath = path.join(this.srcPath, 'renderer');
webapp.set("views", path.join(webRemotePath, "views")); webapp.set("views", path.join(webRemotePath, "views"));
webapp.set("view engine", "ejs"); webapp.set("view engine", "ejs");
webapp.use((req, res, next) => { webapp.use(function (req, res, next) {
// if not localhost // if not localhost
// @ts-ignore // @ts-ignore
if (req.headers.host.includes("localhost") && req.headers["user-agent"].includes("Cider")) { if (req.url.includes("audio.webm") || (req.headers.host.includes("localhost") && req.headers["user-agent"].includes("Cider"))) {
next(); next();
} }
}); });
webapp.use(express.static(webRemotePath)); webapp.use(express.static(webRemotePath));
webapp.get('/', function (req, res) { webapp.get('/', (req, res) => {
//res.sendFile(path.join(webRemotePath, 'index_old.html')); //res.sendFile(path.join(webRemotePath, 'index_old.html'));
res.render("main", Win.envVars) console.log(req)
res.render("main", this.EnvironmentVariables)
}); });
// webelectron.app.get('/audio.webm', (req, res) => {
// try {
// req.connection.setTimeout(Number.MAX_SAFE_INTEGER);
// this.audiostream.on('data', (data) => {
// try {
// res.write(data);
// } catch (ex) {
// console.log(ex)
// }
// })
// } catch (ex) { console.log(ex) }
// });
webapp.listen(this.clientPort, () => { webapp.listen(this.clientPort, () => {
console.debug(`Cider client port: ${this.clientPort}`); console.log(`Cider client port: ${this.clientPort}`);
}); });
} }
/** /**
* Starts the session for the renderer process. * Starts the session for the renderer process.
* @param win The BrowserWindow
*/ */
private startSession(win: Electron.BrowserWindow): void { private startSession(win: any): void {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 // 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( win.webContents.session.webRequest.onBeforeRequest(
{ {
urls: ["https://*/*.js"] urls: ["https://*/*.js"]
}, },
(details, callback) => { (details: { url: string | string[]; }, callback: (arg0: { redirectURL?: string; cancel?: boolean; }) => void) => {
if (details.url.includes("hls.js")) { if (details.url.includes("hls.js")) {
callback({ callback({
redirectURL: `http://localhost:${this.clientPort}/apple-hls.js` redirectURL: `http://localhost:${self.clientPort}/apple-hls.js`
}) })
} else { } else {
callback({ callback({
@ -137,7 +142,7 @@ export default class Win {
} }
) )
win.webContents.session.webRequest.onBeforeSendHeaders(async (details, callback) => { win.webContents.session.webRequest.onBeforeSendHeaders(async (details: { url: string; requestHeaders: { [x: string]: string; }; }, callback: (arg0: { requestHeaders: any; }) => void) => {
if (details.url === "https://buy.itunes.apple.com/account/web/info") { if (details.url === "https://buy.itunes.apple.com/account/web/info") {
details.requestHeaders['sec-fetch-site'] = 'same-site'; details.requestHeaders['sec-fetch-site'] = 'same-site';
details.requestHeaders['DNT'] = '1'; details.requestHeaders['DNT'] = '1';
@ -161,11 +166,7 @@ export default class Win {
* Initializes the window handlers * Initializes the window handlers
* @param win The BrowserWindow * @param win The BrowserWindow
*/ */
private startHandlers(win: Electron.BrowserWindow): void { private startHandlers(win: any): void {
win.on('closed', () => {
this.win = null;
});
if (process.platform === "win32") { if (process.platform === "win32") {
let WND_STATE = { let WND_STATE = {
MINIMIZED: 0, MINIMIZED: 0,
@ -186,20 +187,20 @@ export default class Win {
wndState = WND_STATE.FULL_SCREEN wndState = WND_STATE.FULL_SCREEN
} else if (isMaximized && state !== WND_STATE.MAXIMIZED) { } else if (isMaximized && state !== WND_STATE.MAXIMIZED) {
wndState = WND_STATE.MAXIMIZED wndState = WND_STATE.MAXIMIZED
win.webContents.executeJavaScript(`app.chrome.maximized = true`) win.webContents.executeJavaScript(`electron.app.chrome.maximized = true`)
} else if (state !== WND_STATE.NORMAL) { } else if (state !== WND_STATE.NORMAL) {
wndState = WND_STATE.NORMAL wndState = WND_STATE.NORMAL
win.webContents.executeJavaScript(`app.chrome.maximized = false`) win.webContents.executeJavaScript(`electron.app.chrome.maximized = false`)
} }
}) })
} }
// Set window Handler // Set window Handler
win.webContents.setWindowOpenHandler(({url}) => { win.webContents.setWindowOpenHandler((x: any) => {
if (url.includes("apple") || url.includes("localhost")) { if (x.url.includes("apple") || x.url.includes("localhost")) {
return {action: "allow"} return {action: "allow"}
} }
shell.openExternal(url).catch(() => { electron.shell.openExternal(x.url).catch(() => {
}) })
return { return {
action: 'deny' action: 'deny'
@ -210,46 +211,45 @@ export default class Win {
// Renderer IPC Listeners // Renderer IPC Listeners
//------------------------------------------------------------------------------- //-------------------------------------------------------------------------------
ipcMain.on("cider-platform", (event) => { electron.ipcMain.on("cider-platform", (event) => {
event.returnValue = process.platform event.returnValue = process.platform
}) })
ipcMain.on("get-gpu-mode", (event) => { electron.ipcMain.on("get-gpu-mode", (event) => {
event.returnValue = process.platform event.returnValue = process.platform
}) })
ipcMain.on("is-dev", (event) => { electron.ipcMain.on("is-dev", (event) => {
event.returnValue = !app.isPackaged event.returnValue = !electron.app.isPackaged
}) })
// IPC stuff (listeners) // IPC stuff (listeners)
ipcMain.on('close', () => { // listen for close event electron.ipcMain.on('close', () => { // listen for close event
win.close(); win.close();
}) })
ipcMain.handle('getYTLyrics', async (event, track, artist) => { electron.ipcMain.handle('getYTLyrics', async (event, track, artist) => {
const u = track + " " + artist + " official video"; const u = track + " " + artist + " official video";
const videos = await yt.search(u); return await yt.search(u)
return videos
}) })
// ipcMain.handle('getStoreValue', (event, key, defaultValue) => { // electron.ipcMain.handle('getStoreValue', (event, key, defaultValue) => {
// return (defaultValue ? app.cfg.get(key, true) : app.cfg.get(key)); // return (defaultValue ? electron.app.cfg.get(key, true) : electron.app.cfg.get(key));
// }); // });
// //
// ipcMain.handle('setStoreValue', (event, key, value) => { // electron.ipcMain.handle('setStoreValue', (event, key, value) => {
// app.cfg.set(key, value); // electron.app.cfg.set(key, value);
// }); // });
// //
// ipcMain.on('getStore', (event) => { // electron.ipcMain.on('getStore', (event) => {
// event.returnValue = app.cfg.store // event.returnValue = electron.app.cfg.store
// }) // })
// //
// ipcMain.on('setStore', (event, store) => { // electron.ipcMain.on('setStore', (event, store) => {
// app.cfg.store = store // electron.app.cfg.store = store
// }) // })
ipcMain.on('maximize', () => { // listen for maximize event electron.ipcMain.on('maximize', () => { // listen for maximize event
if (win.isMaximized()) { if (win.isMaximized()) {
win.unmaximize() win.unmaximize()
} else { } else {
@ -257,12 +257,12 @@ export default class Win {
} }
}) })
ipcMain.on('minimize', () => { // listen for minimize event electron.ipcMain.on('minimize', () => { // listen for minimize event
win.minimize(); win.minimize();
}) })
// Set scale // Set scale
ipcMain.on('setScreenScale', (event, scale) => { electron.ipcMain.on('setScreenScale', (event, scale) => {
win.webContents.setZoomFactor(parseFloat(scale)) win.webContents.setZoomFactor(parseFloat(scale))
}) })
} }

View file

@ -1,12 +1,8 @@
require('v8-compile-cache'); require('v8-compile-cache');
// import { app } from 'electron'; import { app } from 'electron';
// import { resolve } from 'path'; // import { resolve } from 'path';
import { app, BrowserWindow } from "electron";
import * as path from "path";
// Analytics for debugging fun yeah. // Analytics for debugging fun yeah.
// const ElectronSentry = require("@sentry/electron"); // const ElectronSentry = require("@sentry/electron");
// ElectronSentry.init({ dsn: "https://68c422bfaaf44dea880b86aad5a820d2@o954055.ingest.sentry.io/6112214" }); // ElectronSentry.init({ dsn: "https://68c422bfaaf44dea880b86aad5a820d2@o954055.ingest.sentry.io/6112214" });
@ -15,7 +11,7 @@ import * as path from "path";
// // Init() // // Init()
// //
import Win from "./base/win"; import {Win} from "./base/win";
const Cider = new Win() const Cider = new Win()