ytDownloader/main.js

304 lines
6.5 KiB
JavaScript
Raw Normal View History

2022-12-13 14:48:02 +06:00
const {
app,
BrowserWindow,
dialog,
ipcMain,
shell,
Tray,
Menu,
2022-12-15 21:52:47 +06:00
clipboard,
2022-12-13 14:48:02 +06:00
} = require("electron");
2022-07-31 12:45:09 +06:00
const { autoUpdater } = require("electron-updater");
2022-10-27 12:49:15 +06:00
const fs = require("fs");
const path = require("path");
2022-09-02 00:25:25 +06:00
autoUpdater.autoDownload = false;
2022-12-24 17:46:19 +06:00
let win = null;
let secondaryWindow = null;
2022-12-13 14:48:02 +06:00
let tray = null;
let isQuiting = false;
let indexIsOpen = true;
let trayEnabled = false;
2022-07-31 12:45:09 +06:00
2022-09-18 22:56:36 +06:00
app.commandLine.appendSwitch("--enable-features", "Metal");
2022-09-10 13:37:48 +06:00
2022-07-31 12:45:09 +06:00
function createWindow() {
2022-08-02 23:27:08 +06:00
let isTransparent = false;
if (process.platform == "linux") {
isTransparent = true;
}
2022-08-05 17:37:17 +06:00
win = new BrowserWindow({
2022-07-31 12:45:09 +06:00
show: false,
2022-12-13 14:48:02 +06:00
icon: __dirname + "/assets/images/icon.png",
2022-08-02 23:27:08 +06:00
spellcheck: false,
transparent: isTransparent,
webPreferences: {
2022-08-05 17:37:17 +06:00
nodeIntegration: true,
2022-09-18 22:56:36 +06:00
contextIsolation: false,
2022-08-05 17:37:17 +06:00
},
2022-07-31 12:45:09 +06:00
});
2022-12-15 21:52:47 +06:00
win.on("close", (event) => {
if (!isQuiting && trayEnabled) {
2022-12-13 14:48:02 +06:00
event.preventDefault();
win.hide();
2022-12-23 21:03:58 +06:00
if (app.dock) app.dock.hide();
2022-12-13 14:48:02 +06:00
}
2022-12-15 21:52:47 +06:00
return false;
});
2022-08-05 17:37:17 +06:00
win.loadFile("html/index.html");
2022-07-31 12:45:09 +06:00
win.maximize();
2022-08-27 21:19:29 +06:00
// win.setMenu(null)
2022-07-31 12:45:09 +06:00
win.show();
2022-08-05 18:16:42 +06:00
// win.webContents.openDevTools();
2022-09-18 22:56:36 +06:00
autoUpdater.checkForUpdates();
2022-07-25 22:15:38 +06:00
}
2022-10-27 12:49:15 +06:00
let loadedLanguage;
2022-12-24 17:46:19 +06:00
const gotTheLock = app.requestSingleInstanceLock();
if (!gotTheLock) {
app.quit();
} else {
app.on("second-instance", (event, commandLine, workingDirectory) => {
// Someone tried to run a second instance, we should focus our window.
if (win) {
win.show();
}
});
}
2022-07-25 22:15:38 +06:00
app.whenReady().then(() => {
2022-09-02 00:25:25 +06:00
// Logging
console.log("Locale:" + app.getLocale());
console.log("Version: " + app.getVersion());
2022-10-27 12:49:15 +06:00
let locale = app.getLocale();
if (fs.existsSync(path.join(__dirname, "translations", locale + ".json"))) {
loadedLanguage = JSON.parse(
fs.readFileSync(
path.join(__dirname, "translations", locale + ".json"),
"utf8"
)
);
} else {
loadedLanguage = JSON.parse(
fs.readFileSync(
path.join(__dirname, "translations", "en.json"),
"utf8"
)
);
}
2022-12-15 21:52:47 +06:00
// Tray context menu
const contextMenu = Menu.buildFromTemplate([
{
label: i18n("Open app"),
click() {
win.show();
2022-12-24 17:46:19 +06:00
if (app.dock) app.dock.show();
2022-12-15 21:52:47 +06:00
},
},
{
label: i18n("Paste video link"),
click() {
const text = clipboard.readText();
if (indexIsOpen) {
win.show();
2022-12-24 17:46:19 +06:00
if (app.dock) app.dock.show();
2022-12-15 21:52:47 +06:00
win.webContents.send("link", text);
} else {
win.loadFile("html/index.html");
win.show();
indexIsOpen = true;
2022-12-17 20:41:31 +06:00
let sent = false;
ipcMain.on("ready-for-links", () => {
if (!sent) {
win.webContents.send("link", text);
sent = true;
}
});
2022-12-15 21:52:47 +06:00
}
},
},
{
label: i18n("Download playlist"),
click() {
indexIsOpen = false;
win.loadFile("html/playlist.html");
win.show();
2022-12-24 17:46:19 +06:00
if (app.dock) app.dock.show();
2022-12-15 21:52:47 +06:00
},
},
{
label: i18n("Quit"),
click() {
isQuiting = true;
app.quit();
},
},
]);
let trayInUse = false;
2022-12-23 21:03:58 +06:00
let icon;
if (process.platform == "win32") {
2022-12-24 17:46:19 +06:00
icon = path.join(__dirname, "resources/icon.ico");
2022-12-23 21:03:58 +06:00
} else if (process.platform == "darwin") {
2022-12-24 17:46:19 +06:00
icon = path.join(__dirname, "resources/icons/16x16.png");
2022-12-23 21:03:58 +06:00
} else {
2022-12-24 17:46:19 +06:00
icon = path.join(__dirname, "resources/icons/256x256.png");
2022-12-23 21:03:58 +06:00
}
2022-12-15 21:52:47 +06:00
ipcMain.on("useTray", (_, enabled) => {
if (enabled && !trayInUse) {
trayEnabled = true;
trayInUse = true;
2022-12-23 21:03:58 +06:00
tray = new Tray(icon);
2022-12-15 21:52:47 +06:00
tray.setToolTip("ytDownloader");
tray.setContextMenu(contextMenu);
tray.on("click", ()=>{
win.show();
if (app.dock) app.dock.show();
})
2022-12-15 21:52:47 +06:00
} else if (!enabled) {
trayEnabled = false;
}
});
2022-07-31 12:45:09 +06:00
createWindow();
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
if (process.platform === "win32") {
app.setAppUserModelId(app.name);
}
});
2022-07-25 22:15:38 +06:00
2022-11-20 17:42:14 +06:00
ipcMain.on("reload", () => {
if (win) {
win.reload();
}
if (secondaryWindow) {
secondaryWindow.reload();
}
2022-09-18 22:56:36 +06:00
});
2022-09-03 23:22:49 +06:00
2022-09-02 00:25:25 +06:00
ipcMain.on("get-version", () => {
const version = app.getVersion();
secondaryWindow.webContents.send("version", version);
});
2022-09-18 22:56:36 +06:00
ipcMain.on("load-win", (event, file) => {
2022-12-15 21:52:47 +06:00
if (file.includes("playlist.html")) {
2022-12-13 14:48:02 +06:00
indexIsOpen = false;
2022-12-15 21:52:47 +06:00
} else {
2022-12-13 14:48:02 +06:00
indexIsOpen = true;
}
2022-09-18 22:56:36 +06:00
win.loadFile(file);
});
2022-08-27 12:02:54 +06:00
ipcMain.on("load-page", (event, file) => {
secondaryWindow = new BrowserWindow({
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
},
parent: win,
modal: true,
show: false,
});
secondaryWindow.loadFile(file);
2022-09-05 21:47:07 +06:00
secondaryWindow.setMenu(null);
2022-09-03 23:22:49 +06:00
// secondaryWindow.maximize();
2022-08-27 12:02:54 +06:00
secondaryWindow.show();
});
ipcMain.on("close-secondary", () => {
secondaryWindow.close();
secondaryWindow = null;
2022-08-05 17:37:17 +06:00
});
2022-08-05 17:37:17 +06:00
ipcMain.on("select-location", () => {
2022-08-27 13:07:12 +06:00
const location = dialog.showOpenDialogSync(secondaryWindow, {
properties: ["openDirectory"],
2022-08-05 17:37:17 +06:00
});
2022-08-05 17:37:17 +06:00
if (location) {
2022-08-27 13:07:12 +06:00
secondaryWindow.webContents.send("downloadPath", location);
}
2022-08-05 17:37:17 +06:00
});
2022-11-25 15:53:52 +06:00
ipcMain.on("select-config", () => {
const location = dialog.showOpenDialogSync(secondaryWindow, {
properties: ["openFile"],
});
if (location) {
secondaryWindow.webContents.send("configPath", location);
}
2022-11-25 15:53:52 +06:00
});
2022-07-31 12:45:09 +06:00
// Auto updater events
autoUpdater.on("update-available", (_event, releaseNotes, releaseName) => {
2022-09-18 22:56:36 +06:00
// For macOS
if (process.platform === "darwin") {
const dialogOpts = {
type: "info",
2022-10-27 12:49:15 +06:00
buttons: [i18n("Download"), i18n("No")],
2022-09-18 22:56:36 +06:00
title: "Update Available",
detail: releaseName,
2022-10-27 12:49:15 +06:00
message: i18n(
"A new version is available, do you want to download it?"
),
2022-09-18 22:56:36 +06:00
};
dialog.showMessageBox(dialogOpts).then((returnValue) => {
if (returnValue.response === 0) {
shell.openExternal(
"https://github.com/aandrew-me/ytDownloader/releases/latest/download/YTDownloader_Mac.zip"
);
}
});
}
// For Windows and Linux
else {
const dialogOpts = {
type: "info",
2022-10-27 12:49:15 +06:00
buttons: [i18n("Update"), i18n("No")],
2022-09-18 22:56:36 +06:00
title: "Update Available",
detail: process.platform === "win32" ? releaseNotes : releaseName,
2022-10-27 12:49:15 +06:00
message: i18n("A new version is available, do you want to update?"),
2022-09-18 22:56:36 +06:00
};
dialog.showMessageBox(dialogOpts).then((returnValue) => {
if (returnValue.response === 0) {
autoUpdater.downloadUpdate();
}
});
}
2022-08-02 23:27:08 +06:00
});
2022-07-31 12:45:09 +06:00
autoUpdater.on("update-downloaded", (_event, releaseNotes, releaseName) => {
const dialogOpts = {
2022-08-02 23:27:08 +06:00
type: "info",
2022-10-27 12:49:15 +06:00
buttons: [i18n("Restart"), i18n("Later")],
2022-09-02 00:25:25 +06:00
title: "Update Ready",
2022-10-27 12:49:15 +06:00
message: i18n("Install and restart now?"),
2022-07-31 12:45:09 +06:00
};
dialog.showMessageBox(dialogOpts).then((returnValue) => {
2022-09-02 00:25:25 +06:00
if (returnValue.response === 0) {
autoUpdater.quitAndInstall();
} else {
autoUpdater.autoInstallOnAppQuit();
}
2022-08-02 23:27:08 +06:00
});
2022-07-31 19:14:15 +06:00
});
2022-10-27 12:49:15 +06:00
// Translation
function i18n(phrase) {
let translation = loadedLanguage[phrase];
if (translation === undefined) {
translation = phrase;
}
return translation;
}