-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
165 lines (145 loc) · 5.58 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
let popup = null;
let popupTimeout = null;
let lastMouseUpTime = 0;
document.addEventListener('mousedown', function (e) {
if (popupTimeout) {
clearTimeout(popupTimeout);
popupTimeout = null;
}
// Close all popups if clicking outside
if (!e.target.closest('.dictionary-popup')) {
closeAllPopups();
}
});
document.addEventListener('mouseup', function (e) {
lastMouseUpTime = Date.now();
});
document.addEventListener('dblclick', function (e) {
if (popupTimeout) {
clearTimeout(popupTimeout);
}
popupTimeout = setTimeout(function () {
const selection = window.getSelection().toString().trim();
if (selection.length > 0 && selection.split(/\s+/).length === 1) {
if (Date.now() - lastMouseUpTime >= 250) {
closeAllPopups(); // Close any existing popups
showPopup(selection, e.pageX, e.pageY);
}
}
}, 250);
});
document.addEventListener('selectionchange', function () {
if (popupTimeout) {
clearTimeout(popupTimeout);
popupTimeout = null;
}
});
function closeAllPopups() {
document.querySelectorAll('.dictionary-popup').forEach(popup => {
document.body.removeChild(popup);
});
popup = null;
}
async function showPopup(word, x, y) {
const { definitions, phonetic } = await getDefinitionsAndPhonetic(word);
popup = document.createElement('div');
popup.className = 'dictionary-popup'; // Add a class for easy selection
popup.style.position = 'absolute';
popup.style.left = `${x}px`;
popup.style.top = `${y}px`;
popup.style.backgroundColor = 'var(--popup-bg-color, white)';
popup.style.color = 'var(--popup-text-color, black)';
popup.style.border = '1px solid var(--popup-border-color, black)';
popup.style.borderRadius = '5px';
popup.style.padding = '10px';
popup.style.zIndex = '1000';
popup.style.maxWidth = '350px';
popup.style.maxHeight = '400px';
popup.style.overflowY = 'auto';
popup.style.boxShadow = '0 2px 5px rgba(0,0,0,0.2)';
// Add CSS variables for color scheme
const style = document.createElement('style');
style.textContent = `
:root {
--popup-bg-color: white;
--popup-text-color: black;
--popup-border-color: black;
--popup-link-color: blue;
}
@media (prefers-color-scheme: dark) {
:root {
--popup-bg-color: #333;
--popup-text-color: #fff;
--popup-border-color: #666;
--popup-link-color: #4da6ff;
}
}
`;
document.head.appendChild(style);
let content = `<h3 style="margin-top: 0; margin-bottom: 5px;">${word}</h3>`;
if (phonetic) {
content += `<p style="margin-top: 0; margin-bottom: 10px; font-size: 0.8em; color: var(--popup-text-color, #666);">${phonetic}</p>`;
}
for (const [partOfSpeech, definitionList] of Object.entries(definitions)) {
if (definitionList && definitionList.length > 0) {
content += `<p><strong>${partOfSpeech}:</strong></p>`;
content += `<ol style="margin-top: 5px; padding-left: 20px;">`;
definitionList.slice(0, 2).forEach(def => {
content += `<li>${def}</li>`;
});
content += `</ol>`;
if (definitionList.length > 2) {
content += `<p class="more-definitions" data-part="${partOfSpeech}" style="cursor: pointer; color: var(--popup-link-color, blue); margin-top: 0;">Show more</p>`;
}
}
}
popup.innerHTML = content || "No definitions found.";
document.body.appendChild(popup);
// Add event listeners for "Show more" links
popup.querySelectorAll('.more-definitions').forEach(element => {
element.addEventListener('click', function (e) {
e.stopPropagation(); // Prevent the click from bubbling up
const partOfSpeech = this.getAttribute('data-part');
const allDefinitions = definitions[partOfSpeech];
let additionalContent = `<ol start="3" style="margin-top: 5px; padding-left: 20px;">`;
allDefinitions.slice(2).forEach(def => {
additionalContent += `<li>${def}</li>`;
});
additionalContent += `</ol>`;
this.insertAdjacentHTML('beforebegin', additionalContent);
this.style.display = 'none';
});
});
// Prevent clicks inside the popup from closing it
popup.addEventListener('click', function (e) {
e.stopPropagation();
});
}
async function getDefinitionsAndPhonetic(word) {
try {
const response = await fetch(`https://api.dictionaryapi.dev/api/v2/entries/en/${word}`);
const data = await response.json();
const definitions = {
noun: [],
verb: [],
adjective: [],
adverb: []
};
let phonetic = null;
if (data[0].phonetic) {
phonetic = data[0].phonetic;
} else if (data[0].phonetics && data[0].phonetics.length > 0) {
phonetic = data[0].phonetics[0].text;
}
data[0].meanings.forEach(meaning => {
const partOfSpeech = meaning.partOfSpeech;
if (partOfSpeech in definitions) {
definitions[partOfSpeech] = meaning.definitions.map(def => def.definition);
}
});
return { definitions, phonetic };
} catch (error) {
console.error("Error fetching definition:", error);
return { definitions: {}, phonetic: null };
}
}