this repo has no description
1import type { BrowserWindow } from "electron";
2
3type PermissionRequestHandler = (
4 webContents: Electron.WebContents,
5 permission: string,
6 callback: (permissionGranted: boolean) => void,
7 details: Electron.PermissionRequestHandlerHandlerDetails
8) => void;
9
10type PermissionCheckHandler = (
11 webContents: Electron.WebContents | null,
12 permission: string,
13 requestingOrigin: string,
14 details: Electron.PermissionCheckHandlerHandlerDetails
15) => boolean;
16
17moonlightHost.events.on(
18 "window-created",
19 (window: BrowserWindow, isMainWindow: boolean) => {
20 if (!isMainWindow) return;
21 const windowSession = window.webContents.session;
22
23 // setPermissionRequestHandler
24 windowSession.setPermissionRequestHandler(
25 (webcontents, permission, callback, details) => {
26 let cbResult = false;
27 function fakeCallback(result: boolean) {
28 cbResult = result;
29 }
30
31 if (caughtPermissionRequestHandler) {
32 caughtPermissionRequestHandler(
33 webcontents,
34 permission,
35 fakeCallback,
36 details
37 );
38 }
39
40 if (permission === "media" || permission === "display-capture") {
41 cbResult = true;
42 }
43
44 callback(cbResult);
45 }
46 );
47
48 let caughtPermissionRequestHandler: PermissionRequestHandler | undefined;
49
50 windowSession.setPermissionRequestHandler =
51 function catchSetPermissionRequestHandler(
52 handler: (
53 webcontents: Electron.WebContents,
54 permission: string,
55 callback: (permissionGranted: boolean) => void
56 ) => void
57 ) {
58 caughtPermissionRequestHandler = handler;
59 };
60
61 // setPermissionCheckHandler
62 windowSession.setPermissionCheckHandler(
63 (webcontents, permission, requestingOrigin, details) => {
64 return false;
65 }
66 );
67
68 let caughtPermissionCheckHandler: PermissionCheckHandler | undefined;
69
70 windowSession.setPermissionCheckHandler(
71 (webcontents, permission, requestingOrigin, details) => {
72 let result = false;
73
74 if (caughtPermissionCheckHandler) {
75 result = caughtPermissionCheckHandler(
76 webcontents,
77 permission,
78 requestingOrigin,
79 details
80 );
81 }
82
83 if (permission === "media" || permission === "display-capture") {
84 result = true;
85 }
86
87 return result;
88 }
89 );
90
91 windowSession.setPermissionCheckHandler =
92 function catchSetPermissionCheckHandler(handler: PermissionCheckHandler) {
93 caughtPermissionCheckHandler = handler;
94 };
95 }
96);