Skip to content

Commit

Permalink
Support older Electron versions.
Browse files Browse the repository at this point in the history
Fix WebCord to work under Electron versions not supporting WebHID API.
  • Loading branch information
SpacingBat3 committed Jan 30, 2022
1 parent 7a22912 commit 3a9b1fe
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
9 changes: 9 additions & 0 deletions sources/code/global/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
7 changes: 5 additions & 2 deletions sources/code/global/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -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();
Expand Down
1 change: 0 additions & 1 deletion sources/code/main/windows/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;});
Expand Down

0 comments on commit 3a9b1fe

Please sign in to comment.