import React, { useEffect, useState } from "react"; import { goTo, Router, } from 'react-chrome-extension-router'; import { createRoot } from "react-dom/client"; import { ToastContainer, toast } from 'react-toastify'; import 'react-toastify/dist/ReactToastify.css'; import "./assets/tailwind.css" import { convertHtmlToMarkdown } from "dom-to-semantic-markdown"; import { WebHistory } from "./interfaces"; import { webhistoryToLangChainDocument, getRenderedHtml, emptyArr } from "./commons"; import Loading from "./pages/Loading"; import { LoginForm } from "./pages/LoginForm"; import { FillEnvVariables } from "./pages/EnvVarSettings"; import { API_SECRET_KEY, BACKEND_URL } from "./env"; export async function clearMem(): Promise { try { let webHistory = await chrome.storage.local.get(["webhistory"]); let urlQueue = await chrome.storage.local.get(["urlQueueList"]); let timeQueue = await chrome.storage.local.get(["timeQueueList"]); if (!webHistory.webhistory) { return } //Main Cleanup COde chrome.tabs.query({}, async (tabs) => { //Get Active Tabs Ids // console.log("Event Tabs",tabs) let actives = tabs.map((tab) => { if (tab.id) { return tab.id } }) actives = actives.filter((item: any) => item) //Only retain which is still active const newHistory = webHistory.webhistory.map((element: any) => { //@ts-ignore if (actives.includes(element.tabsessionId)) { return element } }) const newUrlQueue = urlQueue.urlQueueList.map((element: any) => { //@ts-ignore if (actives.includes(element.tabsessionId)) { return element } }) const newTimeQueue = timeQueue.timeQueueList.map((element: any) => { //@ts-ignore if (actives.includes(element.tabsessionId)) { return element } }) await chrome.storage.local.set({ webhistory: newHistory.filter((item: any) => item) }); await chrome.storage.local.set({ urlQueueList: newUrlQueue.filter((item: any) => item) }); await chrome.storage.local.set({ timeQueueList: newTimeQueue.filter((item: any) => item) }); toast.info("History Store Deleted!", { position: "bottom-center" }); }); } catch (error) { console.log(error); } } export const Popup = () => { const [noOfWebPages, setNoOfWebPages] = useState(0); const [loading, setLoading] = useState(true); useEffect(() => { const verifyToken = async () => { const token = localStorage.getItem('token'); // console.log(token) try { const response = await fetch(`${BACKEND_URL}/verify-token/${token}`); if (!response.ok) { throw new Error('Token verification failed'); }else{ const NEO4JURL = localStorage.getItem('neourl'); const NEO4JUSERNAME = localStorage.getItem('neouser'); const NEO4JPASSWORD = localStorage.getItem('neopass'); const OPENAIKEY = localStorage.getItem('openaikey'); const check = (NEO4JURL && NEO4JUSERNAME && NEO4JPASSWORD && OPENAIKEY) if(!check){ goTo(FillEnvVariables); } } } catch (error) { localStorage.removeItem('token'); goTo(LoginForm); } }; verifyToken(); setLoading(false) }, []); useEffect(() => { async function onLoad() { try { chrome.storage.onChanged.addListener( (changes: any, areaName: string) => { if (changes.webhistory) { // console.log("changes.webhistory", changes.webhistory) const webhistory = changes.webhistory.newValue; let sum = 0 webhistory.forEach((element: any) => { sum = sum + element.tabHistory.length }); setNoOfWebPages(sum) } // console.log(changes) // console.log(areaName) } ); const webhistoryObj = await chrome.storage.local.get(["webhistory"]); if (webhistoryObj.webhistory.length) { const webhistory = webhistoryObj.webhistory; if (webhistoryObj) { let sum = 0 webhistory.forEach((element: any) => { sum = sum + element.tabHistory.length }); setNoOfWebPages(sum) } } else { setNoOfWebPages(0) } } catch (error) { console.log(error); } } onLoad() }, []); const saveData = async () => { try { // setLoading(true); const webhistoryObj = await chrome.storage.local.get(["webhistory"]); const webhistory = webhistoryObj.webhistory; if (webhistory) { let processedHistory: any[] = [] let newHistoryAfterCleanup: any[] = [] webhistory.forEach((element: any) => { let tabhistory = element.tabHistory; for (let i = 0; i < tabhistory.length; i++) { tabhistory[i].pageContentMarkdown = convertHtmlToMarkdown(tabhistory[i].renderedHtml, { extractMainContent: true, enableTableColumnTracking: true, }) delete tabhistory[i].renderedHtml } processedHistory.push({ tabsessionId: element.tabsessionId, tabHistory: tabhistory, }) newHistoryAfterCleanup.push({ tabsessionId: element.tabsessionId, tabHistory: emptyArr, }) }); await chrome.storage.local.set({ webhistory: newHistoryAfterCleanup }); let toSaveFinally = [] for (let i = 0; i < processedHistory.length; i++) { const markdownFormat = webhistoryToLangChainDocument(processedHistory[i].tabsessionId, processedHistory[i].tabHistory) toSaveFinally.push(...markdownFormat) } // console.log("SAVING", toSaveFinally) const toSend = { documents: toSaveFinally, neourl: localStorage.getItem('neourl'), neouser: localStorage.getItem('neouser'), neopass: localStorage.getItem('neopass'), openaikey: localStorage.getItem('openaikey'), apisecretkey: API_SECRET_KEY } // console.log("toSend",toSend) const requestOptions = { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(toSend), }; toast.info("Save Job Initiated.", { position: "bottom-center" }); const response = await fetch(`${BACKEND_URL}/kb/`, requestOptions); const res = await response.json(); if (res.success) { toast.success("Save Job Completed.", { position: "bottom-center", autoClose: false }); } } } catch (error) { console.log(error); } }; // async function showMem(): Promise { // // localStorage.removeItem('token'); // // await chrome.storage.local.clear() // const webhistoryObj = await chrome.storage.local.get(["webhistory"]); // const urlQueue = await chrome.storage.local.get(["urlQueueList"]); // const timeQueue = await chrome.storage.local.get(["timeQueueList"]); // console.log("CURR MEM", webhistoryObj, urlQueue, timeQueue); // // await chrome.storage.local.set({ // // urlQueueList: urlQueueListObj.urlQueueList, // // }); // // await chrome.storage.local.set({ // // timeQueueList: timeQueueListObj.timeQueueList, // // }); // // clearMem() // } async function logOut(): Promise { localStorage.removeItem('token'); goTo(LoginForm) } async function saveCurrSnapShot(): Promise { chrome.tabs.query({ active: true, currentWindow: true }, async function (tabs) { const tab = tabs[0]; if (tab.id) { // await initWebHistory(tab.id); // await initQueues(tab.id); const tabId: number = tab.id const result = await chrome.scripting.executeScript({ // @ts-ignore target: { tabId: tab.id }, // @ts-ignore function: getRenderedHtml, }); let toPushInTabHistory = result[0].result; // const { renderedHtml, title, url, entryTime } = result[0].result; // //Updates 'tabhistory' let webhistoryObj = await chrome.storage.local.get(["webhistory"]); const webHistoryOfTabId = webhistoryObj.webhistory.filter( (data: WebHistory) => { return data.tabsessionId === tab.id; } ); let tabhistory = webHistoryOfTabId[0].tabHistory; const urlQueueListObj = await chrome.storage.local.get(["urlQueueList"]); const timeQueueListObj = await chrome.storage.local.get(["timeQueueList"]); const isUrlQueueThere = urlQueueListObj.urlQueueList.find((data: WebHistory) => data.tabsessionId === tabId) const isTimeQueueThere = timeQueueListObj.timeQueueList.find((data: WebHistory) => data.tabsessionId === tabId) // console.log(isUrlQueueThere) // console.log(isTimeQueueThere) // console.log(isTimeQueueThere.timeQueue[isTimeQueueThere.length - 1]) toPushInTabHistory.duration = toPushInTabHistory.entryTime - isTimeQueueThere.timeQueue[isTimeQueueThere.timeQueue.length - 1] if (isUrlQueueThere.urlQueue.length == 1) { toPushInTabHistory.reffererUrl = 'START' } if (isUrlQueueThere.urlQueue.length > 1) { toPushInTabHistory.reffererUrl = isUrlQueueThere.urlQueue[isUrlQueueThere.urlQueue.length - 2]; } tabhistory.push(toPushInTabHistory); // console.log(toPushInTabHistory) //Update Webhistory try { webhistoryObj.webhistory.find( (data: WebHistory) => data.tabsessionId === tab.id ).tabHistory = tabhistory; await chrome.storage.local.set({ webhistory: webhistoryObj.webhistory, }); } catch (error) { console.log(error); } toast.success("Saved Snapshot !", { position: "bottom-center" }); } }); } if (loading) { return ; } else { return (
{/*
showMem()}>ShowMem
*/}
logo SurfSense
brain
{noOfWebPages}
) } }; const root = createRoot(document.getElementById("root")!); root.render( ); // chrome.tabs.query({ active: true, currentWindow: true }, async function (tabs) { // const tab = tabs[0]; // if (tab.id) { // await initWebHistory(tab.id); // await initQueues(tab.id); // } // }); // export const LoginForm = () => { // const [username, setUsername] = useState(''); // const [password, setPassword] = useState(''); // const [error, setError] = useState(''); // const [loading, setLoading] = useState(false); // // const navigate = useNavigate(); // const validateForm = () => { // if (!username || !password) { // setError('Username and password are required'); // return false; // } // setError(''); // return true; // }; // const handleSubmit = async (event: { preventDefault: () => void; }) => { // event.preventDefault(); // if (!validateForm()) return; // setLoading(true); // const formDetails = new URLSearchParams(); // formDetails.append('username', username); // formDetails.append('password', password); // try { // const response = await fetch('http://localhost:8000/token', { // method: 'POST', // headers: { // 'Content-Type': 'application/x-www-form-urlencoded', // }, // body: formDetails, // }); // setLoading(false); // if (response.ok) { // const data = await response.json(); // await chrome.storage.local.set({ // token: data.access_token, // }); // // localStorage.setItem('token', data.access_token); // goTo(Popup); // } else { // const errorData = await response.json(); // setError(errorData.detail || 'Authentication failed!'); // } // } catch (error) { // setLoading(false); // setError('An error occurred. Please try again later.'); // } // }; // const goToRegister = async () => { // console.log("Reg") // goTo(RegisterForm) // } // return ( // <> //
//
clearMem}>CLEAR MEM
//
// // logo // SurfSense // //
//
//

// Sign in to your account //

//
//
// // setUsername(e.target.value)} name="email" id="email" className="bg-gray-50 border border-gray-300 text-gray-900 rounded-lg focus:ring-primary-600 focus:border-primary-600 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" placeholder="name" /> //
//
// // setPassword(e.target.value)} name="password" id="password" placeholder="••••••••" className="bg-gray-50 border border-gray-300 text-gray-900 rounded-lg focus:ring-primary-600 focus:border-primary-600 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" /> //
// //

// Don’t have an account yet? goToRegister()}>Sign up //

// {error &&

{error}

} //
//
//
//
//
// // ); // } // import { createGlobalState } from 'react-hooks-global-state'; // const initialState = { count: 0 }; // const { useGlobalState } = createGlobalState(initialState); // const [count, setCount] = useGlobalState('count'); // setCount(v => v - 1); // let saveJobsObj = await chrome.storage.local.get(["savejobs"]); // await chrome.storage.local.set({ // savejobs: saveJobsObj.savejobs - 1, // }); // else{ // toPushInTabHistory.reffererUrl = urlQueueLocal.urlQueue[tabId][urlQueueLocal.urlQueue[tabId].length - 1] // } // if(!tabhistory[tabhistory.length - 1].duration){ // tabhistory[tabhistory.length - 1].duration = Date.now() - timeQueueLocal.timeQueue[tabId][timeQueueLocal.timeQueue[tabId].length - 1] // } // if (tabhistory.length === 0) { // toPushInTabHistory.duration = Date.now() - timeQueueLocal.timeQueue[tabId][timeQueueLocal.timeQueue[tabId].length - 1] // } // else { // } // const lastEntryTimeObj = await chrome.storage.local.get([ // "lastEntryTime", // ]); // const autotrackerFlag = await chrome.storage.local.get(["autoTracker"]); // // if (autotrackerFlag.autoTracker) { // // let urlQueue = await chrome.storage.local.get(["urlQueue"]); // // delete urlQueue.urlQueue[tabId]; // // } // //When first entry // if (tabhistory.length === 0) { // let urlQueue = await chrome.storage.local.get(["urlQueue"]); // if (autotrackerFlag.autoTracker) { // toPushInTabHistory.reffererUrl = "START"; // try { // delete urlQueue.urlQueue[tabId]; // } catch (error) { // console.log(error); // } // } else { // if (urlQueue.urlQueue[tabId].length >= 2) { // toPushInTabHistory.reffererUrl = urlQueue.urlQueue[tabId][urlQueue.urlQueue[tabId].length - 2]; // } else { // toPushInTabHistory.reffererUrl = "START"; // } // } // toPushInTabHistory.duration = Date.now() - lastEntryTimeObj.lastEntryTime[tabId]; // tabhistory.push(toPushInTabHistory); // try { // webhistoryObj.webhistory.find( // (data: WebHistory) => data.tabsessionId === tab.id // ).tabHistory = tabhistory; // await chrome.storage.local.set({ // webhistory: webhistoryObj.webhistory, // }); // } catch (error) { // console.log(error); // } // } else { // if (autotrackerFlag.autoTracker) { // toPushInTabHistory.reffererUrl = tabhistory[tabhistory.length - 1].url; // } else { // let urlQueue = await chrome.storage.local.get(["urlQueue"]); // toPushInTabHistory.reffererUrl = urlQueue.urlQueue[tabId][urlQueue.urlQueue[tabId].length - 2]; // } // if (!tabhistory[tabhistory.length - 1].duration) { // toPushInTabHistory.duration = Date.now() - tabhistory[tabhistory.length - 1].entryTime // } // toPushInTabHistory.duration = Date.now() - lastEntryTimeObj.lastEntryTime[tabId]; // tabhistory.push(toPushInTabHistory); // try { // webhistoryObj.webhistory.find( // (data: WebHistory) => data.tabsessionId === tab.id // ).tabHistory = tabhistory; // await chrome.storage.local.set({ // webhistory: webhistoryObj.webhistory, // }); // } catch (error) { // console.log(error); // } // }