Merge pull request #35 from micz/setComposeDetails

Using setComposeDetails
This commit is contained in:
Mic 2024-05-01 21:57:45 +02:00 committed by GitHub
commit 9b0cbf84f2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 102 additions and 46 deletions

View file

@ -1,6 +1,13 @@
# ![ThunderAI icon](images/icon-32px.png "ThunderAI") ThunderAI Release Notes
<h2>Version 1.0.4 - 01/05/2024</h2>
<ul>
<li>Stripping the quotation marks from the response.</li>
<li>Now it is possible to undo text modifications implemented by ChatGPT (CTRL+Z on Windows). See issue <a href="https://github.com/micz/ThunderAI/issues/34">#34</a></li>
<li>Now closing the compose window after unsaved text modifications implemented by ChatGPT triggers the usual warning message. See issue <a href="https://github.com/micz/ThunderAI/issues/30">#30</a></li>
<li>Updated the "Important Information" section on the options page, as it is no longer possible to disable the ChatGPT chat history.</li>
</ul>
<h2>Version 1.0.3 - 27/04/2024</h2>
<ul>
<li>Using the right identity when replying to a message. See issue <a href="https://github.com/micz/ThunderAI/issues/31">#31</a></li>

View file

@ -160,12 +160,12 @@
},
"prefsInfoDesc_1": {
"message": "To prevent requests made through this extension from appearing in your ChatGPT chat history, you can disable them in the settings. This can be done directly in the ChatGPT settings within the window that appears when you use the extension. Remember, this option is valid only in this specific context and does not apply to other situations where you use ChatGPT.",
"message": "It is no longer possible to disable the history of ChatGPT chats. Therefore, there is no way to prevent requests made through this addon from appearing in your ChatGPT chat history.",
"description": ""
},
"prefsInfoDesc_2": {
"message": " ",
"message": "If a white page appears when opening the ChatGPT window, it means there is a problem loading the ChatGPT page, not related to the use of this add-on.",
"description": ""
},

View file

@ -160,12 +160,12 @@
},
"prefsInfoDesc_1": {
"message": "Per evitare che le richieste fatte tramite questa estensione compaiano nella tua cronologia chat di ChatGPT, puoi disattivarle nelle impostazioni. Questo si può fare nelle impostazioni di ChatGPT direttamente nella finestra che appare quando utilizzi l'estensione. Ricorda che questa opzione è valida esclusivamente in questo contesto specifico e non si applica ad altre situazioni in cui utilizzi ChatGPT.",
"message": "Non è più possibile disabilitare la cronologia delle chat di ChatGPT. Pertanto, non c'è modo di impedire che le richieste effettuate tramite questo componente aggiuntivo appaiano nella cronologia delle chat di ChatGPT.",
"description": ""
},
"prefsInfoDesc_2": {
"message": " ",
"message": "Se all'apertura della finestra di ChatGPT, viene mostrata una pagina bianca, significa che c'è un problema di caricamento della pagina di ChatGPT, non relativo all'utilizzo di questo componente aggiuntivo.",
"description": ""
},

View file

@ -97,6 +97,7 @@ async function chatgpt_getFromDOM(pos) {
response = responseDivs[nthOfResponse - 1].textContent;
}
response = response.replace(/^ChatGPTChatGPT/, ''); // strip sender name
response = response.trim().replace(/^"|"$/g, ''); // strip quotation marks
//console.log('chatgpt_getFromDOM: ' + response);
}
return response;

View file

@ -16,8 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Modified version derived from https://github.com/ali-raheem/Aify/blob/13ff87583bc520fb80f555ab90a90c5c9df797a7/plugin/content_scripts/compose.js
// Some original methods are a modified version derived from https://github.com/ali-raheem/Aify/blob/13ff87583bc520fb80f555ab90a90c5c9df797a7/plugin/content_scripts/compose.js
const makeParagraphs = (text, func) => {
const chunks = text.split(/\n{2,}/);
@ -35,34 +34,7 @@ const makeParagraphs = (text, func) => {
};
const insert = function (text) {
const prefix = window.document.body.getElementsByClassName("moz-cite-prefix");
if (prefix.length > 0) {
const divider = prefix[0];
let sibling = divider.previousSibling;
while (sibling) {
window.document.body.removeChild(sibling);
sibling = divider.previousSibling;
}
}
return makeParagraphs(text, function (p) {
window.document.body.insertBefore(p, window.document.body.firstChild);
});
}
/*const insertReply = function (text) {
const prefix = window.document.body.getElementsByClassName("moz-cite-prefix");
if (prefix.length > 0) {
const divider = prefix[0];
let sibling = divider.previousSibling;
return makeParagraphs(text, function (p) {
window.document.body.insertBefore(p, sibling);
});
}
}*/
browser.runtime.onMessage.addListener((message) => {
browser.runtime.onMessage.addListener(async (message) => {
switch (message.command) {
case "getSelectedText":
return Promise.resolve(window.getSelection().toString());
@ -70,7 +42,7 @@ switch (message.command) {
case "replaceSelectedText":
const selectedText = window.getSelection().toString();
if (selectedText === '') {
return insert(message.text);
return;
}
const sel = window.getSelection();
if (!sel || sel.type !== "Range" || !sel.rangeCount) {
@ -81,6 +53,7 @@ switch (message.command) {
makeParagraphs(message.text, function (p) {
r.insertNode(p);
});
browser.runtime.sendMessage({command: "compose_reloadBody", tabId: message.tabId});
break;
case "getText":
@ -99,10 +72,6 @@ switch (message.command) {
case "getTextOnly":
return Promise.resolve(window.document.body.innerText);
case "insertText":
insert(message.text);
break;
case 'promptTooLong':
alert(browser.i18n.getMessage('msg_prompt_too_long'));
break;

View file

@ -59,3 +59,66 @@ function extractEmail(text) {
const match = text.match(emailRegex);
return match ? match[0] : '';
}
export async function reloadBody(tabId){
let composeDetails = await messenger.compose.getComposeDetails(tabId);
let originalHtmlBody = composeDetails.body + " ";
await messenger.compose.setComposeDetails(tabId, {body: originalHtmlBody});
}
export async function getOriginalBody(tabId){
let composeDetails = await messenger.compose.getComposeDetails(tabId);
return composeDetails.body;
}
export async function setBody(tabId, fullHtmlBody){
await messenger.compose.setComposeDetails(tabId, {body: fullHtmlBody});
}
export async function replaceBody(tabId, text){
let composeDetails = await messenger.compose.getComposeDetails(tabId);
let originalHtmlBody = composeDetails.body;
//console.log('originalHtmlBody: ' + originalHtmlBody);
let fullBody = insertText(text, originalHtmlBody);
//console.log('fullBody: ' + fullBody);
await messenger.compose.setComposeDetails(tabId, {body: fullBody});
}
// The following methods are a modified version derived from https://github.com/ali-raheem/Aify/blob/13ff87583bc520fb80f555ab90a90c5c9df797a7/plugin/content_scripts/compose.js
const insertText = function (text, fullBody_string) {
const parser = new DOMParser();
let fullBody = parser.parseFromString(fullBody_string, "text/html");
const prefix = fullBody.getElementsByClassName("moz-cite-prefix");
if (prefix.length > 0) {
const divider = prefix[0];
let sibling = divider.previousSibling;
while (sibling) {
fullBody.body.removeChild(sibling);
sibling = divider.previousSibling;
}
}
let final_p = document.createElement("p");
final_p.innerHTML = "<p><br/><br/></p>";
fullBody.body.insertBefore(final_p, fullBody.body.firstChild);
makeParagraphs(text, function (p) {
fullBody.body.insertBefore(p, fullBody.body.firstChild);
});
return fullBody.body.innerHTML;
}
const makeParagraphs = (text, func) => {
const chunks = text.split(/\n{2,}/);
if (chunks.length == 1) {
return func(document.createTextNode(text));
}
const paragraphs = chunks.map((t) => {
const p = document.createElement("p");
p.innerText = t;
return p;
});
for (let i = paragraphs.length - 1; i >= 0; i--) {
func(paragraphs[i]);
}
};

View file

@ -2,7 +2,7 @@
"manifest_version": 2,
"name": "ThunderAI",
"description": "__MSG_extensionDescription__",
"version": "1.0.3",
"version": "1.0.4",
"author": "Mic (m@micz.it)",
"browser_specific_settings": {
"gecko": {

View file

@ -19,9 +19,11 @@
import { mzta_script } from './js/mzta-chatgpt.js';
import { prefs_default } from './options/mzta-options-default.js';
import { mzta_Menus } from './js/mzta-menus.js';
import { getCurrentIdentity } from './js/mzta-utils.js';
import { getCurrentIdentity, getOriginalBody, reloadBody, replaceBody, setBody } from './js/mzta-utils.js';
var createdWindowID = null;
var original_html = '';
var modified_html = '';
browser.composeScripts.register({
js: [{file: "/js/mzta-compose-script.js"}]
@ -62,17 +64,18 @@ messenger.runtime.onMessage.addListener(async (message, sender, sendResponse) =>
break;
case 'chatgpt_replaceSelectedText':
//console.log('chatgpt_replaceSelectedText: [' + message.tabId +'] ' + message.text)
browser.tabs.sendMessage(message.tabId, { command: "replaceSelectedText", text: message.text });
original_html = await getOriginalBody(message.tabId);
await browser.tabs.sendMessage(message.tabId, { command: "replaceSelectedText", text: message.text, tabId: message.tabId });
return true;
case 'chatgpt_replyMessage':
const paragraphsHtmlString = message.text;
let prefs = await browser.storage.sync.get({reply_type: 'reply_all'});
console.log('reply_type: ' + prefs.reply_type);
//console.log('reply_type: ' + prefs.reply_type);
let replyType = 'replyToAll';
if(prefs.reply_type === 'reply_sender'){
replyType = 'replyToSender';
}
console.log('replyType: ' + replyType);
//console.log('replyType: ' + replyType);
browser.messageDisplay.getDisplayedMessage(message.tabId).then(async (mailMessage) => {
let reply_tab = await browser.compose.beginReply(mailMessage.id, replyType, {
type: "reply",
@ -100,10 +103,16 @@ messenger.runtime.onMessage.addListener(async (message, sender, sendResponse) =>
}
});
// we need to wait for the compose windows to load the content script
setTimeout(() => browser.tabs.sendMessage(reply_tab.id, { command: "insertText", text: paragraphsHtmlString }), 500);
//setTimeout(() => browser.tabs.sendMessage(reply_tab.id, { command: "insertText", text: paragraphsHtmlString, tabId: reply_tab.id }), 500);
setTimeout(async () => await replaceBody(reply_tab.id, paragraphsHtmlString), 500);
return true;
});
break;
case 'compose_reloadBody':
modified_html = await getOriginalBody(message.tabId);
await setBody(message.tabId, original_html);
await setBody(message.tabId, modified_html);
break;
default:
break;
}

View file

@ -7,6 +7,13 @@
<body>
<div id="miczBackPrefs"><a href="mzta-options.html">__MSG_backToOptionsText__</a></div>
<div id="miczRelNotes"><h1>ThunderAI Release Notes</h1>
<h2>Version 1.0.4 - 01/05/2024</h2>
<ul>
<li>Stripping the quotation marks from the response.</li>
<li>Now it is possible to undo text modifications implemented by ChatGPT (CTRL+Z on Windows). See issue <a href="https://github.com/micz/ThunderAI/issues/34">#34</a></li>
<li>Now closing the compose window after unsaved text modifications implemented by ChatGPT triggers the usual warning message. See issue <a href="https://github.com/micz/ThunderAI/issues/30">#30</a></li>
<li>Updated the "Important Information" section on the options page, as it is no longer possible to disable the ChatGPT chat history.</li>
</ul>
<h2>Version 1.0.3 - 27/04/2024</h2>
<ul>
<li>Using the right identity when replying to a message. See issue <a href="https://github.com/micz/ThunderAI/issues/31">#31</a></li>