-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
100 lines (86 loc) · 2.73 KB
/
content.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
const CAMERA_ELEMENT_ID = 'castaCamera';
/**
* Creates a draggable iframe inside a wrapper div with a transparent overlay for drag handling.
*
* @param {string} id - The ID of the wrapper element.
* @param {string} src - The source URL of the iframe content.
* @param {string} styles - The CSS styles for the wrapper element.
* @return {HTMLElement} - The draggable wrapper element containing the iframe.
*/
const createDraggableIframe = (id, src, styles) => {
const wrapper = document.createElement('div');
wrapper.id = id;
wrapper.style.cssText = styles;
const iframe = document.createElement('iframe');
iframe.setAttribute('allow', 'camera; microphone');
iframe.src = src;
iframe.style.cssText = `
width: 100%;
height: 100%;
border-radius: 50%;
border: none;
pointer-events: auto;
`;
const dragOverlay = document.createElement('div');
dragOverlay.style.cssText = `
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
cursor: grab;
z-index: 2;
background: transparent;
`;
let isDragging = false;
let dragOffsetX, dragOffsetY;
dragOverlay.addEventListener('mousedown', (event) => {
isDragging = true;
dragOffsetX = event.clientX - wrapper.offsetLeft;
dragOffsetY = event.clientY - wrapper.offsetTop;
dragOverlay.style.cursor = 'grabbing';
});
document.addEventListener('mousemove', (event) => {
if (isDragging) {
wrapper.style.left = `${event.clientX - dragOffsetX}px`;
wrapper.style.top = `${event.clientY - dragOffsetY}px`;
}
});
document.addEventListener('mouseup', () => {
if (isDragging) {
isDragging = false;
dragOverlay.style.cursor = 'grab';
}
});
wrapper.appendChild(iframe);
wrapper.appendChild(dragOverlay);
return wrapper;
};
/**
* Initializes the draggable camera element. If it already exists, it becomes visible again.
*/
const initCameraElement = () => {
const existingCameraElement = document.getElementById(CAMERA_ELEMENT_ID);
if (existingCameraElement) {
existingCameraElement.style.display = 'block';
return;
}
const cameraIframeStyles = `
position: fixed;
top: 50px;
left: 50px;
width: 200px;
height: 200px;
background-color: rgba(0, 0, 0, 0.5);
z-index: 9999;
border-radius: 50%;
cursor: grab;
`;
const cameraIframe = createDraggableIframe(
CAMERA_ELEMENT_ID,
chrome.runtime.getURL('camera.html'),
cameraIframeStyles
);
document.body.appendChild(cameraIframe);
};
initCameraElement();