ThunderAI/js/mzta-chatgpt.js
2024-04-01 22:58:39 +02:00

156 lines
No EOL
6.9 KiB
JavaScript

// Original methods derived from https://github.com/KudoAI/chatgpt.js/blob/7eb8463cd61143fa9e1d5a8ec3c14d3c1b286e54/chatgpt.js
// Using a full string to inject it in the chatgpt page to avoid any security error
export const mzta_script = `
async function chatgpt_sendMsg(msg, method ='') {
const textArea = document.querySelector('form textarea'),
sendButton = document.querySelector('form button[class*="bottom"]');
textArea.value = msg;
textArea.dispatchEvent(new Event('input', { bubbles: true })); // enable send button
const delaySend = setInterval(() => {
if (!sendButton?.hasAttribute('disabled')) { // send msg
method.toLowerCase() == 'click' ? sendButton.click()
: textArea.dispatchEvent(new KeyboardEvent('keydown', { keyCode: 13, bubbles: true }));
clearInterval(delaySend);
}
}, 25);
}
async function chatgpt_isIdle() {
return new Promise(resolve => {
const intervalId = setInterval(() => {
if (chatgpt_getRegenerateButton()) {
clearInterval(intervalId); resolve(true);
}}, 100);});}
function chatgpt_getRegenerateButton() {
for (const mainSVG of document.querySelectorAll('main svg')) {
if (mainSVG.querySelector('path[d*="M4.5 2.5C5.05228"]')) // regen icon found
return mainSVG.parentNode.parentNode;
}
}
async function chatgpt_getFromDOM(pos) {
const responseDivs = document.querySelectorAll('div[data-testid*="conversation-turn"]:nth-child(odd)'),
strPos = pos.toString().toLowerCase();
let response = '';
if (responseDivs.length) {
if (/last|final/.test(strPos)) // get last response
response = responseDivs[responseDivs.length - 1].textContent;
else { // get nth response
const nthOfResponse = (
// Calculate base number
Number.isInteger(pos) ? pos : // do nothing for integers
/^\d+/.test(strPos) ? /^\d+/.exec(strPos)[0] : // extract first digits for strings w/ them
( // convert words to integers for digitless strings
/^(?:1|one|fir)(?:st)?$/.test(strPos) ? 1
: /^(?:2|tw(?:o|en|el(?:ve|f))|seco)(?:nd|t[yi])?(?:e?th)?$/.test(strPos) ? 2
: /^(?:3|th(?:ree|ir?))(?:rd|teen|t[yi])?(?:e?th)?$/.test(strPos) ? 3
: /^(?:4|fou?r)(?:teen|t[yi])?(?:e?th)?$/.test(strPos) ? 4
: /^(?:5|fi(?:ve|f))(?:teen|t[yi])?(?:e?th)?$/.test(strPos) ? 5
: /^(?:6|six)(?:teen|t[yi])?(?:e?th)?$/.test(strPos) ? 6
: /^(?:7|seven)(?:teen|t[yi])?(?:e?th)?$/.test(strPos) ? 7
: /^(?:8|eight?)(?:teen|t[yi])?(?:e?th)?$/.test(strPos) ? 8
: /^(?:9|nine?)(?:teen|t[yi])?(?:e?th)?$/.test(strPos) ? 9
: /^(?:10|ten)(?:th)?$/.test(strPos) ? 10 : 1 )
// Transform base number if suffixed
* ( /(ty|ieth)$/.test(strPos) ? 10 : 1 ) // x 10 if -ty/ieth
+ ( /teen(th)?$/.test(strPos) ? 10 : 0 ) // + 10 if -teen/teenth
);
response = responseDivs[nthOfResponse - 1].textContent;
}
response = response.replace(/^ChatGPTChatGPT/, ''); // strip sender name
}
return response;
}
function chatpgt_scrollToBottom () {
try { document.querySelector('button[class*="cursor"][class*="bottom"]').click(); }
catch (err) { console.error('', err); }
}
function addCustomDiv(prompt_action,tabId) {
// Create <style> element for the CSS
var style = document.createElement('style');
style.innerHTML = ".mzta-header-fixed {position: fixed;top: 0;left: 0;height:100px;width: 100%;background-color: #333;color: white;text-align: center;padding: 10px 0;z-index: 1000;}"
style.innerHTML += "body {padding-top: 100px;}";
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-curr_msg{}";
// Add <style> to the page's <head>
document.head.appendChild(style);
// Fixed div
var divFisso = document.createElement('div');
divFisso.classList.add('mzta-header-fixed');
divFisso.textContent = '';
// span for the text
var curr_msg = document.createElement('span');
curr_msg.id='mzta-curr_msg';
curr_msg.innerHTML = browser.i18n.getMessage("chatgpt_win_working");
divFisso.appendChild(curr_msg);
var pulsante = document.createElement('button');
pulsante.id="mzta-ok_btn";
//console.log('default: '+prompt_action)
switch(prompt_action){
default:
case 0: // close window
pulsante.innerHTML = browser.i18n.getMessage("chatgpt_win_close");
pulsante.onclick = async function() {
browser.runtime.sendMessage({command: "chatgpt_close"});
};
break;
case 1: // do reply
pulsante.innerHTML = browser.i18n.getMessage("chatgpt_win_get_answer");
pulsante.onclick = async function() { // TODO
const response = await chatgpt_getFromDOM('last');
//console.log(response);
browser.runtime.sendMessage({command: "chatgpt_close"});
};
break;
case 2: // replace text
pulsante.innerHTML = browser.i18n.getMessage("chatgpt_win_get_answer");
pulsante.onclick = async function() { // TODO
const response = await chatgpt_getFromDOM('last');
//console.log('replace text: '+tabId)
browser.runtime.sendMessage({command: "chatgpt_replaceSelectedText", text: response, tabId: tabId});
//console.log(response);
browser.runtime.sendMessage({command: "chatgpt_close"});
};
break;
}
pulsante.style.display = 'none';
divFisso.appendChild(pulsante);
document.body.insertBefore(divFisso, document.body.firstChild);
}
function operation_done(){
document.getElementById('mzta-curr_msg').innerHTML = browser.i18n.getMessage("chatgpt_win_job_completed")+"<br>";
document.getElementById('mzta-ok_btn').style.display = 'inline';
chatpgt_scrollToBottom();
}
// Nello script di contenuto
browser.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.command === "chatgpt_send") {
addCustomDiv(message.action,message.tabId);
//console.log(message.prompt);
(async () => {
//await chatgpt.isLoaded();
await chatgpt_sendMsg(message.prompt,'click');
await chatgpt_isIdle();
// console.log(response);
operation_done();
})();
}
});
`