diff --git a/index.js b/index.js index d3badf255c..f93b7d7b41 100644 --- a/index.js +++ b/index.js @@ -2,8 +2,6 @@ const path = require("path"); const electron = require("electron"); -const remote = require('@electron/remote/main'); -remote.initialize(); const enhanceWebRequest = require("electron-better-web-request").default; const is = require("electron-is"); const unhandled = require("electron-unhandled"); @@ -15,6 +13,7 @@ const { fileExists, injectCSS } = require("./plugins/utils"); const { isTesting } = require("./utils/testing"); const { setUpTray } = require("./tray"); const { setupSongInfo } = require("./providers/song-info"); +const { setupAppControls, restart } = require("./providers/app-controls"); // Catch errors and log them unhandled({ @@ -120,7 +119,6 @@ function createMainWindow() { contextIsolation: false, preload: path.join(__dirname, "preload.js"), nodeIntegrationInSubFrames: true, - nativeWindowOpen: true, // window.open return Window object(like in regular browsers), not BrowserWindowProxy affinity: "main-window", // main window, and addition windows should work in one process ...(isTesting() ? { @@ -138,7 +136,6 @@ function createMainWindow() { : "default", autoHideMenuBar: config.get("options.hideMenu"), }); - remote.enable(win.webContents); if (windowPosition) { const { x, y } = windowPosition; @@ -272,6 +269,7 @@ app.once("browser-window-created", (event, win) => { setupSongInfo(win); loadPlugins(win); + setupAppControls(); win.webContents.on("did-fail-load", ( _event, @@ -459,13 +457,8 @@ function showUnresponsiveDialog(win, details) { cancelId: 0 }).then( result => { switch (result.response) { - case 1: //if relaunch - relaunch+exit - app.relaunch(); - case 2: - app.quit(); - break; - default: - break; + case 1: restart(); break; + case 2: app.quit(); break; } }); } diff --git a/menu.js b/menu.js index e06a925af4..ad0e12f817 100644 --- a/menu.js +++ b/menu.js @@ -3,6 +3,7 @@ const path = require("path"); const { app, Menu, dialog } = require("electron"); const is = require("electron-is"); +const { restart } = require("./providers/app-controls"); const { getAllPlugins } = require("./plugins/utils"); const config = require("./config"); @@ -302,10 +303,7 @@ const mainMenuTemplate = (win) => { }, { label: "Restart App", - click: () => { - app.relaunch(); - app.quit(); - }, + click: restart }, { role: "quit" }, ], diff --git a/package.json b/package.json index eb59c820bb..dda4441161 100644 --- a/package.json +++ b/package.json @@ -93,7 +93,6 @@ }, "dependencies": { "@cliqz/adblocker-electron": "^1.23.7", - "@electron/remote": "^2.0.8", "@ffmpeg/core": "^0.10.0", "@ffmpeg/ffmpeg": "^0.10.1", "Simple-YouTube-Age-Restriction-Bypass": "https://gitpkg.now.sh/zerodytrash/Simple-YouTube-Age-Restriction-Bypass/dist?v2.4.6", @@ -108,7 +107,7 @@ "electron-is": "^3.0.0", "electron-localshortcut": "^3.2.1", "electron-store": "^8.0.1", - "electron-unhandled": "^3.0.2", + "electron-unhandled": "^4.0.1", "electron-updater": "^4.6.3", "filenamify": "^4.3.0", "hark": "^1.2.3", @@ -122,7 +121,7 @@ }, "devDependencies": { "auto-changelog": "^2.4.0", - "electron": "^17.0.0", + "electron": "^18.2.3", "electron-builder": "^23.0.3", "electron-devtools-installer": "^3.1.1", "electron-icon-maker": "0.0.5", diff --git a/plugins/downloader/youtube-dl.js b/plugins/downloader/youtube-dl.js index d7f34ff4bc..a6fd847439 100644 --- a/plugins/downloader/youtube-dl.js +++ b/plugins/downloader/youtube-dl.js @@ -3,7 +3,6 @@ const { join } = require("path"); const Mutex = require("async-mutex").Mutex; const { ipcRenderer } = require("electron"); -const remote = require('@electron/remote'); const is = require("electron-is"); const filenamify = require("filenamify"); @@ -137,7 +136,7 @@ const toMP3 = async ( safeVideoName + "." + extension ); - const folder = options.downloadFolder || remote.app.getPath("downloads"); + const folder = options.downloadFolder || await ipcRenderer.invoke('getDownloadsFolder'); const name = metadata.title ? `${metadata.artist ? `${metadata.artist} - ` : ""}${metadata.title}` : videoName; diff --git a/plugins/precise-volume/back.js b/plugins/precise-volume/back.js index 3d8b23acdd..c33f23bbce 100644 --- a/plugins/precise-volume/back.js +++ b/plugins/precise-volume/back.js @@ -7,9 +7,18 @@ This is used to determine if plugin is actually active */ let enabled = false; -module.exports = (win) => { +const { globalShortcut } = require('electron'); + +module.exports = (win, options) => { enabled = true; injectCSS(win.webContents, path.join(__dirname, "volume-hud.css")); + + if (options.globalShortcuts?.volumeUp) { + globalShortcut.register((options.globalShortcuts.volumeUp), () => win.webContents.send('changeVolume', true)); + } + if (options.globalShortcuts?.volumeDown) { + globalShortcut.register((options.globalShortcuts.volumeDown), () => win.webContents.send('changeVolume', false)); + } } module.exports.enabled = () => enabled; diff --git a/plugins/precise-volume/front.js b/plugins/precise-volume/front.js index 3bc3ff6b65..0bf3e36df2 100644 --- a/plugins/precise-volume/front.js +++ b/plugins/precise-volume/front.js @@ -1,22 +1,24 @@ const { ipcRenderer } = require("electron"); -const { globalShortcut } = require('@electron/remote'); const { setOptions, setMenuOptions, isEnabled } = require("../../config/plugins"); function $(selector) { return document.querySelector(selector); } -let api; -module.exports = (options) => { +let api, options; + +module.exports = (_options) => { + options = _options; document.addEventListener('apiLoaded', e => { api = e.detail; - firstRun(options); + ipcRenderer.on('changeVolume', (_, toIncrease) => changeVolume(toIncrease)); + firstRun(); }, { once: true, passive: true }) }; module.exports.moveVolumeHud = moveVolumeHud; /** Restore saved volume and setup tooltip */ -function firstRun(options) { +function firstRun() { if (typeof options.savedVolume === "number") { // Set saved volume as tooltip setTooltip(options.savedVolume); @@ -26,16 +28,14 @@ function firstRun(options) { } } - setupPlaybar(options); - - setupLocalArrowShortcuts(options); + setupPlaybar(); - setupGlobalShortcuts(options); + setupLocalArrowShortcuts(); const noVid = $("#main-panel")?.computedStyleMap().get("display").value === "none"; injectVolumeHud(noVid); if (!noVid) { - setupVideoPlayerOnwheel(options); + setupVideoPlayerOnwheel(); if (!isEnabled('video-toggle')) { //video-toggle handles hud positioning on its own const videoMode = () => api.getPlayerResponse().videoDetails?.musicVideoType !== 'MUSIC_VIDEO_TYPE_ATV'; @@ -96,22 +96,22 @@ function showVolumeHud(volume) { } /** Add onwheel event to video player */ -function setupVideoPlayerOnwheel(options) { +function setupVideoPlayerOnwheel() { $("#main-panel").addEventListener("wheel", event => { event.preventDefault(); // Event.deltaY < 0 means wheel-up - changeVolume(event.deltaY < 0, options); + changeVolume(event.deltaY < 0); }); } -function saveVolume(volume, options) { +function saveVolume(volume) { options.savedVolume = volume; - writeOptions(options); + writeOptions(); } //without this function it would rewrite config 20 time when volume change by 20 let writeTimeout; -function writeOptions(options) { +function writeOptions() { if (writeTimeout) clearTimeout(writeTimeout); writeTimeout = setTimeout(() => { @@ -121,13 +121,13 @@ function writeOptions(options) { } /** Add onwheel event to play bar and also track if play bar is hovered*/ -function setupPlaybar(options) { +function setupPlaybar() { const playerbar = $("ytmusic-player-bar"); playerbar.addEventListener("wheel", event => { event.preventDefault(); // Event.deltaY < 0 means wheel-up - changeVolume(event.deltaY < 0, options); + changeVolume(event.deltaY < 0); }); // Keep track of mouse position for showVolumeSlider() @@ -139,11 +139,11 @@ function setupPlaybar(options) { playerbar.classList.remove("on-hover"); }); - setupSliderObserver(options); + setupSliderObserver(); } /** Save volume + Update the volume tooltip when volume-slider is manually changed */ -function setupSliderObserver(options) { +function setupSliderObserver() { const sliderObserver = new MutationObserver(mutations => { for (const mutation of mutations) { // This checks that volume-slider was manually set @@ -151,7 +151,7 @@ function setupSliderObserver(options) { (typeof options.savedVolume !== "number" || Math.abs(options.savedVolume - mutation.target.value) > 4)) { // Diff>4 means it was manually set setTooltip(mutation.target.value); - saveVolume(mutation.target.value, options); + saveVolume(mutation.target.value); } } }); @@ -164,7 +164,7 @@ function setupSliderObserver(options) { } /** if (toIncrease = false) then volume decrease */ -function changeVolume(toIncrease, options) { +function changeVolume(toIncrease) { // Apply volume change if valid const steps = Number(options.steps || 1); api.setVolume(toIncrease ? @@ -172,10 +172,10 @@ function changeVolume(toIncrease, options) { Math.max(api.getVolume() - steps, 0)); // Save the new volume - saveVolume(api.getVolume(), options); + saveVolume(api.getVolume()); // change slider position (important) - updateVolumeSlider(options); + updateVolumeSlider(); // Change tooltips to new value setTooltip(options.savedVolume); @@ -185,7 +185,7 @@ function changeVolume(toIncrease, options) { showVolumeHud(options.savedVolume); } -function updateVolumeSlider(options) { +function updateVolumeSlider() { // Slider value automatically rounds to multiples of 5 for (const slider of ["#volume-slider", "#expand-volume-slider"]) { $(slider).value = @@ -228,26 +228,17 @@ function setTooltip(volume) { } } -function setupGlobalShortcuts(options) { - if (options.globalShortcuts.volumeUp) { - globalShortcut.register((options.globalShortcuts.volumeUp), () => changeVolume(true, options)); - } - if (options.globalShortcuts.volumeDown) { - globalShortcut.register((options.globalShortcuts.volumeDown), () => changeVolume(false, options)); - } -} - -function setupLocalArrowShortcuts(options) { +function setupLocalArrowShortcuts() { if (options.arrowsShortcut) { window.addEventListener('keydown', (event) => { switch (event.code) { case "ArrowUp": event.preventDefault(); - changeVolume(true, options); + changeVolume(true); break; case "ArrowDown": event.preventDefault(); - changeVolume(false, options); + changeVolume(false); break; } }); diff --git a/plugins/quality-changer/back.js b/plugins/quality-changer/back.js new file mode 100644 index 0000000000..d46b8675a8 --- /dev/null +++ b/plugins/quality-changer/back.js @@ -0,0 +1,15 @@ +const { ipcMain, dialog } = require("electron"); + +module.exports = () => { + ipcMain.handle('qualityChanger', async (_, qualityLabels, currentIndex) => { + return await dialog.showMessageBox({ + type: "question", + buttons: qualityLabels, + defaultId: currentIndex, + title: "Choose Video Quality", + message: "Choose Video Quality:", + detail: `Current Quality: ${qualityLabels[currentIndex]}`, + cancelId: -1 + }) + }) +}; diff --git a/plugins/quality-changer/front.js b/plugins/quality-changer/front.js index 4c553a9fd3..2b746d1511 100644 --- a/plugins/quality-changer/front.js +++ b/plugins/quality-changer/front.js @@ -1,5 +1,5 @@ const { ElementFromFile, templatePath } = require("../utils"); -const { dialog } = require('@electron/remote'); +const { ipcRenderer } = require("electron"); function $(selector) { return document.querySelector(selector); } @@ -20,21 +20,15 @@ function setup(event) { qualitySettingsButton.onclick = function chooseQuality() { setTimeout(() => $('#player').click()); - const currentIndex = api.getAvailableQualityLevels().indexOf(api.getPlaybackQuality()) - - dialog.showMessageBox({ - type: "question", - buttons: api.getAvailableQualityLabels(), - defaultId: currentIndex, - title: "Choose Video Quality", - message: "Choose Video Quality:", - detail: `Current Quality: ${api.getAvailableQualityLabels()[currentIndex]}`, - cancelId: -1 - }).then((promise) => { + const qualityLevels = api.getAvailableQualityLevels(); + + const currentIndex = qualityLevels.indexOf(api.getPlaybackQuality()); + + ipcRenderer.invoke('qualityChanger', api.getAvailableQualityLabels(), currentIndex).then(promise => { if (promise.response === -1) return; - const newQuality = api.getAvailableQualityLevels()[promise.response]; + const newQuality = qualityLevels[promise.response]; api.setPlaybackQualityRange(newQuality); api.setPlaybackQuality(newQuality) - }) + }); } } diff --git a/preload.js b/preload.js index 618d50ae11..f508a6ae7f 100644 --- a/preload.js +++ b/preload.js @@ -1,12 +1,10 @@ const path = require("path"); - -const remote = require('@electron/remote'); - +require("./providers/front-logger")(); const config = require("./config"); const { fileExists } = require("./plugins/utils"); -const setupFrontLogger = require("./providers/front-logger"); const setupSongInfo = require("./providers/song-info-front"); const { setupSongControls } = require("./providers/song-controls-front"); +const { ipcRenderer } = require("electron"); const plugins = config.plugins.getEnabled(); @@ -49,12 +47,8 @@ document.addEventListener("DOMContentLoaded", () => { // inject song-controls setupSongControls(); - // inject front logger - setupFrontLogger(); - // Add action for reloading - global.reload = () => - remote.getCurrentWindow().webContents.loadURL(config.get("url")); + global.reload = () => ipcRenderer.send('reload'); // Blocks the "Are You Still There?" popup by setting the last active time to Date.now every 15min setInterval(() => window._lact = Date.now(), 900000); diff --git a/providers/app-controls.js b/providers/app-controls.js index 9faded696a..d51b6901e7 100644 --- a/providers/app-controls.js +++ b/providers/app-controls.js @@ -1,6 +1,20 @@ -const app = require("electron").app || require('@electron/remote').app; +const is = require("electron-is"); + +const { app, BrowserWindow, ipcMain, ipcRenderer } = require("electron"); +const config = require("../config"); module.exports.restart = () => { - app.relaunch(); - app.exit(); + is.main() ? restart() : ipcRenderer.send('restart'); }; + +module.exports.setupAppControls = () => { + ipcMain.on('restart', restart); + ipcMain.handle('getDownloadsFolder', () => app.getPath("downloads")); + ipcMain.on('reload', () => BrowserWindow.getFocusedWindow().webContents.loadURL(config.get("url"))); +} + +function restart() { + app.relaunch({ execPath: process.env.PORTABLE_EXECUTABLE_FILE }); + // execPath will be undefined if not running portable app, resulting in default behavior + app.quit(); +} diff --git a/tray.js b/tray.js index f455d7775d..cf407aa223 100644 --- a/tray.js +++ b/tray.js @@ -2,6 +2,7 @@ const path = require("path"); const { app, Menu, nativeImage, Tray } = require("electron"); +const { restart } = require("./providers/app-controls"); const config = require("./config"); const getSongControls = require("./providers/song-controls"); @@ -65,10 +66,7 @@ module.exports.setUpTray = (app, win) => { }, { label: "Restart App", - click: () => { - app.relaunch(); - app.quit(); - }, + click: restart }, { role: "quit" }, ]; diff --git a/yarn.lock b/yarn.lock index 8c10d663ea..9c7564a01b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -716,11 +716,6 @@ global-agent "^3.0.0" global-tunnel-ng "^2.7.1" -"@electron/remote@^2.0.8": - version "2.0.8" - resolved "https://registry.yarnpkg.com/@electron/remote/-/remote-2.0.8.tgz#85ff321f0490222993207106e2f720273bb1a5c3" - integrity sha512-P10v3+iFCIvEPeYzTWWGwwHmqWnjoh8RYnbtZAb3RlQefy4guagzIwcWtfftABIfm6JJTNQf4WPSKWZOpLmHXw== - "@electron/universal@1.2.0": version "1.2.0" resolved "https://registry.yarnpkg.com/@electron/universal/-/universal-1.2.0.tgz#518cac72bccd79c00bf41345119e6fdbabdb871d" @@ -1555,11 +1550,16 @@ resolved "https://registry.yarnpkg.com/@types/ms/-/ms-0.7.31.tgz#31b7ca6407128a3d2bbc27fe2d21b345397f6197" integrity sha512-iiUgKzV9AuaEkZqkOLDIvlQiL6ltuZd9tGcW3gwpnX8JbuiuhFlEGmmFXEXkN50Cvq7Os88IY2v0dkDqXYWVgA== -"@types/node@*", "@types/node@^14.6.2": +"@types/node@*": version "14.14.44" resolved "https://registry.yarnpkg.com/@types/node/-/node-14.14.44.tgz#df7503e6002847b834371c004b372529f3f85215" integrity sha512-+gaugz6Oce6ZInfI/tK4Pq5wIIkJMEJUu92RB3Eu93mtj4wjjjz9EB5mLp5s1pSsLXdC/CPut/xF20ZzAQJbTA== +"@types/node@^16.11.26": + version "16.11.26" + resolved "https://registry.yarnpkg.com/@types/node/-/node-16.11.26.tgz#63d204d136c9916fb4dcd1b50f9740fe86884e47" + integrity sha512-GZ7bu5A6+4DtG7q9GsoHXy3ALcgeIHP4NnL0Vv2wu0uUB/yQex26v0tf6/na1mm0+bS9Uw+0DFex7aaKr2qawQ== + "@types/normalize-package-data@^2.4.0": version "2.4.0" resolved "https://registry.yarnpkg.com/@types/normalize-package-data/-/normalize-package-data-2.4.0.tgz#e486d0d97396d79beedd0a6e33f4534ff6b4973e" @@ -3184,11 +3184,16 @@ electron-is-dev@^0.3.0: resolved "https://registry.yarnpkg.com/electron-is-dev/-/electron-is-dev-0.3.0.tgz#14e6fda5c68e9e4ecbeff9ccf037cbd7c05c5afe" integrity sha1-FOb9pcaOnk7L7/nM8DfL18BcWv4= -electron-is-dev@^1.0.1, electron-is-dev@^1.1.0: +electron-is-dev@^1.1.0: version "1.2.0" resolved "https://registry.yarnpkg.com/electron-is-dev/-/electron-is-dev-1.2.0.tgz#2e5cea0a1b3ccf1c86f577cee77363ef55deb05e" integrity sha512-R1oD5gMBPS7PVU8gJwH6CtT0e6VSoD0+SzSnYpNm+dBkcijgA+K7VAMHDfnRq/lkKPZArpzplTW6jfiMYosdzw== +electron-is-dev@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/electron-is-dev/-/electron-is-dev-2.0.0.tgz#833487a069b8dad21425c67a19847d9064ab19bd" + integrity sha512-3X99K852Yoqu9AcW50qz3ibYBWY79/pBhlMCab8ToEWS48R0T9tyxRiQhwylE7zQdXrMnx2JKqUJyMPmt5FBqA== + electron-is@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/electron-is/-/electron-is-3.0.0.tgz#bd4c27651fbe03f243f3607c2a098be884f9ed50" @@ -3250,15 +3255,16 @@ electron-to-chromium@^1.3.878: resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.878.tgz#baa9fb5c24b9b580f08fb245cbb52a22f8fc8fa8" integrity sha512-O6yxWCN9ph2AdspAIszBnd9v8s11hQx8ub9w4UGApzmNRnoKhbulOWqbO8THEQec/aEHtvy+donHZMlh6l1rbA== -electron-unhandled@^3.0.2: - version "3.0.2" - resolved "https://registry.yarnpkg.com/electron-unhandled/-/electron-unhandled-3.0.2.tgz#e14a19c830ccf7b6e755191c8e78d23094d25112" - integrity sha512-IIqXnM5eNgV7k5sDA/GZ39ygJbpfF3WTArNGQ1TB4AI6ajQuuVztA0M6Mq9uEpmTh5gz4nR+YsTNWYsHLoM5rw== +electron-unhandled@^4.0.1: + version "4.0.1" + resolved "https://registry.yarnpkg.com/electron-unhandled/-/electron-unhandled-4.0.1.tgz#82bbcb5ee5423f611f6122e006bf4842dfd7c908" + integrity sha512-6BsLnBg+i96eUnbaIFZyYdyfNX3f80/Nlfqy34YEMxXT9JP3ddNsNnUeiOF8ezN4+et4t4D37gjghKTP0V3jyw== dependencies: clean-stack "^2.1.0" - electron-is-dev "^1.0.1" + electron-is-dev "^2.0.0" ensure-error "^2.0.0" lodash.debounce "^4.0.8" + serialize-error "^8.1.0" electron-updater@^4.6.3: version "4.6.5" @@ -3274,13 +3280,13 @@ electron-updater@^4.6.3: lodash.isequal "^4.5.0" semver "^7.3.5" -electron@^17.0.0: - version "17.0.0" - resolved "https://registry.yarnpkg.com/electron/-/electron-17.0.0.tgz#60f00f3e3c657020e807a519700213943468b4d1" - integrity sha512-3UXcBQMwbMWdPvGHaSdPMluHrd+/bc+K143MyvE5zVZ+S1XCHt4sau7dj6svJHns5llN0YG/c6h/vRfadIp8Zg== +electron@^18.2.3: + version "18.2.3" + resolved "https://registry.yarnpkg.com/electron/-/electron-18.2.3.tgz#36bcb8f71e41631e6b11179eeff291c8228bfd6a" + integrity sha512-DJWX03hCRKTscsfXxmW4gmgFuseop+g+m4ml7NfOMfankD8uYyr2Xyi3Ui02inL9qZOlbLMeLVCu6jKCKs8p/w== dependencies: "@electron/get" "^1.13.0" - "@types/node" "^14.6.2" + "@types/node" "^16.11.26" extract-zip "^1.0.3" emittery@^0.8.1: @@ -7415,6 +7421,13 @@ serialize-error@^7.0.1: dependencies: type-fest "^0.13.1" +serialize-error@^8.1.0: + version "8.1.0" + resolved "https://registry.yarnpkg.com/serialize-error/-/serialize-error-8.1.0.tgz#3a069970c712f78634942ddd50fbbc0eaebe2f67" + integrity sha512-3NnuWfM6vBYoy5gZFvHiYsVbafvI9vZv/+jlIigFn4oP4zjNPK3LhcY0xSCgeb1a5L8jO71Mit9LlNoi2UfDDQ== + dependencies: + type-fest "^0.20.2" + set-blocking@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/set-blocking/-/set-blocking-2.0.0.tgz#045f9782d011ae9a6803ddd382b24392b3d890f7"