From 5323ce0b93dbaca1072d7766967bb55848d0d534 Mon Sep 17 00:00:00 2001 From: Zephyrxvxx7 Date: Wed, 5 Oct 2022 01:56:13 +0800 Subject: [PATCH] fix: don't rend the HTML tags --- PolyglotSafariExtension/Sources/content.ts | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/PolyglotSafariExtension/Sources/content.ts b/PolyglotSafariExtension/Sources/content.ts index 96f556f..f7565b4 100644 --- a/PolyglotSafariExtension/Sources/content.ts +++ b/PolyglotSafariExtension/Sources/content.ts @@ -84,12 +84,9 @@ function translationHandler(message: ReceivedTranslation): void { const args = { sourceLanguage: message.sourceLanguage || null, - translation: message.translation.replace(/\n/g, "
"), - transliteration: message.transliteration.replace(/\n/g, "
"), - sourceTransliteration: message.sourceTransliteration.replace( - /\n/g, - "
" - ), + translation: convertHTMLToDisplayText(message.translation), + transliteration: convertHTMLToDisplayText(message.transliteration), + sourceTransliteration: convertHTMLToDisplayText(message.sourceTransliteration), synonyms: message.synonyms ? message.synonyms.map((synonym) => ({ pos: synonym.pos, @@ -203,4 +200,17 @@ function getSelectedText(): string | undefined { } } +function convertHTMLToDisplayText(htmlString: string): string { + let encodedString = htmlString.replace(/&/g, '&'); + encodedString = encodedString.replace(//g, '>'); + encodedString = encodedString.replace(/"/g, '"'); + + return convertNewlineToHTML(encodedString) +} + +function convertNewlineToHTML(text: string): string { + return text.replace(/\n/g, "
") +} + init();