migrate executeJavaScript calls to new command system

This commit is contained in:
Vendicated
2025-05-16 21:00:32 +02:00
parent bb3cec0d13
commit 3295a7d344
5 changed files with 51 additions and 23 deletions

View File

@@ -7,6 +7,7 @@
import { app } from "electron"; import { app } from "electron";
import { existsSync, mkdirSync, renameSync, rmSync, writeFileSync } from "fs"; import { existsSync, mkdirSync, renameSync, rmSync, writeFileSync } from "fs";
import { join } from "path"; import { join } from "path";
import { stripIndent } from "shared/utils/text";
interface AutoStart { interface AutoStart {
isEnabled(): boolean; isEnabled(): boolean;
@@ -31,15 +32,15 @@ function makeAutoStartLinux(): AutoStart {
return { return {
isEnabled: () => existsSync(file), isEnabled: () => existsSync(file),
enable() { enable() {
const desktopFile = ` const desktopFile = stripIndent`
[Desktop Entry] [Desktop Entry]
Type=Application Type=Application
Name=Vesktop Name=Vesktop
Comment=Vesktop autostart script Comment=Vesktop autostart script
Exec=${commandLine} Exec=${commandLine}
StartupNotify=false StartupNotify=false
Terminal=false Terminal=false
`.trim(); `;
mkdirSync(dir, { recursive: true }); mkdirSync(dir, { recursive: true });
writeFileSync(file, desktopFile); writeFileSync(file, desktopFile);

View File

@@ -6,8 +6,9 @@
import { desktopCapturer, session, Streams } from "electron"; import { desktopCapturer, session, Streams } from "electron";
import type { StreamPick } from "renderer/components/ScreenSharePicker"; import type { StreamPick } from "renderer/components/ScreenSharePicker";
import { IpcEvents } from "shared/IpcEvents"; import { IpcCommands, IpcEvents } from "shared/IpcEvents";
import { sendRendererCommand } from "./ipcCommands";
import { handle } from "./utils/ipcWrappers"; import { handle } from "./utils/ipcWrappers";
const isWayland = const isWayland =
@@ -49,11 +50,11 @@ export function registerScreenShareHandler() {
if (isWayland) { if (isWayland) {
const video = data[0]; const video = data[0];
if (video) { if (video) {
const stream = await request const stream = await sendRendererCommand<StreamPick>(IpcCommands.SCREEN_SHARE_PICKER, {
.frame!.executeJavaScript( screens: [video],
`Vesktop.Components.ScreenShare.openScreenSharePicker(${JSON.stringify([video])},true)` skipPicker: true
) }).catch(() => null);
.catch(() => null);
if (stream === null) return callback({}); if (stream === null) return callback({});
} }
@@ -61,10 +62,10 @@ export function registerScreenShareHandler() {
return; return;
} }
const choice = await request const choice = await sendRendererCommand<StreamPick>(IpcCommands.SCREEN_SHARE_PICKER, {
.frame!.executeJavaScript(`Vesktop.Components.ScreenShare.openScreenSharePicker(${JSON.stringify(data)})`) screens: data,
.then(e => e as StreamPick) skipPicker: false
.catch(e => { }).catch(e => {
console.error("Error during screenshare picker", e); console.error("Error during screenshare picker", e);
return null; return null;
}); });

View File

@@ -7,6 +7,8 @@
import { SettingsRouter } from "@vencord/types/webpack/common"; import { SettingsRouter } from "@vencord/types/webpack/common";
import { IpcCommands } from "shared/IpcEvents"; import { IpcCommands } from "shared/IpcEvents";
import { openScreenSharePicker } from "./components/ScreenSharePicker";
type IpcCommandHandler = (data: any) => any; type IpcCommandHandler = (data: any) => any;
const handlers = new Map<string, IpcCommandHandler>(); const handlers = new Map<string, IpcCommandHandler>();
@@ -46,4 +48,9 @@ export function offIpcCommand(channel: string) {
onIpcCommand(IpcCommands.NAVIGATE_SETTINGS, () => { onIpcCommand(IpcCommands.NAVIGATE_SETTINGS, () => {
SettingsRouter.open("My Account"); SettingsRouter.open("My Account");
}); });
onIpcCommand(IpcCommands.GET_LANGUAGES, () => navigator.languages); onIpcCommand(IpcCommands.GET_LANGUAGES, () => navigator.languages);
onIpcCommand(IpcCommands.SCREEN_SHARE_PICKER, data => {
openScreenSharePicker(data.screens, data.skipPicker);
});

View File

@@ -62,6 +62,10 @@ export const enum IpcCommands {
RPC_ACTIVITY = "rpc:activity", RPC_ACTIVITY = "rpc:activity",
RPC_INVITE = "rpc:invite", RPC_INVITE = "rpc:invite",
RPC_DEEP_LINK = "rpc:link", RPC_DEEP_LINK = "rpc:link",
NAVIGATE_SETTINGS = "navigate:settings", NAVIGATE_SETTINGS = "navigate:settings",
GET_LANGUAGES = "navigator.languages"
GET_LANGUAGES = "navigator.languages",
SCREEN_SHARE_PICKER = "screenshare:picker"
} }

15
src/shared/utils/text.ts Normal file
View File

@@ -0,0 +1,15 @@
/*
* Vesktop, a desktop app aiming to give you a snappier Discord Experience
* Copyright (c) 2025 Vendicated and Vesktop contributors
* SPDX-License-Identifier: GPL-3.0-or-later
*/
export function stripIndent(strings: TemplateStringsArray, ...values: any[]) {
const string = String.raw({ raw: strings }, ...values);
const match = string.match(/^[ \t]*(?=\S)/gm);
if (!match) return string.trim();
const minIndent = match.reduce((r, a) => Math.min(r, a.length), Infinity);
return string.replace(new RegExp(`^[ \\t]{${minIndent}}`, "gm"), "").trim();
}