Files
planet/client/src/main/protocol.ts
T
linkong 842cba4dc7
ci / backend (push) Has been cancelled
ci / frontend (push) Has been cancelled
release / images (push) Has been cancelled
release / client_release (push) Has been cancelled
ci / delivery (push) Has been cancelled
release: bump version to 0.77.0
2026-11-20 16:59:38 +08:00

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)
})
}