-
Notifications
You must be signed in to change notification settings - Fork 0
/
sidepanel.js
154 lines (135 loc) · 4.67 KB
/
sidepanel.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
document.addEventListener("DOMContentLoaded", async () => {
// Add error boundary
window.onerror = function (msg, url, lineNo, columnNo, error) {
// console.error(
// "Error: ",
// msg,
// "\nURL: ",
// url,
// "\nLine: ",
// lineNo,
// "\nColumn: ",
// columnNo,
// "\nError object: ",
// error
// );
return false;
};
// console.log("Sidepanel loaded");
const etymologyContent = document.getElementById("etymologyContent");
const usageContent = document.getElementById("usageContent");
const synonymContent = document.getElementById("synonymContent");
// Function to update panel content
function updatePanelContent(data) {
const panelContainer = document.querySelector(".panel-container");
if (data.success) {
// Check if any of the responses indicate cancellation
const isCancelled = [data.etymology, data.usage, data.synonyms].some(
(response) => response?.cancelled
);
if (isCancelled) {
// Clear existing content
panelContainer.innerHTML = `
<div class="cancelled-message">
New word selected...
</div>
`;
panelContainer.classList.add("cancelled");
return;
}
// Remove cancelled class if it exists
panelContainer.classList.remove("cancelled");
// Restore original sections if they were removed
if (!document.querySelector(".section")) {
panelContainer.innerHTML = `
<div class="section">
<h2>Etymology</h2>
<div id="etymologyContent" class="content-area"></div>
</div>
<div class="section">
<h2>Usage Examples</h2>
<div id="usageContent" class="content-area"></div>
</div>
<div class="section">
<h2>Synonyms & Antonyms</h2>
<div id="synonymContent" class="content-area"></div>
</div>
`;
}
// Get fresh references to content areas
const etymologyContent = document.getElementById("etymologyContent");
const usageContent = document.getElementById("usageContent");
const synonymContent = document.getElementById("synonymContent");
etymologyContent.innerHTML = DOMPurify.sanitize(
marked.parse(
data.etymology?.message || data.etymology || "No etymology available"
)
);
usageContent.innerHTML = DOMPurify.sanitize(
marked.parse(
data.usage?.message || data.usage || "No usage examples available"
)
);
synonymContent.innerHTML = DOMPurify.sanitize(
marked.parse(
data.synonyms?.message ||
data.synonyms ||
"No synonyms/antonyms available"
)
);
} else {
etymologyContent.textContent = "Error loading etymology";
usageContent.textContent = "Error loading usage examples";
synonymContent.textContent = "Error loading synonyms/antonyms";
}
}
// Listen for messages from background script
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
// Add message validation
if (!message || typeof message !== "object") return;
// Add source validation
if (sender.id !== chrome.runtime.id) return;
const timer = setTimeout(() => {
sendResponse({ success: false, error: "Operation timed out" });
}, 30000); // 30-second timeout
console.log("Sidepanel received message:", message);
const loadingSpinner = document.getElementById("loadingSpinner");
if (message.action === "updateSidePanel") {
if (loadingSpinner) {
loadingSpinner.hidden = false;
loadingSpinner.style.display = "flex";
}
chrome.runtime.sendMessage(
{
action: "getSidePanelData",
text: message.word,
source: "sidepanel",
},
async (response) => {
console.log("...Received data response:", response);
// Start view transition for spinner fade-out
if (loadingSpinner) {
document.startViewTransition(() => {
loadingSpinner.style.display = "none";
loadingSpinner.hidden = true;
});
}
updatePanelContent(response || { success: false });
}
);
}
// Clear timeout on successful response
return () => clearTimeout(timer);
});
// Request data for current tab on load
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
if (tabs[0]) {
chrome.runtime.sendMessage({
action: "getStoredWord",
tabId: tabs[0].id,
});
}
});
// Add connection to background
chrome.runtime.connect({ name: "sidepanel" });
});