Advertisement
doritosYuh

popup-signedin.js

Jul 9th, 2025
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 14.93 KB | Source Code | 0 0
  1. let highlightedElements = [];
  2. let currentHighlightIndex = -1;
  3.  
  4. // popup-signedin.js
  5. const firebaseConfig = {
  6.   apiKey: "AIzaSyA7wge6iWbXVH2bOnH0VLx2ssY5MPGliwo",
  7.   authDomain: "patent-plus-chrome-extension.firebaseapp.com",
  8.   projectId: "patent-plus-chrome-extension",
  9.   storageBucket: "patent-plus-chrome-extension.firebasestorage.app",
  10.   messagingSenderId: "95801979992",
  11.   appId: "1:95801979992:web:462955f495f2b262f8462d",
  12.   measurementId: "G-CGPDJEFHFE"
  13. };
  14.  
  15. firebase.initializeApp(firebaseConfig);
  16. const auth = firebase.auth();
  17.  
  18. // We must reuse the same Firebase config or just rely on the existing init.
  19.  
  20.  
  21.  
  22. const statusDiv = document.getElementById("status");
  23. const highlightBtn = document.getElementById("highlightBtn");
  24. const removeHighlightBtn = document.getElementById("removeHighlightBtn");
  25. const signOutBtn = document.getElementById("signOutBtn");
  26.  
  27. document.getElementById('toggle-buttons').addEventListener('click', () => {
  28.   chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
  29.     chrome.tabs.sendMessage(tabs[0].id, { action: 'toggleFloatingButtons' });
  30.   });
  31. });
  32.  
  33. auth.onAuthStateChanged((user) => {
  34.   if (user) {
  35.     const statusDiv = document.getElementById("status");
  36.     statusDiv.textContent = `Logged in as: ${user.email}`;
  37.   } else {
  38.     // Redirect back to sign-in page if the user is signed out
  39.     window.location.href = "popup.html";
  40.   }
  41. });
  42.  
  43. signOutBtn.addEventListener("click", () => {
  44.   auth.signOut()
  45.     .then(() => {
  46.       // Redirect to sign-in page after sign out
  47.       window.location.href = "popup.html";
  48.     })
  49.     .catch((error) => {
  50.       console.error("Error signing out:", error.message);
  51.     });
  52. });
  53.  
  54. // Check auth state
  55. auth.onAuthStateChanged((user) => {
  56.   if (user) {
  57.     statusDiv.textContent = "Logged in as: " + user.email;
  58.   } else {
  59.     // If no user is logged in, go back to sign in page
  60.     window.location.href = "popup.html";
  61.   }
  62. });
  63.  
  64.  
  65. // Call for toolkit to be enabled
  66.  
  67. /**
  68.  * Helper function to call highlight or remove functions
  69.  * in the content script. We'll rely on window.patentHighlighter
  70.  * in contentScript.js
  71.  *
  72.  * @param {string} method - "highlightAll" or "removeAllHighlights"
  73.  */
  74. function injectScript(method) {
  75.   // Get current active tab
  76.   chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
  77.     const tabId = tabs[0].id;
  78.     // Use chrome.scripting.executeScript with an inline function
  79.     // that calls window.patentHighlighter[method].
  80.     chrome.scripting.executeScript({
  81.       target: { tabId },
  82.       function: (methodName) => {
  83.         if (window.patentHighlighter && window.patentHighlighter[methodName]) {
  84.           window.patentHighlighter[methodName]();
  85.         } else {
  86.           console.log("No patentHighlighter or method not found:", methodName);
  87.         }
  88.       },
  89.       args: [method]
  90.     });
  91.   });
  92. }
  93. // popup-signedin.js
  94.  
  95. // Sign Out
  96. signOutBtn.addEventListener("click", () => {
  97.   auth.signOut()
  98.     .then(() => {
  99.       // After sign out, return to sign in page
  100.       window.location.href = "popup.html";
  101.     });
  102. });
  103.  
  104.  
  105. // 1) Initialize Firestore
  106. const db = firebase.firestore();
  107.  
  108. const saveProjectBtn = document.getElementById("saveProjectBtn");
  109. const projectNameInput = document.getElementById("projectName");
  110. const projectDropdown = document.getElementById("projectDropdown");
  111. const loadProjectBtn = document.getElementById("loadProjectBtn");
  112.  
  113. let currentUser = null;
  114.  
  115. auth.onAuthStateChanged(async (user) => {
  116.   if (user) {
  117.     currentUser = user;
  118.     statusDiv.textContent = "Logged in as: " + user.email;
  119.  
  120.     // Populate the dropdown with existing projects
  121.     populateProjectDropdown(user.uid);
  122.   } else {
  123.     // If no user, go back to sign in
  124.     window.location.href = "popup.html";
  125.   }
  126. });
  127.  
  128. // 2) Save Current Patent to Project
  129. saveProjectBtn.addEventListener("click", () => {
  130.   const projectName = projectNameInput.value.trim();
  131.   if (!projectName) {
  132.     alert("Please enter a project name.");
  133.     return;
  134.   }
  135.  
  136.   // Get the active tab's patent URL
  137.   chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
  138.     const patentUrl = tabs[0].url; // e.g., https://patents.google.com/patent/US12345...
  139.     savePatentToProject(currentUser.uid, projectName, patentUrl);
  140.   });
  141. });
  142.  
  143. // 3) Load Project
  144. loadProjectBtn.addEventListener("click", () => {
  145.   const selectedProject = projectDropdown.value;
  146.   if (!selectedProject) {
  147.     alert("Select a project first.");
  148.     return;
  149.   }
  150.   // For example, open a new window with the project's patents,
  151.   // or show them in the popup, etc. We'll just console.log for now:
  152.   loadProject(currentUser.uid, selectedProject).then((docData) => {
  153.     console.log("Project data:", docData);
  154.     if (docData && docData.patentUrls) {
  155.       alert("Patents in this project:\n" + docData.patentUrls.join("\n"));
  156.     } else {
  157.       alert("No data for this project or it doesn't exist.");
  158.     }
  159.   });
  160. });
  161.  
  162. // 4) Save Patent to Firestore
  163. async function savePatentToProject(uid, projectName, patentUrl) {
  164.   try {
  165.     // We'll store each project in "users/{uid}/projects/{projectName}"
  166.     const projectRef = db.collection("users").doc(uid)
  167.       .collection("projects").doc(projectName);
  168.  
  169.     // Transaction or simpler approach:
  170.     await db.runTransaction(async (transaction) => {
  171.       const docSnap = await transaction.get(projectRef);
  172.       let newData = { name: projectName, updatedAt: Date.now() };
  173.  
  174.       if (!docSnap.exists) {
  175.         // Create a new doc
  176.         newData.patentUrls = [patentUrl];
  177.       } else {
  178.         // Update existing
  179.         const existingData = docSnap.data();
  180.         const existingUrls = existingData.patentUrls || [];
  181.         if (!existingUrls.includes(patentUrl)) {
  182.           existingUrls.push(patentUrl);
  183.         }
  184.         newData.patentUrls = existingUrls;
  185.       }
  186.       transaction.set(projectRef, newData, { merge: true });
  187.     });
  188.  
  189.     alert(`Saved ${patentUrl} to project "${projectName}"`);
  190.     // Refresh the dropdown if new project was created
  191.     populateProjectDropdown(uid);
  192.   } catch (err) {
  193.     console.error("Error saving patent:", err);
  194.     alert("Failed to save patent: " + err.message);
  195.   }
  196. }
  197.  
  198. // 5) Populate the Project Dropdown
  199. async function populateProjectDropdown(uid) {
  200.   try {
  201.     // Clear existing options
  202.     projectDropdown.innerHTML = '<option value="">--Select Project--</option>';
  203.  
  204.     const snapshot = await db.collection("users").doc(uid)
  205.       .collection("projects").get();
  206.     snapshot.forEach((doc) => {
  207.       const option = document.createElement("option");
  208.       option.value = doc.id;           // doc.id = projectName
  209.       option.textContent = doc.id;     // or doc.data().name
  210.       projectDropdown.appendChild(option);
  211.     });
  212.   } catch (err) {
  213.     console.error("Error populating projects:", err);
  214.   }
  215. }
  216.  
  217. // 6) Load Project
  218. async function loadProject(uid, projectName) {
  219.   try {
  220.     const docRef = db.collection("users").doc(uid)
  221.       .collection("projects").doc(projectName);
  222.     const docSnap = await docRef.get();
  223.     if (docSnap.exists) {
  224.       return docSnap.data();
  225.     } else {
  226.       return null;
  227.     }
  228.   } catch (err) {
  229.     console.error("Error loading project:", err);
  230.     return null;
  231.   }
  232. }
  233. console.log("Firebase apps:", firebase.apps);
  234. console.log("Firestore instance:", db);
  235.  
  236.  
  237. // toggle thru highlight sections
  238. const highlightRandomBtn = document.getElementById("highlightRandomBtn");
  239. const prevHighlightBtn = document.getElementById("prevHighlightBtn");
  240. const nextHighlightBtn = document.getElementById("nextHighlightBtn");
  241. const addNoteBtn = document.getElementById("noteBtn");
  242.  
  243. // Initially hide buttons
  244. document.getElementById("highlightRandomBtn").classList.add("hidden");
  245. document.getElementById("prevHighlightBtn").classList.add("hidden");
  246. document.getElementById("nextHighlightBtn").classList.add("hidden");
  247. document.getElementById("noteBtn").classList.add("hidden")
  248.  
  249. // Function to handle project selection
  250. loadProjectBtn.addEventListener("click", () => {
  251.   const selectedProject = projectDropdown.value;
  252.   if (selectedProject) {
  253.     console.log("Project loaded:", selectedProject);
  254.     // Show the buttons when a project is loaded
  255.     highlightRandomBtn.classList.remove("hidden");
  256.     prevHighlightBtn.classList.remove("hidden");
  257.     nextHighlightBtn.classList.remove("hidden");
  258.     addNoteBtn.classList.remove("hidden");
  259.   } else {
  260.     console.log("No project selected");
  261.     // Hide the buttons if no project is selected
  262.     highlightRandomBtn.classList.add("hidden");
  263.     prevHighlightBtn.classList.add("hidden");
  264.     nextHighlightBtn.classList.add("hidden");
  265.     addNoteBtn.classList.add("hidden");
  266.   }
  267. });
  268.  
  269. projectDropdown.addEventListener("change", () => {
  270.   const selectedProject = projectDropdown.value;
  271.  
  272.   if (!selectedProject) {
  273.     console.log("No project selected. Hiding buttons.");
  274.     highlightRandomBtn.classList.add("hidden");
  275.     prevHighlightBtn.classList.add("hidden");
  276.     nextHighlightBtn.classList.add("hidden");
  277.   }
  278. });
  279.  
  280. // // Highlight 15 random sentences
  281. // highlightRandomBtn.addEventListener("click", () => {
  282. //   // 1) Get the active tab
  283. //   chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
  284. //     const tabId = tabs[0].id;
  285.  
  286. //     // 2) First inject the content script (which defines window.patentHighlighter)
  287. //     chrome.scripting.executeScript(
  288. //       {
  289. //         target: { tabId },
  290. //         files: ["content-script.js"],
  291. //       },
  292. //       () => {
  293. //         // 3) Now that the script is injected,
  294. //         //    we can call the function it defines
  295. //         chrome.scripting.executeScript({
  296. //           target: { tabId },
  297. //           function: () => {
  298. //             // This runs in the page context, after content-script.js is loaded
  299. //             if (window.patentHighlighter && window.patentHighlighter.highlightRandomSentences) {
  300. //               window.patentHighlighter.highlightRandomSentences(15);
  301. //             } else {
  302. //               console.error("patentHighlighter not defined yet");
  303. //             }
  304. //           },
  305. //         });
  306. //       }
  307. //     );
  308. //   });
  309. // });
  310.  
  311. // Highlighting 5 things in the passage:
  312. document.getElementById('highlightRandomBtn').addEventListener('click', () => {
  313.   const paragraph = document.querySelector('.fitEntirePage p');
  314.  
  315.   if (!paragraph) return;
  316.  
  317.   // Clear previous highlights
  318.   paragraph.innerHTML = paragraph.textContent;
  319.  
  320.   // Keywords to highlight
  321.   const keywords = [
  322.     "AI-driven summarization",
  323.     "trend analysis",
  324.     "transformer-based AI model",
  325.     "USPTO patent",
  326.     "citation networks"
  327.   ];
  328.  
  329.   let text = paragraph.innerHTML;
  330.  
  331.   // Reset state
  332.   highlightedElements = [];
  333.   currentHighlightIndex = -1;
  334.  
  335.   // Highlight up to 5 keywords
  336.   for (let i = 0; i < keywords.length; i++) {
  337.     const word = keywords[i];
  338.     const regex = new RegExp(`(${word})`, 'i');
  339.     text = text.replace(regex, '<span class="highlight">$1</span>');
  340.   }
  341.  
  342.   paragraph.innerHTML = text;
  343.  
  344.   // Re-collect highlights
  345.   highlightedElements = Array.from(paragraph.querySelectorAll('.highlight'));
  346.  
  347.   if (highlightedElements.length > 0) {
  348.     currentHighlightIndex = 0;
  349.     scrollToHighlight(currentHighlightIndex);
  350.   }
  351. });
  352.  
  353. nextHighlightBtn.addEventListener("click", () => {
  354.   if (highlightedElements.length === 0) return;
  355.  
  356.   currentHighlightIndex = (currentHighlightIndex + 1) % highlightedElements.length;
  357.   scrollToHighlight(currentHighlightIndex);
  358. });
  359.  
  360. currentHighlightIndex = (currentHighlightIndex - 1 + highlightedElements.length) % highlightedElements.length;
  361.  
  362. addNoteBtn.addEventListener("click", () => {
  363.   document.getElementById("notePopup").style.display = "flex";
  364. });
  365.  
  366. document.querySelector("#notePopup .cancel").addEventListener("click", () => {
  367.   document.getElementById("notePopup").style.display = "none";
  368. });
  369.  
  370.  
  371. // Helper to scroll and style the current highlight
  372. function scrollToHighlight(index) {
  373.   if (highlightedElements.length === 0) return;
  374.  
  375.   highlightedElements.forEach((el, i) => {
  376.     el.style.backgroundColor = i === index ? '#ffc107' : 'yellow';
  377.   });
  378.  
  379.   highlightedElements[index].scrollIntoView({ behavior: 'smooth', block: 'center' });
  380. }
  381.  
  382. nextHighlightBtn.addEventListener("click", () => {
  383.   if (highlightedElements.length === 0) return;
  384.  
  385.   currentHighlightIndex = (currentHighlightIndex + 1) % highlightedElements.length;
  386.   scrollToHighlight(currentHighlightIndex);
  387. });
  388.  
  389.  
  390.  
  391. // Navagate between highlights
  392. nextHighlightBtn.addEventListener("click", () => {
  393.   console.log("Next Highlight button clicked (popup console)");
  394.   chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
  395.     chrome.scripting.executeScript({
  396.       target: { tabId: tabs[0].id },
  397.       function: () => {
  398.         console.log("Calling window.patentHighlighter.nextHighlight()");
  399.         if (window.patentHighlighter?.nextHighlight) {
  400.           window.patentHighlighter.nextHighlight();
  401.         }
  402.       },
  403.     });
  404.   });
  405. });
  406.  
  407. prevHighlightBtn.addEventListener("click", () => {
  408.   console.log("Previous Highlight button clicked (popup console)");
  409.   chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
  410.     chrome.scripting.executeScript({
  411.       target: { tabId: tabs[0].id },
  412.       function: () => {
  413.         console.log("Calling window.patentHighlighter.previousHighlight()");
  414.         if (window.patentHighlighter?.previousHighlight) {
  415.           window.patentHighlighter.previousHighlight();
  416.         }
  417.       },
  418.     });
  419.   });
  420. });
  421.  
  422. document.querySelectorAll('.icon-button img').forEach((icon) => {
  423.   const defaultSrc = icon.src; // Original image source
  424.   const hoverSrc = defaultSrc.replace('.png', '-hover.png'); // Assuming hover images follow the "-hover.png" naming convention
  425.  
  426.   // Change to hover image on mouseover
  427.   icon.addEventListener('mouseover', () => {
  428.     icon.src = hoverSrc;
  429.   });
  430.  
  431.   // Revert to default image on mouseout
  432.   icon.addEventListener('mouseout', () => {
  433.     icon.src = defaultSrc;
  434.   });
  435. });
  436.  
  437. // Adjusting the position of the add comment button
  438.  
  439. document.getElementById("noteBtn").addEventListener("click", () => {
  440.   const popup = document.getElementById("notePopup");
  441.   const button = document.getElementById("noteBtn");
  442.  
  443.   // Toggle visibility
  444.   if (popup.style.display === "block") {
  445.     popup.style.display = "none";
  446.   } else {
  447.     // Position and show
  448.     const rect = button.getBoundingClientRect();
  449.     popup.style.position = "fixed";
  450.     popup.style.top = `${rect.bottom + 8}px`;
  451.     popup.style.left = `${rect.left}px`;
  452.     popup.style.display = "block";
  453.   }
  454. });
  455.  
  456. // Cancel button closes popup
  457. document.querySelector("#notePopup .cancel").addEventListener("click", () => {
  458.   document.getElementById("notePopup").style.display = "none";
  459. });
  460.  
  461. const textarea = document.getElementById('commentInput');
  462.  
  463. textarea.addEventListener('input', () => {
  464.   textarea.style.height = 'auto'; // Reset height
  465.   textarea.style.height = textarea.scrollHeight + 'px'; // Expand to fit content
  466. });
  467.  
  468.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement