Fix Custom Protocol Handling

Fixed an issue where the custom protocol ('tidal://...') wasn't being recognized correctly. Now, the system checks for the presence of the custom protocol before the usual URL ('https://listen.tidal.com') is loaded.

Also implemented functionality to ensure that if only one instance is allowed at a time, the first instance changes its page accordingly.
This commit is contained in:
TheRockYT 2024-03-26 00:16:09 +01:00
parent 2e31b5d913
commit 554cb12a01

View File

@ -5,7 +5,6 @@ import {
components,
globalShortcut,
ipcMain,
protocol,
session,
} from "electron";
import path from "path";
@ -61,20 +60,31 @@ function syncMenuBarWithStore() {
}
/**
* Determine whether the current window is the main window
* if singleInstance is requested.
* If singleInstance isn't requested simply return true
* @returns true if singInstance is not requested, otherwise true/false based on whether the current window is the main window
* @returns true/false based on whether the current window is the main window
*/
function isMainInstanceOrMultipleInstancesAllowed() {
if (settingsStore.get(settings.singleInstance)) {
const gotTheLock = app.requestSingleInstanceLock();
function isMainInstance() {
return app.requestSingleInstanceLock();
}
if (!gotTheLock) {
return false;
}
/**
* @returns true/false based on whether multiple instances are allowed
*/
function isMultipleInstancesAllowed() {
return !settingsStore.get(settings.singleInstance);
}
/**
* @param args the arguments passed to the app
* @returns the custom protocol url if it exists, otherwise null
*/
function getCustomProtocolUrl(args: string[]) {
const customProtocolArg = args.find((arg) => arg.startsWith(PROTOCOL_PREFIX));
if (!customProtocolArg) {
return null;
}
return true;
return tidalUrl + "/" + customProtocolArg.substring(PROTOCOL_PREFIX.length + 3)
}
function createWindow(options = { x: 0, y: 0, backgroundColor: "white" }) {
@ -98,8 +108,16 @@ function createWindow(options = { x: 0, y: 0, backgroundColor: "white" }) {
registerHttpProtocols();
syncMenuBarWithStore();
// load the Tidal website
mainWindow.loadURL(tidalUrl);
// find the custom protocol argument
const customProtocolUrl = getCustomProtocolUrl(process.argv);
if(customProtocolUrl) {
// load the url received from the custom protocol
mainWindow.loadURL(customProtocolUrl);
} else {
// load the Tidal website
mainWindow.loadURL(tidalUrl);
}
if (settingsStore.get(settings.disableBackgroundThrottle)) {
// prevent setInterval lag
@ -139,9 +157,6 @@ function createWindow(options = { x: 0, y: 0, backgroundColor: "white" }) {
}
function registerHttpProtocols() {
protocol.registerHttpProtocol(PROTOCOL_PREFIX, (request) => {
mainWindow.loadURL(`${tidalUrl}/${request.url.substring(PROTOCOL_PREFIX.length + 3)}`);
});
if (!app.isDefaultProtocolClient(PROTOCOL_PREFIX)) {
app.setAsDefaultProtocolClient(PROTOCOL_PREFIX);
}
@ -159,7 +174,24 @@ function addGlobalShortcuts() {
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on("ready", async () => {
if (isMainInstanceOrMultipleInstancesAllowed()) {
// check if the app is the main instance and multiple instances are not allowed
if (isMainInstance() && !isMultipleInstancesAllowed()) {
app.on('second-instance', (_, commandLine) => {
const customProtocolUrl = getCustomProtocolUrl(commandLine);
if (customProtocolUrl) {
mainWindow.loadURL(customProtocolUrl);
}
if (mainWindow) {
if (mainWindow.isMinimized()) mainWindow.restore();
mainWindow.focus();
}
});
}
if (isMainInstance() || isMultipleInstancesAllowed()) {
await components.whenReady();
// Adblock