Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions PolyglotSafariExtension/Sources/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,28 @@ function translationErrorHandler(message: UpstreamError) {
showError(message.error);
}

function convertNewlineToHTML(text: string): string {
return text.replace(/\n/g, "<br/>");
}

function convertHTMLToDisplayText(htmlString: string): string {
let encodedString = htmlString.replace(/&/g, "&amp;");
encodedString = encodedString.replace(/</g, "&lt;");
encodedString = encodedString.replace(/>/g, "&gt;");
encodedString = encodedString.replace(/"/g, "&quot;");

return convertNewlineToHTML(encodedString);
}

function translationHandler(message: ReceivedTranslation): void {
if (message.id !== window.location.href) return;

const args = {
sourceLanguage: message.sourceLanguage || null,
translation: message.translation.replace(/\n/g, "<br/>"),
transliteration: message.transliteration.replace(/\n/g, "<br/>"),
sourceTransliteration: message.sourceTransliteration.replace(
/\n/g,
"<br/>"
translation: convertHTMLToDisplayText(message.translation),
transliteration: convertHTMLToDisplayText(message.transliteration),
sourceTransliteration: convertHTMLToDisplayText(
message.sourceTransliteration
),
synonyms: message.synonyms
? message.synonyms.map((synonym) => ({
Expand Down