Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update electron & remote & user agents #515

Merged
merged 13 commits into from
Dec 30, 2021
53 changes: 27 additions & 26 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
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");
Expand Down Expand Up @@ -98,7 +100,6 @@ function createMainWindow() {
preload: path.join(__dirname, "preload.js"),
nodeIntegrationInSubFrames: true,
nativeWindowOpen: true, // window.open return Window object(like in regular browsers), not BrowserWindowProxy
enableRemoteModule: true,
affinity: "main-window", // main window, and addition windows should work in one process
...(isTesting()
? {
Expand All @@ -116,6 +117,7 @@ function createMainWindow() {
: "default",
autoHideMenuBar: config.get("options.hideMenu"),
});
remote.enable(win.webContents);
if (windowPosition) {
const { x, y } = windowPosition;
win.setPosition(x, y);
Expand Down Expand Up @@ -163,6 +165,30 @@ 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 = {
Araxeus marked this conversation as resolved.
Show resolved Hide resolved
mac: "Mozilla/5.0 (Macintosh; Intel Mac OS X 12.1; rv:95.0) Gecko/20100101 Firefox/95.0",
Araxeus marked this conversation as resolved.
Show resolved Hide resolved
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) => {
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);

Expand Down Expand Up @@ -197,31 +223,6 @@ app.once("browser-window-created", (event, win) => {
event.preventDefault();
});

win.webContents.on("will-navigate", (_, url) => {
if (url.startsWith("https://accounts.google.com")) {
// Force user-agent "Firefox Windows" for Google OAuth to work
// From https:/firebase/firebase-js-sdk/issues/2478#issuecomment-571356751
// Only set on accounts.google.com, otherwise querySelectors in preload scripts fail (?)
// Uses custom user agent to Google alert with a correct device type (https:/th-ch/youtube-music/issues/327)
// User agents are from https://developers.whatismybrowser.com/useragents/explore/
const userAgents = {
mac: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:70.0) Gecko/20100101 Firefox/70.0",
windows: "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:90.0) Gecko/20100101 Firefox/90.0",
linux: "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:90.0) Gecko/20100101 Firefox/90.0",
}

const userAgent =
is.macOS() ? userAgents.mac :
is.windows() ? userAgents.windows :
userAgents.linux;

win.webContents.session.webRequest.onBeforeSendHeaders((details, cb) => {
details.requestHeaders["User-Agent"] = userAgent;
cb({ requestHeaders: details.requestHeaders });
});
}
});

win.webContents.on(
"new-window",
(e, url, frameName, disposition, options) => {
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,14 @@
},
"dependencies": {
"@cliqz/adblocker-electron": "^1.23.0",
"@electron/remote": "^2.0.1",
"@ffmpeg/core": "^0.10.0",
"@ffmpeg/ffmpeg": "^0.10.0",
"@rozzzly/custom-electron-titlebar": "^3.3.1",
"async-mutex": "^0.3.2",
"browser-id3-writer": "^4.4.0",
"custom-electron-prompt": "^1.3.1",
"chokidar": "^3.5.2",
"custom-electron-titlebar": "^3.2.7",
"custom-electron-prompt": "^1.3.1",
"discord-rpc": "^3.2.0",
"electron-better-web-request": "^1.0.1",
"electron-debug": "^3.2.0",
Expand All @@ -88,7 +89,7 @@
"ytpl": "^2.2.3"
},
"devDependencies": {
"electron": "^12.2.2",
"electron": "^16.0.2",
Araxeus marked this conversation as resolved.
Show resolved Hide resolved
"electron-builder": "^22.10.5",
"electron-devtools-installer": "^3.1.1",
"electron-icon-maker": "0.0.5",
Expand Down
3 changes: 2 additions & 1 deletion plugins/downloader/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const electron = require("electron");
//const remote = require('@electron/remote');

module.exports.getFolder = (customFolder) =>
customFolder || (electron.app || electron.remote.app).getPath("downloads");
customFolder || electron.app.getPath("downloads");
module.exports.defaultMenuDownloadLabel = "Download playlist";

const orderedQualityList = ["maxresdefault", "hqdefault", "mqdefault", "sdddefault"];
Expand Down
8 changes: 5 additions & 3 deletions plugins/in-app-menu/front.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const { remote, ipcRenderer } = require("electron");
const { ipcRenderer } = require("electron");
const { Menu } = require("@electron/remote");

const customTitlebar = require("custom-electron-titlebar");

const customTitlebar = require("@rozzzly/custom-electron-titlebar");
function $(selector) { return document.querySelector(selector); }

module.exports = () => {
Expand All @@ -12,7 +14,7 @@ module.exports = () => {
document.title = "Youtube Music";

ipcRenderer.on("updateMenu", function (_event, showMenu) {
bar.updateMenu(showMenu ? remote.Menu.getApplicationMenu() : null);
bar.updateMenu(showMenu ? Menu.getApplicationMenu() : null);
});

// Increases the right margin of Navbar background when the scrollbar is visible to avoid blocking it (z-index doesn't affect it)
Expand Down
7 changes: 4 additions & 3 deletions plugins/precise-volume/front.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { ipcRenderer, remote } = require("electron");
const { ipcRenderer } = require("electron");
const { globalShortcut } = require('@electron/remote');

const { setOptions } = require("../../config/plugins");

Expand Down Expand Up @@ -202,10 +203,10 @@ function setTooltip(volume) {

function setupGlobalShortcuts(options) {
if (options.globalShortcuts.volumeUp) {
remote.globalShortcut.register((options.globalShortcuts.volumeUp), () => changeVolume(true, options));
globalShortcut.register((options.globalShortcuts.volumeUp), () => changeVolume(true, options));
}
if (options.globalShortcuts.volumeDown) {
remote.globalShortcut.register((options.globalShortcuts.volumeDown), () => changeVolume(false, options));
globalShortcut.register((options.globalShortcuts.volumeDown), () => changeVolume(false, options));
}
}

Expand Down
2 changes: 1 addition & 1 deletion plugins/quality-changer/front.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { ElementFromFile, templatePath } = require("../utils");
const dialog = require('electron').remote.dialog
const { dialog } = require('@electron/remote');

function $(selector) { return document.querySelector(selector); }

Expand Down
4 changes: 2 additions & 2 deletions plugins/video-toggle/front.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function changeDisplay(showVideo) {
}

function videoStarted() {
if (player.videoMode_) {
if (api.getPlayerResponse().videoDetails.musicVideoType !== 'MUSIC_VIDEO_TYPE_ATV') {
// switch to high res thumbnail
forceThumbnail($('#song-image img'));
// show toggle button
Expand Down Expand Up @@ -98,7 +98,7 @@ function moveVolumeHud(showVideo) {

function observeThumbnail() {
const playbackModeObserver = new MutationObserver(mutations => {
if (!$('#player').videoMode_) return;
if (!player.videoMode_) return;

mutations.forEach(mutation => {
if (!mutation.target.src.startsWith('data:')) return;
Expand Down
2 changes: 1 addition & 1 deletion preload.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const path = require("path");

const { remote } = require("electron");
const remote = require('@electron/remote');

const config = require("./config");
const { fileExists } = require("./plugins/utils");
Expand Down
62 changes: 51 additions & 11 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@
ajv "^6.12.0"
ajv-keywords "^3.4.1"

"@electron/get@^1.0.1", "@electron/get@^1.12.4":
"@electron/get@^1.12.4":
version "1.12.4"
resolved "https://registry.yarnpkg.com/@electron/get/-/get-1.12.4.tgz#a5971113fc1bf8fa12a8789dc20152a7359f06ab"
integrity sha512-6nr9DbJPUR9Xujw6zD3y+rS95TyItEVM0NVjt1EehY2vUWfIgPiIPVHxCvaTS0xr2B+DRxovYVKbuOWqC35kjg==
Expand All @@ -716,11 +716,37 @@
global-agent "^2.0.2"
global-tunnel-ng "^2.7.1"

"@electron/get@^1.13.0":
version "1.13.1"
resolved "https://registry.yarnpkg.com/@electron/get/-/get-1.13.1.tgz#42a0aa62fd1189638bd966e23effaebb16108368"
integrity sha512-U5vkXDZ9DwXtkPqlB45tfYnnYBN8PePp1z/XDCupnSpdrxT8/ThCv9WCwPLf9oqiSGZTkH6dx2jDUPuoXpjkcA==
dependencies:
debug "^4.1.1"
env-paths "^2.2.0"
fs-extra "^8.1.0"
got "^9.6.0"
progress "^2.0.3"
semver "^6.2.0"
sumchecker "^3.0.1"
optionalDependencies:
global-agent "^3.0.0"
global-tunnel-ng "^2.7.1"

"@electron/remote@^1.0.4":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@electron/remote/-/remote-1.1.0.tgz#167d119c7c03c7778b556fdc4f1f38a44b23f1c2"
integrity sha512-yr8gZTkIgJYKbFqExI4QZqMSjn1kL/us9Dl46+TH1EZdhgRtsJ6HDfdsIxu0QEc6Hv+DMAXs69rgquH+8FDk4w==

"@electron/remote@^1.1.0":
version "1.2.2"
resolved "https://registry.yarnpkg.com/@electron/remote/-/remote-1.2.2.tgz#4c390a2e669df47af973c09eec106162a296c323"
integrity sha512-PfnXpQGWh4vpX866NNucJRnNOzDRZcsLcLaT32fUth9k0hccsohfxprqEDYLzRg+ZK2xRrtyUN5wYYoHimMCJg==

"@electron/remote@^2.0.1":
version "2.0.1"
resolved "https://registry.yarnpkg.com/@electron/remote/-/remote-2.0.1.tgz#810cbc595a21f0f94641eb2d7e8264063a3f84de"
integrity sha512-bGX4/yB2bPZwXm1DsxgoABgH0Cz7oFtXJgkerB8VrStYdTyvhGAULzNLRn9rVmeAuC3VUDXaXpZIlZAZHpsLIA==

"@electron/[email protected]":
version "1.0.5"
resolved "https://registry.yarnpkg.com/@electron/universal/-/universal-1.0.5.tgz#b812340e4ef21da2b3ee77b2b4d35c9b86defe37"
Expand Down Expand Up @@ -1344,6 +1370,13 @@
resolved "https://registry.yarnpkg.com/@remusao/trie/-/trie-1.4.1.tgz#755d09f8a007476334e611f42719b2d581f00720"
integrity sha512-yvwa+aCyYI/UjeD39BnpMypG8N06l86wIDW1/PAc6ihBRnodIfZDwccxQN3n1t74wduzaz74m4ZMHZnB06567Q==

"@rozzzly/custom-electron-titlebar@^3.3.1":
version "3.3.1"
resolved "https://registry.yarnpkg.com/@rozzzly/custom-electron-titlebar/-/custom-electron-titlebar-3.3.1.tgz#1900604ac9f8b818f8fc69f7bd45addab818acdd"
integrity sha512-TshwwVRYydmrzU/7VKYeEPY7Xgf+yZcOUl3dH3AkZ+Iqmyyb8MwzgLbY9Jc6lAyHjbHDa0vgE3aClIsUYf4EQQ==
dependencies:
"@electron/remote" "^1.1.0"

"@sindresorhus/is@^0.14.0":
version "0.14.0"
resolved "https://registry.yarnpkg.com/@sindresorhus/is/-/is-0.14.0.tgz#9fb3a3cf3132328151f353de4632e01e52102bea"
Expand Down Expand Up @@ -2908,11 +2941,6 @@ custom-electron-prompt@^1.3.1:
resolved "https://registry.yarnpkg.com/custom-electron-prompt/-/custom-electron-prompt-1.3.1.tgz#5c5c1266bb94ca618c493bdbebef2732286a06f2"
integrity sha512-QKq0H87G1EQS6QEc1lNmPrMj+J/h/1F4BAcmH2UQ+JUX9MOHZKZvbuIMEdsjYHlgI42SUAcnQklJ9F18ZCAc3A==

custom-electron-titlebar@^3.2.7:
version "3.2.7"
resolved "https://registry.yarnpkg.com/custom-electron-titlebar/-/custom-electron-titlebar-3.2.7.tgz#fb249d6180cbda074b1d392bea755fa0743012a8"
integrity sha512-KO/6e3r6YflfNUOzi5QHLwkLHBP+ICtHPo70u/kUIKR8UUkDTPb4a9i19q0uDZQcjkH6oqRvFCz9wEHeEpCgxw==

dashdash@^1.12.0:
version "1.14.1"
resolved "https://registry.yarnpkg.com/dashdash/-/dashdash-1.14.1.tgz#853cfa0f7cbe2fed5de20326b8dd581035f6e2f0"
Expand Down Expand Up @@ -3412,12 +3440,12 @@ electron-updater@^4.6.1:
lodash.isequal "^4.5.0"
semver "^7.3.5"

electron@^12.2.2:
version "12.2.2"
resolved "https://registry.yarnpkg.com/electron/-/electron-12.2.2.tgz#9627594d6b5bb589f00355989d316b6542539e54"
integrity sha512-Oma/nIfvgql9JjAxdB9gQk//qxpJaI6PgMocYMiW4kFyLi+8jS6oGn33QG3FESS//cw09KRnWmA9iutuFAuXtw==
electron@^16.0.2:
version "16.0.4"
resolved "https://registry.yarnpkg.com/electron/-/electron-16.0.4.tgz#87dfe21d17608537fc6df292c437362297566a73"
integrity sha512-IptwmowvMP1SFOmZLh6rrURwfnOxbDBXBRBcaOdfBM5I+B9mgtdNwzNC3ymFFNzEkZUwdOyg9fu3iyjAAQIQgw==
dependencies:
"@electron/get" "^1.0.1"
"@electron/get" "^1.13.0"
"@types/node" "^14.6.2"
extract-zip "^1.0.3"

Expand Down Expand Up @@ -4395,6 +4423,18 @@ global-agent@^2.0.2:
semver "^7.3.2"
serialize-error "^7.0.1"

global-agent@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/global-agent/-/global-agent-3.0.0.tgz#ae7cd31bd3583b93c5a16437a1afe27cc33a1ab6"
integrity sha512-PT6XReJ+D07JvGoxQMkT6qji/jVNfX/h364XHZOWeRzy64sSFr+xJ5OX7LI3b4MPQzdL4H8Y8M0xzPpsVMwA8Q==
dependencies:
boolean "^3.0.1"
es6-error "^4.1.1"
matcher "^3.0.0"
roarr "^2.15.3"
semver "^7.3.2"
serialize-error "^7.0.1"

global-dirs@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/global-dirs/-/global-dirs-3.0.0.tgz#70a76fe84ea315ab37b1f5576cbde7d48ef72686"
Expand Down