this repo has no description
at develop 2.3 kB view raw
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("window-created", (window: BrowserWindow, isMainWindow: boolean) => { 18 if (!isMainWindow) return; 19 const windowSession = window.webContents.session; 20 21 // setPermissionRequestHandler 22 windowSession.setPermissionRequestHandler((webcontents, permission, callback, details) => { 23 let cbResult = false; 24 function fakeCallback(result: boolean) { 25 cbResult = result; 26 } 27 28 if (caughtPermissionRequestHandler) { 29 caughtPermissionRequestHandler(webcontents, permission, fakeCallback, details); 30 } 31 32 if (permission === "media" || permission === "display-capture") { 33 cbResult = true; 34 } 35 36 callback(cbResult); 37 }); 38 39 let caughtPermissionRequestHandler: PermissionRequestHandler | undefined; 40 41 windowSession.setPermissionRequestHandler = function catchSetPermissionRequestHandler( 42 handler: ( 43 webcontents: Electron.WebContents, 44 permission: string, 45 callback: (permissionGranted: boolean) => void 46 ) => void 47 ) { 48 caughtPermissionRequestHandler = handler; 49 }; 50 51 // setPermissionCheckHandler 52 windowSession.setPermissionCheckHandler((webcontents, permission, requestingOrigin, details) => { 53 return false; 54 }); 55 56 let caughtPermissionCheckHandler: PermissionCheckHandler | undefined; 57 58 windowSession.setPermissionCheckHandler((webcontents, permission, requestingOrigin, details) => { 59 let result = false; 60 61 if (caughtPermissionCheckHandler) { 62 result = caughtPermissionCheckHandler(webcontents, permission, requestingOrigin, details); 63 } 64 65 if (permission === "media" || permission === "display-capture") { 66 result = true; 67 } 68 69 return result; 70 }); 71 72 windowSession.setPermissionCheckHandler = function catchSetPermissionCheckHandler(handler: PermissionCheckHandler) { 73 caughtPermissionCheckHandler = handler; 74 }; 75});