Merge branch 'master' into update-custom-electron-titlebar

This commit is contained in:
Araxeus
2022-02-10 23:13:55 +02:00
committed by GitHub
18 changed files with 242 additions and 117 deletions

View File

@ -1,11 +1,17 @@
const defaultConfig = require("./defaults");
const plugins = require("./plugins");
const store = require("./store");
const { restart } = require("../providers/app-controls");
const set = (key, value) => {
store.set(key, value);
};
function setMenuOption(key, value) {
set(key, value);
if (store.get("options.restartOnConfigChanges")) restart();
}
const get = (key) => {
return store.get(key);
};
@ -14,6 +20,7 @@ module.exports = {
defaultConfig,
get,
set,
setMenuOption,
edit: () => store.openInEditor(),
watch: (cb) => {
store.onDidChange("options", cb);

View File

@ -1,4 +1,5 @@
const store = require("./store");
const { restart } = require("../providers/app-controls");
function getEnabled() {
const plugins = store.get("plugins");
@ -24,16 +25,21 @@ function setOptions(plugin, options) {
});
}
function setMenuOptions(plugin, options) {
setOptions(plugin, options);
if (store.get("options.restartOnConfigChanges")) restart();
}
function getOptions(plugin) {
return store.get("plugins")[plugin];
}
function enable(plugin) {
setOptions(plugin, { enabled: true });
setMenuOptions(plugin, { enabled: true });
}
function disable(plugin) {
setOptions(plugin, { enabled: false });
setMenuOptions(plugin, { enabled: false });
}
module.exports = {
@ -42,5 +48,6 @@ module.exports = {
enable,
disable,
setOptions,
setMenuOptions,
getOptions,
};

View File

@ -26,6 +26,22 @@ unhandled({
process.env.NODE_OPTIONS = "";
const app = electron.app;
// Prevent window being garbage collected
let mainWindow;
autoUpdater.autoDownload = false;
if(config.get("options.singleInstanceLock")){
const gotTheLock = app.requestSingleInstanceLock();
if (!gotTheLock) app.quit();
app.on('second-instance', () => {
if (!mainWindow) return;
if (mainWindow.isMinimized()) mainWindow.restore();
if (!mainWindow.isVisible()) mainWindow.show();
mainWindow.focus();
});
}
app.commandLine.appendSwitch(
"js-flags",
// WebAssembly flags
@ -54,10 +70,6 @@ require("electron-debug")({
showDevTools: false //disable automatic devTools on new window
});
// Prevent window being garbage collected
let mainWindow;
autoUpdater.autoDownload = false;
let icon = "assets/youtube-music.png";
if (process.platform == "win32") {
icon = "assets/generated/icon.ico";
@ -160,22 +172,39 @@ function createMainWindow() {
win.on("closed", onClosed);
win.on("move", () => {
if (win.isMaximized()) return;
let position = win.getPosition();
config.set("window-position", { x: position[0], y: position[1] });
lateSave("window-position", { x: position[0], y: position[1] });
});
let winWasMaximized;
win.on("resize", () => {
const windowSize = win.getSize();
config.set("window-maximized", win.isMaximized());
if (!win.isMaximized()) {
config.set("window-size", {
const isMaximized = win.isMaximized();
if (winWasMaximized !== isMaximized) {
winWasMaximized = isMaximized;
config.set("window-maximized", isMaximized);
}
if (!isMaximized) {
lateSave("window-size", {
width: windowSize[0],
height: windowSize[1],
});
}
});
let savedTimeouts = {};
function lateSave(key, value) {
if (savedTimeouts[key]) clearTimeout(savedTimeouts[key]);
savedTimeouts[key] = setTimeout(() => {
config.set(key, value);
savedTimeouts[key] = undefined;
}, 1000)
}
win.webContents.on("render-process-gone", (event, webContents, details) => {
showUnresponsiveDialog(win, details);
});
@ -192,30 +221,31 @@ function createMainWindow() {
}
app.once("browser-window-created", (event, win) => {
// User agents are from https://developers.whatismybrowser.com/useragents/explore/
const originalUserAgent = win.webContents.userAgent;
const userAgents = {
mac: "Mozilla/5.0 (Macintosh; Intel Mac OS X 12.1; rv:95.0) Gecko/20100101 Firefox/95.0",
windows: "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:95.0) Gecko/20100101 Firefox/95.0",
linux: "Mozilla/5.0 (Linux x86_64; rv:95.0) Gecko/20100101 Firefox/95.0",
}
const updatedUserAgent =
is.macOS() ? userAgents.mac :
is.windows() ? userAgents.windows :
userAgents.linux;
win.webContents.userAgent = updatedUserAgent;
app.userAgentFallback = updatedUserAgent;
win.webContents.session.webRequest.onBeforeSendHeaders((details, cb) => {
// this will only happen if login failed, and "retry" was pressed
if (win.webContents.getURL().startsWith("https://accounts.google.com") && details.url.startsWith("https://accounts.google.com")){
details.requestHeaders["User-Agent"] = originalUserAgent;
if (config.get("options.overrideUserAgent")) {
// User agents are from https://developers.whatismybrowser.com/useragents/explore/
const originalUserAgent = win.webContents.userAgent;
const userAgents = {
mac: "Mozilla/5.0 (Macintosh; Intel Mac OS X 12.1; rv:95.0) Gecko/20100101 Firefox/95.0",
windows: "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:95.0) Gecko/20100101 Firefox/95.0",
linux: "Mozilla/5.0 (Linux x86_64; rv:95.0) Gecko/20100101 Firefox/95.0",
}
cb({ requestHeaders: details.requestHeaders });
});
const updatedUserAgent =
is.macOS() ? userAgents.mac :
is.windows() ? userAgents.windows :
userAgents.linux;
win.webContents.userAgent = updatedUserAgent;
app.userAgentFallback = updatedUserAgent;
win.webContents.session.webRequest.onBeforeSendHeaders((details, cb) => {
// this will only happen if login failed, and "retry" was pressed
if (win.webContents.getURL().startsWith("https://accounts.google.com") && details.url.startsWith("https://accounts.google.com")) {
details.requestHeaders["User-Agent"] = originalUserAgent;
}
cb({ requestHeaders: details.requestHeaders });
});
}
setupSongInfo(win);
loadPlugins(win);
@ -325,12 +355,6 @@ app.on("ready", () => {
mainWindow = createMainWindow();
setApplicationMenu(mainWindow);
if (config.get("options.restartOnConfigChanges")) {
config.watch(() => {
app.relaunch();
app.exit();
});
}
setUpTray(app, mainWindow);
// Autostart at login

69
menu.js
View File

@ -68,7 +68,7 @@ const mainMenuTemplate = (win) => {
type: "checkbox",
checked: config.get("options.autoUpdates"),
click: (item) => {
config.set("options.autoUpdates", item.checked);
config.setMenuOption("options.autoUpdates", item.checked);
},
},
{
@ -76,7 +76,7 @@ const mainMenuTemplate = (win) => {
type: "checkbox",
checked: config.get("options.resumeOnStart"),
click: (item) => {
config.set("options.resumeOnStart", item.checked);
config.setMenuOption("options.resumeOnStart", item.checked);
},
},
{
@ -84,7 +84,20 @@ const mainMenuTemplate = (win) => {
type: "checkbox",
checked: config.get("options.removeUpgradeButton"),
click: (item) => {
config.set("options.removeUpgradeButton", item.checked);
config.setMenuOption("options.removeUpgradeButton", item.checked);
},
},
{
label: "Single instance lock",
type: "checkbox",
checked: config.get("options.singleInstanceLock"),
click: (item) => {
config.set("options.singleInstanceLock", item.checked);
if (item.checked && !app.hasSingleInstanceLock()) {
app.requestSingleInstanceLock();
} else if (!item.checked && app.hasSingleInstanceLock()) {
app.releaseSingleInstanceLock();
}
},
},
...(is.windows() || is.linux()
@ -94,7 +107,7 @@ const mainMenuTemplate = (win) => {
type: "checkbox",
checked: config.get("options.hideMenu"),
click: (item) => {
config.set("options.hideMenu", item.checked);
config.setMenuOption("options.hideMenu", item.checked);
if (item.checked && !config.get("options.hideMenuWarned")) {
dialog.showMessageBox(win, {
type: 'info', title: 'Hide Menu Enabled',
@ -114,7 +127,7 @@ const mainMenuTemplate = (win) => {
type: "checkbox",
checked: config.get("options.startAtLogin"),
click: (item) => {
config.set("options.startAtLogin", item.checked);
config.setMenuOption("options.startAtLogin", item.checked);
},
},
]
@ -127,8 +140,8 @@ const mainMenuTemplate = (win) => {
type: "radio",
checked: !config.get("options.tray"),
click: () => {
config.set("options.tray", false);
config.set("options.appVisible", true);
config.setMenuOption("options.tray", false);
config.setMenuOption("options.appVisible", true);
},
},
{
@ -137,8 +150,8 @@ const mainMenuTemplate = (win) => {
checked:
config.get("options.tray") && config.get("options.appVisible"),
click: () => {
config.set("options.tray", true);
config.set("options.appVisible", true);
config.setMenuOption("options.tray", true);
config.setMenuOption("options.appVisible", true);
},
},
{
@ -147,8 +160,8 @@ const mainMenuTemplate = (win) => {
checked:
config.get("options.tray") && !config.get("options.appVisible"),
click: () => {
config.set("options.tray", true);
config.set("options.appVisible", false);
config.setMenuOption("options.tray", true);
config.setMenuOption("options.appVisible", false);
},
},
{ type: "separator" },
@ -157,7 +170,7 @@ const mainMenuTemplate = (win) => {
type: "checkbox",
checked: config.get("options.trayClickPlayPause"),
click: (item) => {
config.set("options.trayClickPlayPause", item.checked);
config.setMenuOption("options.trayClickPlayPause", item.checked);
},
},
],
@ -166,20 +179,28 @@ const mainMenuTemplate = (win) => {
{
label: "Advanced options",
submenu: [
{
label: "Proxy",
type: "checkbox",
checked: !!config.get("options.proxy"),
click: (item) => {
setProxy(item, win);
},
},
{
label: "Proxy",
type: "checkbox",
checked: !!config.get("options.proxy"),
click: (item) => {
setProxy(item, win);
},
},
{
label: "Override useragent",
type: "checkbox",
checked: config.get("options.overrideUserAgent"),
click: (item) => {
config.setMenuOption("options.overrideUserAgent", item.checked);
}
},
{
label: "Disable hardware acceleration",
type: "checkbox",
checked: config.get("options.disableHardwareAcceleration"),
click: (item) => {
config.set("options.disableHardwareAcceleration", item.checked);
config.setMenuOption("options.disableHardwareAcceleration", item.checked);
},
},
{
@ -187,7 +208,7 @@ const mainMenuTemplate = (win) => {
type: "checkbox",
checked: config.get("options.restartOnConfigChanges"),
click: (item) => {
config.set("options.restartOnConfigChanges", item.checked);
config.setMenuOption("options.restartOnConfigChanges", item.checked);
},
},
{
@ -195,7 +216,7 @@ const mainMenuTemplate = (win) => {
type: "checkbox",
checked: config.get("options.autoResetAppCache"),
click: (item) => {
config.set("options.autoResetAppCache", item.checked);
config.setMenuOption("options.autoResetAppCache", item.checked);
},
},
{ type: "separator" },
@ -316,7 +337,7 @@ async function setProxy(item, win) {
}, win);
if (typeof output === "string") {
config.set("options.proxy", output);
config.setMenuOption("options.proxy", output);
item.checked = output !== "";
} else { //user pressed cancel
item.checked = !item.checked; //reset checkbox

View File

@ -1,7 +1,7 @@
{
"name": "youtube-music",
"productName": "YouTube Music",
"version": "1.15.0",
"version": "1.15.1",
"description": "YouTube Music Desktop App - including custom plugins",
"license": "MIT",
"repository": "th-ch/youtube-music",
@ -89,7 +89,7 @@
"async-mutex": "^0.3.2",
"browser-id3-writer": "^4.4.0",
"chokidar": "^3.5.3",
"custom-electron-prompt": "^1.4.0",
"custom-electron-prompt": "^1.4.1",
"custom-electron-titlebar": "^4.1.0",
"discord-rpc": "^4.0.1",
"electron-better-web-request": "^1.0.1",

View File

@ -1,5 +1,7 @@
const { setOptions } = require("../../config/plugins");
const { edit } = require("../../config");
const prompt = require("custom-electron-prompt");
const { setMenuOptions } = require("../../config/plugins");
const promptOptions = require("../../providers/prompt-options");
const { clear, connect, registerRefresh, isConnected } = require("./back");
let hasRegisterred = false;
@ -26,7 +28,7 @@ module.exports = (win, options, refreshMenu) => {
checked: options.activityTimoutEnabled,
click: (item) => {
options.activityTimoutEnabled = item.checked;
setOptions('discord', options);
setMenuOptions('discord', options);
},
},
{
@ -35,13 +37,29 @@ module.exports = (win, options, refreshMenu) => {
checked: options.listenAlong,
click: (item) => {
options.listenAlong = item.checked;
setOptions('discord', options);
setMenuOptions('discord', options);
},
},
{
label: "Set timeout time in config",
// open config.json
click: edit,
label: "Set inactivity timeout",
click: () => setInactivityTimeout(win, options),
},
];
};
async function setInactivityTimeout(win, options) {
let output = await prompt({
title: 'Set Inactivity Timeout',
label: 'Enter inactivity timeout in seconds:',
value: Math.round((options.activityTimoutTime ?? 0) / 1e3),
type: "counter",
counterOptions: { minimum: 0, multiFire: true },
width: 450,
...promptOptions()
}, win)
if (output) {
options.activityTimoutTime = Math.round(output * 1e3);
setMenuOptions("discord", options);
}
}

View File

@ -7,7 +7,7 @@ const ytpl = require("ytpl");
const chokidar = require('chokidar');
const filenamify = require('filenamify');
const { setOptions } = require("../../config/plugins");
const { setMenuOptions } = require("../../config/plugins");
const { sendError } = require("./back");
const { defaultMenuDownloadLabel, getFolder, presets, setBadge } = require("./utils");
@ -49,7 +49,7 @@ module.exports = (win, options) => {
});
if (result) {
options.downloadFolder = result[0];
setOptions("downloader", options);
setMenuOptions("downloader", options);
} // else = user pressed cancel
},
},
@ -60,7 +60,7 @@ module.exports = (win, options) => {
type: "radio",
click: () => {
options.preset = preset;
setOptions("downloader", options);
setMenuOptions("downloader", options);
},
checked: options.preset === preset || presets[preset] === undefined,
})),

View File

@ -3,20 +3,34 @@ const is = require("electron-is");
module.exports = () => {
ipcRenderer.on("update-song-info", (_, extractedSongInfo) => {
const lyricsTab = document.querySelector('tp-yt-paper-tab[tabindex="-1"]');
const tabList = document.querySelectorAll("tp-yt-paper-tab");
const tabs = {
upNext: tabList[0],
lyrics: tabList[1],
discover: tabList[2],
}
// Check if disabled
if (!lyricsTab || !lyricsTab.hasAttribute("disabled")) {
if (!tabs.lyrics?.hasAttribute("disabled")) {
return;
}
let hasLyrics = true;
const html = ipcRenderer.sendSync(
"search-genius-lyrics",
extractedSongInfo
);
if (!html) {
// Delete previous lyrics if tab is open and couldn't get new lyrics
checkLyricsContainer(() => {
hasLyrics = false;
setTabsOnclick(undefined);
});
return;
} else if (is.dev()) {
}
if (is.dev()) {
console.log("Fetched lyrics from Genius");
}
@ -35,27 +49,16 @@ module.exports = () => {
return;
}
lyricsTab.removeAttribute("disabled");
lyricsTab.removeAttribute("aria-disabled");
document.querySelector("tp-yt-paper-tab").onclick = () => {
lyricsTab.removeAttribute("disabled");
lyricsTab.removeAttribute("aria-disabled");
};
enableLyricsTab();
lyricsTab.onclick = () => {
setTabsOnclick(enableLyricsTab);
checkLyricsContainer();
tabs.lyrics.onclick = () => {
const tabContainer = document.querySelector("ytmusic-tab-renderer");
const observer = new MutationObserver((_, observer) => {
const lyricsContainer = document.querySelector(
'[page-type="MUSIC_PAGE_TYPE_TRACK_LYRICS"] > ytmusic-message-renderer'
);
if (lyricsContainer) {
lyricsContainer.innerHTML = `<div id="contents" class="style-scope ytmusic-section-list-renderer genius-lyrics">
${lyrics}
<yt-formatted-string class="footer style-scope ytmusic-description-shelf-renderer">Source&nbsp;: Genius</yt-formatted-string>
</div>`;
observer.disconnect();
}
checkLyricsContainer(() => observer.disconnect());
});
observer.observe(tabContainer, {
attributes: true,
@ -63,5 +66,41 @@ module.exports = () => {
subtree: true,
});
};
function checkLyricsContainer(callback = () => {}) {
const lyricsContainer = document.querySelector(
'[page-type="MUSIC_PAGE_TYPE_TRACK_LYRICS"] > ytmusic-message-renderer'
);
if (lyricsContainer) {
callback();
setLyrics(lyricsContainer)
}
}
function setLyrics(lyricsContainer) {
lyricsContainer.innerHTML =
`<div id="contents" class="style-scope ytmusic-section-list-renderer description ytmusic-description-shelf-renderer genius-lyrics">
${hasLyrics ? lyrics : 'Could not retrieve lyrics from genius'}
</div>
<yt-formatted-string class="footer style-scope ytmusic-description-shelf-renderer" style="align-self: baseline"></yt-formatted-string>`;
if (hasLyrics) {
lyricsContainer.querySelector('.footer').textContent = 'Source: Genius';
enableLyricsTab();
}
}
function setTabsOnclick(callback) {
for (const tab of [tabs.upNext, tabs.discover]) {
if (tab) {
tab.onclick = callback;
}
}
}
function enableLyricsTab() {
tabs.lyrics.removeAttribute("disabled");
tabs.lyrics.removeAttribute("aria-disabled");
}
});
};

View File

@ -6,8 +6,7 @@
text-decoration: none;
}
#contents.genius-lyrics {
font-size: 1vw;
opacity: 0.9;
text-align: center;
.description {
font-size: 1.1vw !important;
text-align: center !important;
}

View File

@ -1,4 +1,4 @@
const { setOptions } = require("../../config/plugins");
const { setMenuOptions } = require("../../config/plugins");
const path = require("path");
const { app } = require("electron");
const fs = require("fs");
@ -15,7 +15,7 @@ module.exports.icons = {
module.exports.setOption = (options, option, value) => {
options[option] = value;
setOptions("notifications", options)
setMenuOptions("notifications", options)
}
module.exports.urgencyLevels = [

View File

@ -1,7 +1,7 @@
const { ipcRenderer } = require("electron");
const { globalShortcut } = require('@electron/remote');
const { setOptions, isEnabled } = require("../../config/plugins");
const { setOptions, setMenuOptions, isEnabled } = require("../../config/plugins");
function $(selector) { return document.querySelector(selector); }
let api;
@ -48,7 +48,7 @@ function firstRun(options) {
for (option in newOptions) {
options[option] = newOptions[option];
}
setOptions("precise-volume", options);
setMenuOptions("precise-volume", options);
});
}

View File

@ -1,5 +1,5 @@
const { enabled } = require("./back");
const { setOptions } = require("../../config/plugins");
const { setMenuOptions } = require("../../config/plugins");
const prompt = require("custom-electron-prompt");
const promptOptions = require("../../providers/prompt-options");
@ -11,7 +11,7 @@ function changeOptions(changedOptions, options, win) {
if (enabled()) {
win.webContents.send("setOptions", changedOptions);
} else { // Fallback to usual method if disabled
setOptions("precise-volume", options);
setMenuOptions("precise-volume", options);
}
}

View File

@ -1,4 +1,4 @@
const { setOptions } = require("../../config/plugins");
const { setMenuOptions } = require("../../config/plugins");
const prompt = require("custom-electron-prompt");
const promptOptions = require("../../providers/prompt-options");
@ -20,7 +20,7 @@ function setOption(options, key = null, newValue = null) {
options[key] = newValue;
}
setOptions("shortcuts", options);
setMenuOptions("shortcuts", options);
}
// Helper function for keybind prompt

View File

@ -1,4 +1,4 @@
const { setOptions } = require("../../config/plugins");
const { setMenuOptions } = require("../../config/plugins");
module.exports = (win, options) => [
{
@ -7,7 +7,7 @@ module.exports = (win, options) => [
checked: options.forceHide,
click: item => {
options.forceHide = item.checked;
setOptions("video-toggle", options);
setMenuOptions("video-toggle", options);
}
}
];

View File

@ -0,0 +1,6 @@
const app = require("electron").app || require('@electron/remote').app;
module.exports.restart = () => {
app.relaunch();
app.exit();
};

View File

@ -6,7 +6,8 @@ const config = require("../config");
global.songInfo = {};
function $(selector) { return document.querySelector(selector); }
const $ = s => document.querySelector(s);
const $$ = s => Array.from(document.querySelectorAll(s));
ipcRenderer.on("update-song-info", async (_, extractedSongInfo) => {
global.songInfo = JSON.parse(extractedSongInfo);
@ -43,7 +44,11 @@ module.exports = () => {
function sendSongInfo() {
const data = apiEvent.detail.getPlayerResponse();
data.videoDetails.album = $('ytmusic-player-page')?.__data?.playerPageWatchMetadata?.albumName?.runs[0].text
data.videoDetails.album = $$(
".byline.ytmusic-player-bar > .yt-simple-endpoint"
).find(e => e.href?.includes("browse"))?.textContent;
data.videoDetails.elapsedSeconds = Math.floor(video.currentTime);
data.videoDetails.isPaused = false;
ipcRenderer.send("video-src-changed", JSON.stringify(data));

View File

@ -112,13 +112,12 @@ const suffixesToRemove = [
" - topic",
"vevo",
" (performance video)",
" (official music video)",
" (official video)",
" (clip officiel)",
];
function cleanupName(name) {
if (!name) return name;
name = name.replace(/\((?:official)?[ ]?(?:music)?[ ]?(?:lyric[s]?)?[ ]?(?:video)?\)$/i, '')
const lowCaseName = name.toLowerCase();
for (const suffix of suffixesToRemove) {
if (lowCaseName.endsWith(suffix)) {

View File

@ -2725,10 +2725,10 @@ cssstyle@^2.3.0:
dependencies:
cssom "~0.3.6"
custom-electron-prompt@^1.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/custom-electron-prompt/-/custom-electron-prompt-1.4.0.tgz#04d261372807b87ec07ed12e990306d5debd5667"
integrity sha512-tqDa3yDILVI3xxwNRW1m2fyQgdMYtFcnl1d3uMx8Tt6sQ9zG+Y4n/ie4VbttQaq7dJvFCu9K3JX65K8hfaSf1g==
custom-electron-prompt@^1.4.1:
version "1.4.1"
resolved "https://registry.yarnpkg.com/custom-electron-prompt/-/custom-electron-prompt-1.4.1.tgz#79adc3d5cd9a372e5dfe43b21de70f0fe7d1c2f7"
integrity sha512-bR6JhEusBxKnooXfFFlIIUhDbF23eaDhaYwvqcw3ZTcdEzzJew63+ilwhIwD7flRAO+sCroaLwP495VBexHg/A==
custom-electron-titlebar@^3.2.10:
version "3.2.10"