Skip to content

Commit

Permalink
- testing stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
TobiasMue91 committed Apr 24, 2024
1 parent bc0aa47 commit 7765dec
Showing 1 changed file with 38 additions and 43 deletions.
81 changes: 38 additions & 43 deletions tools/gptranslator.html
Original file line number Diff line number Diff line change
Expand Up @@ -616,31 +616,58 @@ <h3 style="cursor: pointer;" data-target="#aboutText">About ️👆</h3>
translateButton.textContent = "Translate";
});

function parseJsonResponse(responseContent) {
try {
const responseJsonStr = responseContent.substring(
responseContent.indexOf("{"),
responseContent.lastIndexOf("}") + 1
);

return JSON5.parse(responseJsonStr);
} catch (error) {
console.error('Failed to parse JSON response:', error);
return null;
}
}

function sanitizeTranslation(translation) {
return translation.replace(/\n/g, '<br>');
}

function displayTranslations(data) {
resultsContainer.innerHTML = "";

if (!data || !data.choices || data.choices.length === 0) {
resultsContainer.innerHTML = `<p>No response data available.</p>`;
return;
}

const responseContent = data.choices[0].message.content;
const responseJsonStr = responseContent.substring(responseContent.indexOf("{"), responseContent.lastIndexOf("}") + 1);
let responseJsonData;
try {
responseJsonData = fixJSON(responseJsonStr);
} catch (e) {
console.log(e);

const responseJsonData = parseJsonResponse(responseContent);

if (!responseJsonData) {
resultsContainer.innerHTML = `<p>Could not parse translations. But here is the response (most likely) in JSON format:</p><pre>${responseContent}</pre>`;
return;
}
if (responseJsonData && responseJsonData.translation_options) {

if (responseJsonData.translation_options) {
const {detected_language, translation_options} = responseJsonData;
resultsContainer.innerHTML += `<p><strong>Detected Language:</strong> ${detected_language}</p>`;

translation_options.forEach(option => {
const fittingScore = option.hasOwnProperty('fitting_score') ? ` (${option.fitting_score.toFixed(2)})` : '';
const sanitizedTranslation = sanitizeTranslation(option.translation);

resultsContainer.innerHTML += `
<p class="translation">
<strong>Translation:${fittingScore}</strong>
<span class="value" style="cursor: pointer;">${option.translation}</span>
<span class="value" style="cursor: pointer;">${sanitizedTranslation}</span>
<span class="favorite-btn" style="cursor: pointer; float: right;" title="Add to Favorites">⭐</span>
</p>
`;
});

resultsContainer.querySelectorAll('p.translation').forEach(translation => {
translation.querySelector('.value').addEventListener('click', function() {
const spanText = this.innerText;
Expand All @@ -650,14 +677,15 @@ <h3 style="cursor: pointer;" data-target="#aboutText">About ️👆</h3>
console.error('Error in copying text: ', err);
});
});

translation.querySelector('.favorite-btn').addEventListener('click', function() {
const translationText = this.previousElementSibling.innerText;
const inputText = document.getElementById('input-string').value;
addFavorite(inputText, translationText);
});
})
});
} else {
resultsContainer.innerHTML = `<p>Could not retrieve translations.</p>`;
resultsContainer.innerHTML = `<p>No translation options found in the response.</p>`;
}
}

Expand Down Expand Up @@ -731,39 +759,6 @@ <h3 style="cursor: pointer;" data-target="#aboutText">About ️👆</h3>
});
});

const fixJSON = (jsonStr) => {
// Remove newline characters from the input string.
jsonStr = jsonStr.replace(/\n/g, '\\n');

// Substitute all the backslashes from JSON string.
jsonStr = jsonStr.replace(/\\/g, '');

try {
return JSON5.parse(jsonStr);
} catch (error) {
while (true) {
// Search json string specifically for '"'
const match = jsonStr.match(/[\w|"]\s?(")\s?[\w|"]/);

// If we don't find any, then we come out of the loop
if (!match) {
break;
}

// Get the location of \"
const s = match.index + match[0].indexOf('"');
const e = s + 1;
let c = jsonStr.substring(s, e);

// Replace \" with \'
c = c.replace(/"/g, "'");
jsonStr = jsonStr.substring(0, s) + c + jsonStr.substring(e);
}
return JSON5.parse(jsonStr);
}
};


// Additional JavaScript for Camera and OCR Functionality
document.getElementById('close-camera').addEventListener('click', closeCameraModal);
document.getElementById('open-camera').addEventListener('click', openCamera);
Expand Down

0 comments on commit 7765dec

Please sign in to comment.