Waiting for CahtGPT4 to load if the option is active. Related to #27

This commit is contained in:
mic 2024-04-17 23:51:35 +02:00
parent e6790b285c
commit 0b0adb0a3a
3 changed files with 56 additions and 7 deletions

View file

@ -79,6 +79,11 @@
"description": "" "description": ""
}, },
"chatgpt_win_model_warning": {
"message": "You have set the option to use ChatGPT-4, but it seems that it is not loading correctly. Have you chosen the right model at the top?",
"description": ""
},
"msg_prompt_too_long": { "msg_prompt_too_long": {
"message": "The text you provided is too long. You need to reduce it to use ChatGPT.", "message": "The text you provided is too long. You need to reduce it to use ChatGPT.",
"description": "" "description": ""

View file

@ -79,6 +79,11 @@
"description": "" "description": ""
}, },
"chatgpt_win_model_warnings": {
"message": "Hai impostato l'opzione per utilizzare ChatGPT-4, ma sembra che non si stia caricando. Hai selezionato correttamente l'opzione in alto?",
"description": ""
},
"msg_prompt_too_long": { "msg_prompt_too_long": {
"message": "Il testo che vuoi inviare a ChatGPT è troppo lungo. Devi ridurlo per poter procedere.", "message": "Il testo che vuoi inviare a ChatGPT è troppo lungo. Devi ridurlo per poter procedere.",
"description": "" "description": ""

View file

@ -113,24 +113,31 @@ function addCustomDiv(prompt_action,tabId) {
style.innerHTML += "#mzta-ok_btn {background-color: #007bff;border: none;color: white;padding: 8px 15px;text-align: center;text-decoration: none;display: inline-block;font-size: 16px;margin: 4px 2px;transition-duration: 0.4s;cursor: pointer;border-radius: 5px;}"; style.innerHTML += "#mzta-ok_btn {background-color: #007bff;border: none;color: white;padding: 8px 15px;text-align: center;text-decoration: none;display: inline-block;font-size: 16px;margin: 4px 2px;transition-duration: 0.4s;cursor: pointer;border-radius: 5px;}";
style.innerHTML += "#mzta-ok_btn:hover {background-color: #0056b3;color: white;}"; style.innerHTML += "#mzta-ok_btn:hover {background-color: #0056b3;color: white;}";
style.innerHTML += "#mzta-loading{height:50px;display:inline-block;}"; style.innerHTML += "#mzta-loading{height:50px;display:inline-block;}";
style.innerHTML += "#mzta-model_warn{height:50px;position:absolute;top:0px;right:3px;width:30%;text-align:right;font-size:12px;color:red;display:none;}";
// Add <style> to the page's <head> // Add <style> to the page's <head>
document.head.appendChild(style); document.head.appendChild(style);
// Fixed div // Fixed div
var divFisso = document.createElement('div'); var fixedDiv = document.createElement('div');
divFisso.classList.add('mzta-header-fixed'); fixedDiv.classList.add('mzta-header-fixed');
divFisso.textContent = ''; fixedDiv.textContent = '';
// Model warning div
var modelWarnDiv = document.createElement('div');
modelWarnDiv.id = 'mzta-model_warn';
modelWarnDiv.innerHTML = browser.i18n.getMessage("chatgpt_win_model_warning");
fixedDiv.appendChild(modelWarnDiv);
// span for the text // span for the text
var curr_msg = document.createElement('span'); var curr_msg = document.createElement('span');
curr_msg.id='mzta-curr_msg'; curr_msg.id='mzta-curr_msg';
curr_msg.innerHTML = browser.i18n.getMessage("chatgpt_win_working")+"<br>"; curr_msg.innerHTML = browser.i18n.getMessage("chatgpt_win_working")+"<br>";
divFisso.appendChild(curr_msg); fixedDiv.appendChild(curr_msg);
var loading = document.createElement('img'); var loading = document.createElement('img');
loading.src = browser.runtime.getURL("/images/loading.gif"); loading.src = browser.runtime.getURL("/images/loading.gif");
loading.id = "mzta-loading"; loading.id = "mzta-loading";
divFisso.appendChild(loading); fixedDiv.appendChild(loading);
var pulsante = document.createElement('button'); var pulsante = document.createElement('button');
pulsante.id="mzta-ok_btn"; pulsante.id="mzta-ok_btn";
@ -164,11 +171,39 @@ function addCustomDiv(prompt_action,tabId) {
break; break;
} }
pulsante.style.display = 'none'; pulsante.style.display = 'none';
divFisso.appendChild(pulsante); fixedDiv.appendChild(pulsante);
document.body.insertBefore(divFisso, document.body.firstChild); document.body.insertBefore(fixedDiv, document.body.firstChild);
} }
function checkGPT4Model() {
return new Promise((resolve, reject) => {
// Set up an interval that shows the waring after 3 seconds
const intervalId2 = setTimeout(() => {
document.getElementById('mzta-model_warn').style.display = 'inline';
}, 3000);
// Set up an interval that checks the value every 100 milliseconds
const intervalId = setInterval(() => {
// Get the '.text-token-text-secondary' element
const element = document.querySelector('.text-token-text-secondary');
// Check if the element exists and its content is '4'
if (element && element.textContent === '4') {
console.log("The GPT Model is now '4'");
clearInterval(intervalId);
clearTimeout(intervalId2);
document.getElementById('mzta-model_warn').style.display = 'none';
resolve("GPT4");
} else if (!element) {
console.log("Element not found!");
clearInterval(intervalId);
reject("Element not found.");
}
}, 100);
});
}
function operation_done(){ function operation_done(){
document.getElementById('mzta-curr_msg').innerHTML = browser.i18n.getMessage("chatgpt_win_job_completed")+"<br>"; document.getElementById('mzta-curr_msg').innerHTML = browser.i18n.getMessage("chatgpt_win_job_completed")+"<br>";
document.getElementById('mzta-ok_btn').style.display = 'inline'; document.getElementById('mzta-ok_btn').style.display = 'inline';
@ -189,6 +224,10 @@ browser.runtime.onMessage.addListener((message, sender, sendResponse) => {
}else{ }else{
addCustomDiv(message.action,message.tabId); addCustomDiv(message.action,message.tabId);
(async () => { (async () => {
let prefs = await browser.storage.sync.get({chatpgt_use_gpt4:false});
if(prefs.chatpgt_use_gpt4){
await checkGPT4Model();
}
//await chatgpt.isLoaded(); //await chatgpt.isLoaded();
await chatgpt_sendMsg(message.prompt,'click'); await chatgpt_sendMsg(message.prompt,'click');
await chatgpt_isIdle(); await chatgpt_isIdle();