Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ulixee
GitHub Repository: ulixee/secret-agent
Path: blob/main/plugins/default-browser-emulator/lib/Viewports.ts
1029 views
1
import IViewport from '@secret-agent/interfaces/IViewport';
2
import { IDataWindowFraming } from '../interfaces/IBrowserData';
3
4
const defaultWindowFraming = {
5
screenGapLeft: 0,
6
screenGapTop: 0,
7
screenGapRight: 0,
8
screenGapBottom: 0,
9
frameBorderWidth: 0,
10
frameBorderHeight: 0,
11
};
12
13
export const defaultScreen = {
14
width: 1440,
15
height: 900,
16
};
17
18
export default class Viewports {
19
static getDefault(windowBaseFraming: IDataWindowFraming, windowFraming: IDataWindowFraming) {
20
windowFraming = windowFraming || { ...defaultWindowFraming };
21
const base = windowBaseFraming || { ...defaultWindowFraming };
22
const screenWidth = defaultScreen.width;
23
const screenHeight = defaultScreen.height;
24
25
const windowInnerWidth =
26
screenWidth - (base.screenGapLeft + base.screenGapRight + base.frameBorderWidth);
27
const windowWidth = windowInnerWidth + windowFraming.frameBorderWidth;
28
29
const windowInnerHeight =
30
screenHeight - (base.screenGapTop + base.screenGapBottom + base.frameBorderHeight);
31
const windowHeight = windowInnerHeight + windowFraming.frameBorderHeight;
32
33
const availableScreenWidth =
34
screenWidth - (windowFraming.screenGapLeft + windowFraming.screenGapRight);
35
const leftoverSpacingWidth = availableScreenWidth - windowWidth;
36
const positionX = randomIntFromInterval(
37
windowFraming.screenGapLeft,
38
windowFraming.screenGapLeft + leftoverSpacingWidth,
39
);
40
41
const availableScreenHeight =
42
screenHeight - (windowFraming.screenGapTop + windowFraming.screenGapBottom);
43
const leftoverSpacingHeight = availableScreenHeight - windowHeight;
44
const positionY = randomIntFromInterval(
45
windowFraming.screenGapTop,
46
windowFraming.screenGapTop + leftoverSpacingHeight,
47
);
48
49
return {
50
positionX,
51
positionY,
52
screenWidth,
53
screenHeight,
54
width: windowWidth,
55
height: windowHeight,
56
deviceScaleFactor: 1,
57
} as IViewport;
58
}
59
}
60
61
// HELPERS
62
63
function randomIntFromInterval(min, max) {
64
if (min === max) return min;
65
return Math.floor(Math.random() * (max - min + 1) + min);
66
}
67
68