-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsidechat.js
More file actions
50 lines (43 loc) · 1.55 KB
/
sidechat.js
File metadata and controls
50 lines (43 loc) · 1.55 KB
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
const chatFrame = document.getElementById('chatFrame');
const emptyState = document.getElementById('empty');
const openTab = document.getElementById('openTab');
const reloadFrame = document.getElementById('reloadFrame');
const status = document.getElementById('status');
loadSidechat();
reloadFrame.addEventListener('click', () => {
try {
chatFrame.contentWindow.location.reload();
} catch (error) {
status.innerText = 'Sidechat could not reload the embedded page.';
status.classList.remove('hidden');
}
});
chrome.storage.onChanged.addListener((changes, areaName) => {
if (areaName === 'local' && (changes.sidechatURL || changes.sidechatError || changes.sidechatUpdatedAt)) {
loadSidechat();
}
});
function loadSidechat() {
chrome.storage.local.get(['sidechatURL', 'sidechatError'], (data) => {
const sidechatURL = data.sidechatURL || '';
const sidechatError = data.sidechatError || '';
if (!sidechatURL) {
chatFrame.classList.add('hidden');
emptyState.classList.remove('hidden');
emptyState.innerText = 'Use a Sidechat preset from the right-click menu.';
status.classList.add('hidden');
openTab.href = 'https://chatgpt.com/';
return;
}
openTab.href = sidechatURL;
if (sidechatError) {
status.innerText = `Sidechat could not open automatically: ${sidechatError}`;
status.classList.remove('hidden');
} else {
status.classList.add('hidden');
}
chatFrame.src = sidechatURL;
emptyState.classList.add('hidden');
chatFrame.classList.remove('hidden');
});
}