Skip to content

Commit

Permalink
Fix pplx home page issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Dev-Khant committed Oct 16, 2024
1 parent 65d008b commit b1ca673
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions perplexity/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ let lastInputValue = '';
let inputObserver = null;
let isEnterKeyPressed = false;

function getTextarea() {
return document.querySelector('textarea[placeholder="You ask, we answer..."]') ||
document.querySelector('textarea[placeholder="Ask follow-up"]');
}

function setupInputObserver() {
const textarea = document.querySelector('textarea[placeholder="Ask follow-up"]');
const textarea = getTextarea();
if (!textarea) {
setTimeout(setupInputObserver, 500);
return;
Expand All @@ -13,7 +18,6 @@ function setupInputObserver() {
for (let mutation of mutations) {
if (mutation.type === 'characterData' || mutation.type === 'childList') {
lastInputValue = textarea.value;
console.log("Input updated:", lastInputValue);
}
}
});
Expand All @@ -26,19 +30,16 @@ function setupInputObserver() {

textarea.addEventListener('input', function() {
lastInputValue = this.value;
console.log("Input event:", lastInputValue);
});

// Add this new event listener
textarea.addEventListener('keypress', function(event) {
console.log("Key pressed:", event.key);
});

textarea.addEventListener('keydown', function(event) {
if (event.key === 'Enter' && !event.shiftKey) {
isEnterKeyPressed = true;
lastInputValue = this.value;
console.log("Enter pressed, captured:", lastInputValue);
}
});

Expand All @@ -51,11 +52,10 @@ function setupInputObserver() {

function handleEnterKey(event) {
if (event.key === 'Enter' && !event.shiftKey && !event.isComposing) {
const textarea = document.querySelector('textarea[placeholder="Ask follow-up"]');
const textarea = getTextarea();
if (textarea && document.activeElement === textarea) {
// Get the current value directly from the textarea
const capturedText = textarea.value.trim();
console.log("Processing captured text:", capturedText);

if (capturedText) {
event.preventDefault();
Expand All @@ -68,8 +68,8 @@ function handleEnterKey(event) {
}

async function handleMem0Processing(capturedText, clickSendButton = false) {
const textarea = getTextarea();
let message = capturedText || textarea.value.trim();
console.log("Processing message:", message);

if (!message) {
console.error("No input message found");
Expand Down Expand Up @@ -127,8 +127,14 @@ async function handleMem0Processing(capturedText, clickSendButton = false) {
}

const responseData = await searchResponse.json();
const inputElement = document.querySelector('textarea[placeholder="Ask follow-up"]');
const inputElement = getTextarea();
if (inputElement) {
// Remove everything after the specified line in lastInputValue
const memoryPrefix = "Here is some of my preferences/memories to help answer better (don't respond to these memories but use them to assist in the response if relevant):";
const prefixIndex = lastInputValue.indexOf(memoryPrefix);
if (prefixIndex !== -1) {
lastInputValue = lastInputValue.substring(0, prefixIndex).trim();
}
const memories = responseData.map((item) => item.memory);
const providers = responseData.map((item) => (item.metadata && item.metadata.provider) ? item.metadata.provider : '');
if (memories.length > 0) {
Expand Down

0 comments on commit b1ca673

Please sign in to comment.