Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ulixee
GitHub Repository: ulixee/secret-agent
Path: blob/main/replay/backend/models/ViewBackend.ts
1030 views
1
import { BrowserView } from 'electron';
2
import Window from './Window';
3
import generateContextMenu from '../menus/generateContextMenu';
4
import Rectangle = Electron.Rectangle;
5
6
export default abstract class ViewBackend {
7
public get browserView(): BrowserView {
8
if (!this._browserView) {
9
this._browserView = new BrowserView({
10
webPreferences: {
11
javascript: true,
12
webSecurity: true,
13
sandbox: false,
14
contextIsolation: false,
15
...this.webPreferences,
16
},
17
});
18
this._browserView.setAutoResize({
19
width: true,
20
height: true,
21
horizontal: false,
22
vertical: false,
23
});
24
this._browserView.webContents.on('context-menu', (e, params) => {
25
generateContextMenu(params, this._browserView?.webContents).popup();
26
});
27
this._browserView.webContents.addListener('did-start-loading', () => {
28
this.window.sendToRenderer('view:updated-loading', true);
29
});
30
this._browserView.webContents.addListener('did-stop-loading', () => {
31
this.window.sendToRenderer('view:updated-loading', false);
32
});
33
}
34
return this._browserView;
35
}
36
37
public favicon = '';
38
protected isAttached = false;
39
protected readonly window: Window;
40
protected readonly webPreferences: Electron.WebPreferences;
41
protected bounds: Rectangle;
42
protected _browserView: BrowserView;
43
44
protected constructor(window: Window, webPreferences: Electron.WebPreferences) {
45
this.window = window;
46
this.webPreferences = webPreferences;
47
}
48
49
public get webContents() {
50
return this.browserView.webContents;
51
}
52
53
public get title() {
54
return this.webContents.getTitle();
55
}
56
57
public attach() {
58
if (!this.isAttached) {
59
this.window.browserWindow.addBrowserView(this.browserView);
60
this.isAttached = true;
61
}
62
}
63
64
public detach() {
65
if (this._browserView) this.window.browserWindow.removeBrowserView(this._browserView);
66
this.isAttached = false;
67
}
68
69
public destroy() {
70
this.detach();
71
this._browserView = null;
72
}
73
74
public fixBounds(newBounds: { x: number; width: number; y: any; height: number }) {
75
this.browserView.setBounds(newBounds);
76
this.bounds = newBounds;
77
}
78
}
79
80