62 lines
1.7 KiB
TypeScript
62 lines
1.7 KiB
TypeScript
import { net, protocol, type Session } from 'electron'
|
|
|
|
import { createEarthProtocolHandler } from './protocol-core'
|
|
|
|
export const EARTH_ORIGIN = 'planet://earth'
|
|
export const EARTH_ENTRY_URL = `${EARTH_ORIGIN}/earth/index.html`
|
|
|
|
export function registerEarthScheme(): void {
|
|
protocol.registerSchemesAsPrivileged([
|
|
{
|
|
scheme: 'planet',
|
|
privileges: {
|
|
standard: true,
|
|
secure: true,
|
|
supportFetchAPI: true,
|
|
corsEnabled: true,
|
|
stream: true,
|
|
allowServiceWorkers: true,
|
|
},
|
|
},
|
|
])
|
|
}
|
|
|
|
export function installEarthSessionHandlers(
|
|
targetSession: Session,
|
|
options: {
|
|
earthBundleRoot: string
|
|
getBackendUrl: () => string
|
|
},
|
|
): void {
|
|
const handler = createEarthProtocolHandler({
|
|
earthBundleRoot: options.earthBundleRoot,
|
|
getBackendUrl: options.getBackendUrl,
|
|
fetchImpl: (input, init) => net.fetch(input as string, init),
|
|
})
|
|
|
|
targetSession.protocol.handle('planet', handler)
|
|
|
|
targetSession.webRequest.onBeforeSendHeaders(
|
|
{
|
|
urls: ['*://*.youtube.com/*', '*://*.youtube-nocookie.com/*'],
|
|
},
|
|
(details, callback) => {
|
|
const requestHeaders = { ...details.requestHeaders }
|
|
if (!requestHeaders.Referer && !requestHeaders.referer) {
|
|
requestHeaders.Referer = 'https://earth.planet.local/'
|
|
}
|
|
callback({ requestHeaders })
|
|
},
|
|
)
|
|
|
|
targetSession.setPermissionRequestHandler((webContents, permission, callback) => {
|
|
const pageUrl = webContents.getURL()
|
|
const isEarthOrigin = pageUrl.startsWith(EARTH_ORIGIN)
|
|
if (isEarthOrigin && (permission === 'media' || permission === 'fullscreen')) {
|
|
callback(true)
|
|
return
|
|
}
|
|
callback(false)
|
|
})
|
|
}
|