Skip to content

Commit

Permalink
refactor : simplified logic and reduced code duplication in listener …
Browse files Browse the repository at this point in the history
…class
  • Loading branch information
oggnimodd committed Oct 4, 2023
1 parent b787b6b commit ae909a2
Showing 1 changed file with 85 additions and 65 deletions.
150 changes: 85 additions & 65 deletions apps/plugin/content-scripts/listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,92 +5,112 @@ type ChatProvider = "chat-gpt" | "poe" | "claude" | "perplexity";
class Listener {
private autoSubmit: boolean;
private chatprovider: ChatProvider;
private prompt: string;

constructor(autoSubmit: boolean, provider: ChatProvider) {
this.autoSubmit = autoSubmit;
this.chatprovider = provider;
this.prompt = "";
}

listen() {
// Listen message from iframe
window.addEventListener("message", (event) => {
if (event.origin === extensionUrl) {
if (this.chatprovider === "chat-gpt") {
const promptField = document.querySelector(
"#prompt-textarea",
) as HTMLTextAreaElement;
const submitButton = document.querySelector(
'[data-testid="send-button"]',
) as HTMLButtonElement;
this.prompt = event.data;

switch (this.chatprovider) {
case "chat-gpt":
this.chatGPT();
break;
case "claude":
this.claude();
break;
case "perplexity":
this.perplexity();
break;
case "poe":
this.poe();
break;
default:
break;
}
}
});
}

this.setNativeValue(promptField, event.data);
chatGPT() {
const promptField = document.querySelector(
"#prompt-textarea",
) as HTMLTextAreaElement;
const submitButton = document.querySelector(
'[data-testid="send-button"]',
) as HTMLButtonElement;

this.submit(submitButton);
}
this.setNativeValue(promptField, this.prompt);
this.submit(submitButton);
}

if (this.chatprovider === "poe") {
const promptField = document.querySelector(
"[class*='ChatMessageInputContainer_textArea'] textarea",
) as HTMLTextAreaElement;
const submitButton = document.querySelector(
"[class*='ChatMessageInputContainer_submitButton']",
) as HTMLButtonElement;
claude() {
const promptField = document.querySelector(
"[contenteditable='true'].ProseMirror",
) as HTMLDivElement;

if (submitButton.disabled) {
submitButton.removeAttribute("disabled");
}
const paragraph = document.createElement("p");
paragraph.innerHTML = this.prompt;
promptField.appendChild(paragraph);

promptField.value = event.data;
promptField.dispatchEvent(new Event("input", { bubbles: true }));
setTimeout(() => {
const submitButton = document.querySelector(
"[aria-label='Send Message']",
) as HTMLButtonElement;

this.submit(submitButton);
}
this.submit(submitButton);
}, 100);
}

if (this.chatprovider === "claude") {
const promptField = document.querySelector(
"[contenteditable='true'].ProseMirror",
) as HTMLDivElement;
poe() {
const promptField = document.querySelector(
"[class*='ChatMessageInputContainer_textArea'] textarea",
) as HTMLTextAreaElement;
const submitButton = document.querySelector(
"[class*='sendButton']",
) as HTMLButtonElement;

const paragraph = document.createElement("p");
paragraph.innerHTML = event.data;
promptField.appendChild(paragraph);
if (submitButton.disabled) {
submitButton.removeAttribute("disabled");
}

setTimeout(() => {
const submitButton = document.querySelector(
"[aria-label='Send Message']",
) as HTMLButtonElement;
promptField.value = this.prompt;
promptField.dispatchEvent(new Event("input", { bubbles: true }));

this.submit(submitButton);
}, 100);
}
this.submit(submitButton);
}

if (this.chatprovider === "perplexity") {
const promptField =
(document.querySelector(
"textarea[placeholder*='follow']",
) as HTMLTextAreaElement) ||
(document.querySelector(
"textarea[placeholder*='nything']",
) as HTMLTextAreaElement);
const submitButton =
(document.querySelector(
".relative > div:has(lt-mirror):has(textarea) ~ div.absolute:has(button + button) button:has(svg[data-icon*='arrow'])",
) as HTMLButtonElement) ||
document.querySelector(
".relative:has(textarea[placeholder*='nything']) button:has(svg[data-icon*='arrow'])",
);

if (submitButton.disabled) {
submitButton.removeAttribute("disabled");
}

promptField.value = event.data;
promptField.dispatchEvent(new Event("input", { bubbles: true }));

this.submit(submitButton);
}
}
});
perplexity() {
const promptField =
(document.querySelector(
"textarea[placeholder*='follow']",
) as HTMLTextAreaElement) ||
(document.querySelector(
"textarea[placeholder*='nything']",
) as HTMLTextAreaElement);
const submitButton =
(document.querySelector(
".relative > div:has(lt-mirror):has(textarea) ~ div.absolute:has(button + button) button:has(svg[data-icon*='arrow'])",
) as HTMLButtonElement) ||
document.querySelector(
".relative:has(textarea[placeholder*='nything']) button:has(svg[data-icon*='arrow'])",
);

if (submitButton.disabled) {
submitButton.removeAttribute("disabled");
}

promptField.value = this.prompt;
promptField.dispatchEvent(new Event("input", { bubbles: true }));

this.submit(submitButton);
}

submit(submitButton: HTMLButtonElement) {
Expand Down

0 comments on commit ae909a2

Please sign in to comment.