Link handling should be working

This commit is contained in:
Core 2022-01-24 00:25:43 +00:00
parent 64f646e123
commit 5c7a05ff61
5 changed files with 155 additions and 147 deletions

View file

@ -9,7 +9,7 @@ export class AppEvents {
"musics",
"music"
]
private static plugin: any = null;
private static store: any = null;
constructor(store: any) {
@ -88,27 +88,34 @@ export class AppEvents {
})
}
electron.app.on('open-url', (event, url) => {
event.preventDefault()
if (this.protocols.some((protocol: string) => url.includes(protocol))) {
AppEvents.LinkHandler(url)
}
})
}
public quit() {
console.log('App stopped');
}
public ready() {
public ready(plug: any) {
AppEvents.plugin = plug
console.log('App ready');
}
public bwCreated(win: Electron.BrowserWindow) {
electron.app.on('open-url', (event, url) => {
event.preventDefault()
if (AppEvents.protocols.some((protocol: string) => url.includes(protocol))) {
AppEvents.LinkHandler(url, win)
}
})
AppEvents.InstanceHandler(win)
}
/***********************************************************************************************************************
* Private methods
**********************************************************************************************************************/
private static LinkHandler(arg: string) {
private static LinkHandler(arg: any, win: Electron.BrowserWindow) {
if (!arg) return;
// LastFM Auth URL
@ -118,8 +125,8 @@ export class AppEvents {
const authKey = authURI.split('lastfm?token=')[1];
AppEvents.store.set('lastfm.enabled', true);
AppEvents.store.set('lastfm.auth_token', authKey);
// AppEvents.window.webContents.send('LastfmAuthenticated', authKey);
// lastfm.authenticate()
win.webContents.send('LastfmAuthenticated', authKey);
AppEvents.plugin.callPlugin('lastfm', 'authenticate', authKey);
}
}
// Play
@ -148,4 +155,28 @@ export class AppEvents {
electron.ipcRenderer.send('play', 'url', url)
}
}
private static InstanceHandler(win: Electron.BrowserWindow) {
// Detects of an existing instance is running (So if the lock has been achieved, no existing instance has been found)
const gotTheLock = electron.app.requestSingleInstanceLock()
if (!gotTheLock) { // Runs on the new instance if another instance has been found
console.log('[Cider] Another instance has been found, quitting.')
electron.app.quit()
} else { // Runs on the first instance if no other instance has been found
electron.app.on('second-instance', (_event, startArgs) => {
if (startArgs.includes("--force-quit")) {
console.warn('[InstanceHandler][SecondInstanceHandler] Force Quit found. Quitting App.');
electron.app.quit()
} else if (startArgs.includes("cider://")) {
AppEvents.LinkHandler(startArgs, win)
} else if (win) {
if (win.isMinimized()) win.restore()
win.focus()
}
})
}
}
}

View file

@ -34,6 +34,7 @@ export default class PluginHandler {
fs.readdirSync(this.userPluginsPath).forEach(file => {
if (file.endsWith('.ts') || file.endsWith('.js')) {
const plugin = require(path.join(this.userPluginsPath, file)).default;
file = file.replace('.ts', '').replace('.js', '');
if (plugins[file] || plugin in plugins) {
console.log(`[${plugin.name}] Plugin already loaded / Duplicate Class Name`);
} else {
@ -54,4 +55,10 @@ export default class PluginHandler {
}
}
public callPlugin(plugin: string, event: string, ...args: any[]) {
if (this.pluginsList[plugin][event]) {
this.pluginsList[plugin][event](...args);
}
}
}

View file

@ -8,7 +8,6 @@ import * as yt from "youtube-search-without-api-key";
import * as fs from "fs";
import { Stream } from "stream";
import * as qrcode from "qrcode-terminal";
import * as qrcode2 from "qrcode";
import * as os from "os";
import {wsapi} from "./wsapi";
@ -62,7 +61,6 @@ export class Win {
sandbox: true,
allowRunningInsecureContent: true,
contextIsolation: false,
webviewTag: true,
plugins: true,
nodeIntegrationInWorker: false,
@ -74,7 +72,7 @@ export class Win {
/**
* Creates the browser window
*/
async createWindow(): Promise<void> {
async createWindow(): Promise<Electron.BrowserWindow> {
this.clientPort = await getPort({ port: 9000 });
this.verifyFiles();
@ -91,9 +89,6 @@ export class Win {
this.startWebServer();
this.win = new electron.BrowserWindow(this.options);
this.win.on("ready-to-show", () => {
this.win.show();
});
const ws = new wsapi(this.win)
ws.InitWebSockets()
// and load the renderer.