31 lines
1.2 KiB
TypeScript
31 lines
1.2 KiB
TypeScript
import type { LoadURLOptions } from 'electron';
|
|
import { SiteAppWindow } from '../app-window/site-app-window';
|
|
import { mergeContentSecurityPolicy } from '../session/session-util';
|
|
import { cloudflareSiteCsp, humanInteractionRequired, isCloudFlareSite } from './cloudflare-util';
|
|
|
|
export abstract class CloudflareSiteAppWindow extends SiteAppWindow {
|
|
protected loadUrl(url: string, options?: LoadURLOptions): Promise<number> {
|
|
return new Promise((resolve) => {
|
|
const onDidNavigate: (event: Event, url: string, httpResponseCode: number) => void = async (
|
|
event,
|
|
navigationUrl,
|
|
httpResponseCode
|
|
) => {
|
|
if (!(await isCloudFlareSite(this.getWindow().webContents))) {
|
|
this.getWindow().webContents.removeListener('did-navigate', onDidNavigate);
|
|
resolve(httpResponseCode);
|
|
} else if (await humanInteractionRequired(this.getWindow().webContents)) {
|
|
this.askForUserInteraction();
|
|
}
|
|
};
|
|
|
|
this.getWindow().webContents.on('did-navigate', onDidNavigate);
|
|
void this.getWindow().loadURL(url, options);
|
|
});
|
|
}
|
|
|
|
protected getCsp(): Session.ContentSecurityPolicy {
|
|
return mergeContentSecurityPolicy(super.getCsp(), cloudflareSiteCsp);
|
|
}
|
|
}
|