-
Notifications
You must be signed in to change notification settings - Fork 1
/
content.js
165 lines (147 loc) · 5.17 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Listen for messages from the background script
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.simplifiedText) {
// try to remove popup if it already exists
closeExistingPopup();
showSimplifiedText(message.simplifiedText);
} else if (message.token || message.token === ' ') {
appendTokenToContent(message.token);
}
if (message.waiting) {
// try to remove popup if it already exists
closeExistingPopup();
showWaitingMessage();
}
});
// close existing popup if it exists
function closeExistingPopup() {
const popup = document.getElementById('simplified-text-popup');
if (popup) {
document.body.removeChild(popup);
}
}
// Show a waiting message in the popup
async function showWaitingMessage() {
const lang = await getStoredLanguage();
if (lang == 'en') {
showSimplifiedText('Waiting for OpenAI to respond...');
} else if (lang == 'zh') {
showSimplifiedText('等待OpenAI服务器回应...');
}
};
function showSimplifiedText(simplifiedText) {
// Retrieve the last position from storage
chrome.storage.local.get(['popupBottom', 'popupRight'], (result) => {
const popupBottom = result.popupBottom || '1cm';
const popupRight = result.popupRight || '1cm';
// Create the popup element
const popup = document.createElement('div');
popup.id = 'simplified-text-popup';
popup.style.position = 'fixed';
popup.style.bottom = popupBottom;
popup.style.width = '10cm'
popup.style.height = '15cm'
// add scroll bar to the popup
popup.style.overflow = 'auto';
popup.style.right = popupRight;
popup.style.backgroundColor = 'white';
popup.style.border = '1px solid #ccc';
popup.style.padding = '5mm';
// add blank space for the close button
popup.style.paddingTop = '10mm';
popup.style.zIndex = '999999';
// set rounded corners
popup.style.borderRadius = '5mm';
// add shadow
popup.style.boxShadow = '0 0 5mm #000';
// Create the close button
const closeButton = document.createElement('button');
closeButton.innerText = '⊗';
// set the inner text font size to 5mm
closeButton.style.fontSize = '5mm';
// set the inner text font
closeButton.style.fontFamily = 'Arial';
// set text color to black
closeButton.style.color = 'black';
closeButton.style.position = 'absolute';
closeButton.style.top = '0';
// set button with no border nor frame
closeButton.style.border = 'none';
closeButton.style.outline = 'none';
// setting button height to 5mm
closeButton.style.height = '5mm';
// setting button background color to transparent
closeButton.style.backgroundColor = 'transparent';
closeButton.style.right = '0';
closeButton.onclick = () => {
document.body.removeChild(popup);
// send a message to background.js to stop the stream
chrome.runtime.sendMessage({ stopStream: true });
};
// Create the content container
const content = document.createElement('div');
content.id = "simplified-text-content";
content.innerText = "";
// set the inner text font and size
content.style.fontFamily = 'Arial';
content.style.fontSize = '4mm';
content.style.color = 'black';
// Add the content and close button to the popup
popup.appendChild(closeButton);
popup.appendChild(content);
// Add the popup to the page
document.body.appendChild(popup);
// Make the popup draggable
makePopupDraggable(popup);
});
}
function getStoredLanguage() {
return new Promise((resolve, reject) => {
chrome.storage.sync.get(['language'], (result) => {
resolve(result.language);
});
});
}
function makePopupDraggable(popup) {
let pos1 = 0,
pos2 = 0,
pos3 = 0,
pos4 = 0;
popup.onmousedown = dragMouseDown;
function dragMouseDown(e) {
// Check if the target is the popup
if (e.target === popup) {
e = e || window.event;
e.preventDefault();
pos3 = e.clientX;
pos4 = e.clientY;
document.onmouseup = closeDragElement;
document.onmousemove = elementDrag;
}
}
function elementDrag(e) {
e = e || window.event;
e.preventDefault();
pos1 = pos3 - e.clientX;
pos2 = pos4 - e.clientY;
pos3 = e.clientX;
pos4 = e.clientY;
popup.style.bottom = `${document.documentElement.clientHeight - (popup.offsetTop + popup.offsetHeight) + pos2}px`;
popup.style.right = `${document.documentElement.clientWidth - (popup.offsetLeft + popup.offsetWidth) + pos1}px`;
}
function closeDragElement() {
// Save the position in storage
chrome.storage.local.set({
popupRight: popup.style.right,
popupBottom: popup.style.bottom,
});
document.onmouseup = null;
document.onmousemove = null;
}
}
function appendTokenToContent(token) {
const content = document.getElementById("simplified-text-content");
if (content) {
content.innerHTML += token.replace(/\n/g, '<br>');
}
}