Skip to content

Commit

Permalink
last checks needed, chatgpt almost finished
Browse files Browse the repository at this point in the history
  • Loading branch information
giosilvi committed Mar 5, 2023
1 parent fae98ae commit 137588a
Show file tree
Hide file tree
Showing 6 changed files with 384 additions and 61 deletions.
71 changes: 53 additions & 18 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function createContextMenu() {
contexts: ["all"],
});

// Create sub-context menu for prompt on the fly
// Create sub-context menu for prompt on the fly
chrome.contextMenus.create({
id: "ChatGPT",
title: "🤖 ChatGPT",
Expand Down Expand Up @@ -109,9 +109,8 @@ function passTitleOrPrompt(customprompt, symbol) {
const prompt = JSON.parse(customprompt.prompt);
// get the last element of the prompt
const lastElement = prompt[prompt.length - 1];
return `${symbol} ${lastElement['content'].replaceAll("#TEXT#", "%s")}`;
}
else{
return `${symbol} ${lastElement["content"].replaceAll("#TEXT#", "%s")}`;
} else {
return `${symbol} ${customprompt.prompt.replaceAll("#TEXT#", "%s")}`;
}
}
Expand Down Expand Up @@ -252,15 +251,34 @@ chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
}
});

function launchPromptOnTheFly(selectionText, prompt) {
function replacePlaceHolder(selectionText) {
// if there is a text /#TEXT#/g inside selectionText replace with nothing, and use the position to set the cursor later
if (typeof selectionText == "undefined") {
selectionText = "";
}
// if there is a text /#TEXT#/g inside selectionText replace with nothing, and use the position to set the cursor later
var cursorPosition = selectionText.search(/#TEXT#/g);
if (cursorPosition !== -1) {
selectionText = selectionText.replace(/#TEXT#/g, "");
}
return [selectionText, cursorPosition];
}

function launchPopUpInPage(selectionText, prompt, command) {
// replace the placeholder
if (command == "showPopUpOnTheFly") {
var [selectionText, cursorPosition] = replacePlaceHolder(selectionText);
} else if (command == "showPopUpChatGPT") {
// loop over the selectionText and replace the placeholder
for (var i = 0; i < selectionText.length; i++) {

var [contentText, cursorPosition] = replacePlaceHolder(selectionText[i]["content"]);

selectionText[i]["content"] = contentText;
}
} else {
console.error("Unknown command: ", command);
}

// if prompt is not null, use it
if (prompt !== null) {
var model = prompt["model"];
Expand All @@ -283,27 +301,40 @@ function launchPromptOnTheFly(selectionText, prompt) {
// here we want to create a minipop-up to ask the user to insert the prompt
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.tabs.sendMessage(tabs[0].id, {
message: "showPopUpOnTheFly",
message: command,
text: selectionText,
bodyData: bodyData,
cursorPosition: cursorPosition,
});
});
}

function defaultChatPrompt(selectionText) {
var chatPrompt = [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: selectionText },
];
return chatPrompt;
}

// Shortcut to launch the prompt on the fly
chrome.commands.onCommand.addListener(function (command) {
// if command is prompt-on-the-fly, and the context menu is enabled, launch the prompt on the fly
console.log("Command: " + command + " Context menu enabled: " + contextMenuEnabled);
if (command === "prompt-on-the-fly" && contextMenuEnabled) {
if (contextMenuEnabled) {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
// Get the current tab
var tab = tabs[0];
// Send a message to the content script to get the selected text
chrome.tabs.sendMessage(tab.id, { getSelection: true }, function (response) {
// Call the launchPromptOnTheFly function with the selected text and the current tab
launchPromptOnTheFly(response.selection, null);
});
// Send a message to the content script
if (command === "prompt-on-the-fly") {
chrome.tabs.sendMessage(tab.id, { getSelection: true }, function (response) {
launchPopUpInPage(response.selection, null, "showPopUpOnTheFly");
});
} else if (command === "chatGPT") {
chrome.tabs.sendMessage(tab.id, { getSelection: true }, function (response) {
launchPopUpInPage(defaultChatPrompt(response.selection), null, "showPopUpChatGPT");
});
}
});
} else {
//send a message to the content script to show a notification
Expand Down Expand Up @@ -362,25 +393,29 @@ chrome.contextMenus.onClicked.addListener(async (info, tabs) => {
})();
});
} else {
// launch prompt on the fly with the prompt object
launchPromptOnTheFly(prompt.prompt, prompt);
if (prompt.model == "gpt-3.5-turbo") {
launchPopUpInPage(JSON.parse(prompt.prompt), prompt, "showPopUpChatGPT");
} else {
launchPopUpInPage(prompt.prompt, prompt, "showPopUpOnTheFly");
}
}
} else {
// If the prompt number is invalid, send an error message to the tab and log a message to the console
chrome.tabs.sendMessage(tabs.id, "Error: invalid prompt number");
console.log("Error: invalid prompt number");
}
} else {
// If the list of custom prompts does not exist, send an error message to the tab and log a message to the console
chrome.tabs.sendMessage(tabs.id, "Error: no prompt list found");
console.log("Error: no custom prompts");
}
});
}
// If the clicked context menu item is the "On-the-Fly" prompt
else if (info.menuItemId === "On-the-Fly") {
// Launch the "On-the-Fly" prompt with the selected text and the current tabs object
launchPromptOnTheFly(info.selectionText, null);
launchPopUpInPage(info.selectionText, null, "showPopUpOnTheFly");
} else if (info.menuItemId === "ChatGPT") {
const chatPrompt = defaultChatPrompt(info.selectionText);
launchPopUpInPage(chatPrompt, null, "showPopUpChatGPT");
}
});

Expand Down
6 changes: 6 additions & 0 deletions content.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,12 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
popUpShadow.ontheflypopup(request.text, request.bodyData, request.cursorPosition);
addListenersForDrag();
break;
case"showPopUpChatGPT":
handleShowPopUp();
console.log("ChatGPT");
popUpShadow.chatGPTpopup(request.text, request.bodyData, request.cursorPosition);
addListenersForDrag();
break;
case "GPTprompt":
popUpShadow.updatePopupHeader(request, idPopup);
break;
Expand Down
6 changes: 6 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@
"default": "Alt+P"
},
"description": "Launches the Prompt-on-the-Fly functionality"
},
"chatGPT" : {
"suggested_key": {
"default": "Alt+G"
},
"description": "Launches the ChatGPT functionality"
}
}
}
11 changes: 6 additions & 5 deletions popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -269,20 +269,20 @@ <h5>
id="systeminput"
rows="1"
title="Describe the assistant's personality."
style="width: 90%; margin-bottom: 10px; margin-left: 5%"
placeholder="Assistant's personality"
style="margin-bottom: 10px;"
placeholder="Insert assistant's description here..."
>
You are a helpful assistant.</textarea
>
<div id="message-container">
<div class="row" id="message-row-1">
<div class="col-sm-2 ">
<div class="col-sm-2 " style="padding-right:0px">

<button
type="button"
class="btn btn-default btn-sm apikeybutton assistantOrUser"
id="assistantOrUser-1"
style="cursor: pointer; color: #303030; width:6em;"
style="cursor: pointer; color: #303030; width:6em; border: 2px #ededed solid;"
>user
</button>
<button
Expand All @@ -302,7 +302,7 @@ <h5>
id="systeminput-1"
rows="2"
title="User's input."
style="margin-bottom: 10px; float: right"
style="margin-bottom: 10px; float: right; border-radius: 1em;"
placeholder="Type your message here..."
></textarea>
</div>
Expand Down Expand Up @@ -349,6 +349,7 @@ <h5>
id="promptinput"
rows="3"
title="The keyword #TEXT# is later replaced with the selected text. It cannot be erased here"
style = "margin-bottom: 10px; border-radius: 1em;"
placeholder="Type your prompt here... (with #TEXT# placeholder)"
></textarea
>
Expand Down
15 changes: 12 additions & 3 deletions popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -764,12 +764,18 @@ function openLink() {
chrome.tabs.create({ active: true, url: this.href });
}

function userOrAssistant(button) {
function userOrAssistant(button,textarea) {
button.addEventListener("click", function () {
if (button.textContent.includes("user")) {
button.textContent = "assistant";
if (textarea){
textarea.placeholder = "Type the assistant message here...";
}
} else {
button.textContent = "user";
if (textarea){
textarea.placeholder = "Type your message here...";
}
}
});
}
Expand All @@ -786,7 +792,7 @@ function addMessageLogic() {
const addMessageButton = document.getElementById("addChatGPTMessage");
// add listener to the button button[id^='assistantOrUser-'], if clicked, change the text to the other one
const button = document.querySelector("button[id^='assistantOrUser-']");
userOrAssistant(button);
userOrAssistant(button, messageContainer.lastElementChild.querySelector("textarea"));
const remove = document.querySelector("button[id^='deleteChatGPTMessage-']");
removeMessage(remove, messageContainer.lastElementChild);

Expand All @@ -810,13 +816,16 @@ function addMessageLogic() {
const button = messageRow.querySelector("button[id^='assistantOrUser-']");
if (button.textContent.includes("user")) {
button.textContent = "assistant";
messageRow.querySelector("textarea").placeholder = "Type the assistant message here...";

} else {
button.textContent = "user";
messageRow.querySelector("textarea").placeholder = "Type your message here...";
}
button.setAttribute("id", "assistantOrUser-" + messageCount);

// add listener to the button, if clicked, change the text to the other one
userOrAssistant(button);
userOrAssistant(button,messageRow.querySelector("textarea"));
removeMessage(deleteBtn, messageRow);

messageContainer.appendChild(messageRow);
Expand Down
Loading

0 comments on commit 137588a

Please sign in to comment.