From 3a9b1fe1644ee04b27f00cb425bc0f67450cc349 Mon Sep 17 00:00:00 2001 From: SpacingBat3 Date: Sun, 30 Jan 2022 19:44:18 +0100 Subject: [PATCH] Support older Electron versions. Fix WebCord to work under Electron versions not supporting WebHID API. --- sources/code/global/global.ts | 9 +++++++++ sources/code/global/main.ts | 7 +++++-- sources/code/main/windows/main.ts | 1 - 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/sources/code/global/global.ts b/sources/code/global/global.ts index 09944be1..b2599fdd 100644 --- a/sources/code/global/global.ts +++ b/sources/code/global/global.ts @@ -216,4 +216,13 @@ export function getAppIcon(sizes:number[]) { if(existsSync("/usr/share/icons/hicolor/"+size.toString()+"x"+size.toString()+"/apps/webcord.png")) return "/usr/share/icons/hicolor/"+size.toString()+"x"+size.toString()+"/apps/webcord.png"; return ""; +} + +export type SessionLatest = Electron.Session & { + /** + * A method that is unsupported within your Electron version, but valid + * for Electron releases supporting WebHID API, which are versions from + * range `>=14.1.0 && <15.0.0 || >=15.1.0`. + */ + setDevicePermissionHandler: (handler: (()=>boolean)|null)=>void; } \ No newline at end of file diff --git a/sources/code/global/main.ts b/sources/code/global/main.ts index d5bd0a99..31875f4e 100644 --- a/sources/code/global/main.ts +++ b/sources/code/global/main.ts @@ -63,13 +63,14 @@ console.debug = function (message?:unknown, ...optionalParams:unknown[]) { } import { app, BrowserWindow, dialog, shell } from 'electron'; import { promises as fs } from 'fs'; -import { knownIstancesList, trustedProtocolRegExp } from './global'; +import { knownIstancesList, trustedProtocolRegExp, SessionLatest } from './global'; import { checkVersion } from '../main/modules/update'; import l10n from './modules/l10n'; import createMainWindow from "../main/windows/main"; import { AppConfig } from '../main/modules/config'; import colors from '@spacingbat3/kolor'; import { resolve as resolvePath, relative } from 'path'; +import { major } from "semver"; // Handle command line switches: @@ -180,7 +181,9 @@ app.on('web-contents-created', (_event, webContents) => { // Block all permission requests/checks by the default. webContents.session.setPermissionCheckHandler(() => false); webContents.session.setPermissionRequestHandler((_webContents,_permission,callback) => callback(false)); - webContents.session.setDevicePermissionHandler(() => false); + // Block HID request only when Electron supports handling them. + if(major(process.versions.electron) >= 16 || /^(?:14|15)\.1\.\d+.*$/.test(process.versions.electron)) + (webContents.session as SessionLatest).setDevicePermissionHandler(() => false); // Block navigation to the different origin. webContents.on('will-navigate', (event, url) => { const originUrl = webContents.getURL(); diff --git a/sources/code/main/windows/main.ts b/sources/code/main/windows/main.ts index 6d4bfcd2..645f0432 100644 --- a/sources/code/main/windows/main.ts +++ b/sources/code/main/windows/main.ts @@ -168,7 +168,6 @@ export default function createMainWindow(startHidden: boolean, l10nStrings: l10n } return callback(returnValue); }); - win.webContents.session.setDevicePermissionHandler(() => false); } win.loadFile(resolve(app.getAppPath(), 'sources/assets/web/html/load.html')) .catch(()=>{return;});