fix(reader): fix reader word popup translation key and sanitize token lookup
This commit is contained in:
+11
-6
@@ -1897,7 +1897,8 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
// Tokenize words while preserving spaces & punctuation
|
||||
const tokens = paraText.split(/([A-Za-z0-9'-]+)/);
|
||||
tokens.forEach(token => {
|
||||
if (/^[A-Za-z0-9'-]+$/.test(token) && token.length > 1) {
|
||||
const isWord = /^[A-Za-z0-9'-]+$/.test(token) && token.replace(/[^A-Za-z0-9]/g, '').length >= 1;
|
||||
if (isWord) {
|
||||
const span = document.createElement('span');
|
||||
span.className = 'reader-word';
|
||||
span.dataset.word = token;
|
||||
@@ -1951,13 +1952,16 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
}
|
||||
|
||||
async function openWordPopup(term, contextSentence, targetElement) {
|
||||
const cleanTerm = (term || '').replace(/^['"“«`\s\-_.,;:!?()[\]{}]+|['"”»`\s\-_.,;:!?()[\]{}]+$/g, '').trim();
|
||||
if (!cleanTerm) return;
|
||||
|
||||
popupCurrentData = {
|
||||
term: term,
|
||||
term: cleanTerm,
|
||||
contextSentence: contextSentence,
|
||||
targetElement: targetElement,
|
||||
};
|
||||
|
||||
popupTerm.textContent = term;
|
||||
popupTerm.textContent = cleanTerm;
|
||||
popupPhonetic.textContent = '';
|
||||
popupTranslation.textContent = '';
|
||||
popupContext.textContent = contextSentence ? `“${contextSentence}”` : '';
|
||||
@@ -1985,13 +1989,14 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
}
|
||||
|
||||
try {
|
||||
const resp = await fetch(`/api/lookup?q=${encodeURIComponent(term)}`);
|
||||
const resp = await fetch(`/api/lookup?q=${encodeURIComponent(cleanTerm)}`);
|
||||
if (resp.ok) {
|
||||
const data = await resp.json();
|
||||
popupCurrentData.lookup = data;
|
||||
|
||||
const trans = data.translation || data.translation_ru || (data.translations_list && data.translations_list.length > 0 ? data.translations_list.join(', ') : '');
|
||||
popupPhonetic.textContent = data.phonetic || '';
|
||||
popupTranslation.textContent = data.translation_ru || 'Перевод не найден';
|
||||
popupTranslation.textContent = trans || 'Перевод не найден';
|
||||
popupLoading.classList.add('hidden');
|
||||
popupContent.classList.remove('hidden');
|
||||
|
||||
@@ -2035,7 +2040,7 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
|
||||
const payload = {
|
||||
term: d.term,
|
||||
translation: lookup.translation_ru || popupTranslation.textContent,
|
||||
translation: lookup.translation || lookup.translation_ru || popupTranslation.textContent,
|
||||
context_sentence: d.contextSentence || '',
|
||||
phonetic: lookup.phonetic || '',
|
||||
audio_url: lookup.audio_url || '',
|
||||
|
||||
Reference in New Issue
Block a user