Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let highlightedElements = [];
- let currentHighlightIndex = -1;
- // popup-signedin.js
- const firebaseConfig = {
- apiKey: "AIzaSyA7wge6iWbXVH2bOnH0VLx2ssY5MPGliwo",
- authDomain: "patent-plus-chrome-extension.firebaseapp.com",
- projectId: "patent-plus-chrome-extension",
- storageBucket: "patent-plus-chrome-extension.firebasestorage.app",
- messagingSenderId: "95801979992",
- appId: "1:95801979992:web:462955f495f2b262f8462d",
- measurementId: "G-CGPDJEFHFE"
- };
- firebase.initializeApp(firebaseConfig);
- const auth = firebase.auth();
- // We must reuse the same Firebase config or just rely on the existing init.
- const statusDiv = document.getElementById("status");
- const highlightBtn = document.getElementById("highlightBtn");
- const removeHighlightBtn = document.getElementById("removeHighlightBtn");
- const signOutBtn = document.getElementById("signOutBtn");
- document.getElementById('toggle-buttons').addEventListener('click', () => {
- chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
- chrome.tabs.sendMessage(tabs[0].id, { action: 'toggleFloatingButtons' });
- });
- });
- auth.onAuthStateChanged((user) => {
- if (user) {
- const statusDiv = document.getElementById("status");
- statusDiv.textContent = `Logged in as: ${user.email}`;
- } else {
- // Redirect back to sign-in page if the user is signed out
- window.location.href = "popup.html";
- }
- });
- signOutBtn.addEventListener("click", () => {
- auth.signOut()
- .then(() => {
- // Redirect to sign-in page after sign out
- window.location.href = "popup.html";
- })
- .catch((error) => {
- console.error("Error signing out:", error.message);
- });
- });
- // Check auth state
- auth.onAuthStateChanged((user) => {
- if (user) {
- statusDiv.textContent = "Logged in as: " + user.email;
- } else {
- // If no user is logged in, go back to sign in page
- window.location.href = "popup.html";
- }
- });
- // Call for toolkit to be enabled
- /**
- * Helper function to call highlight or remove functions
- * in the content script. We'll rely on window.patentHighlighter
- * in contentScript.js
- *
- * @param {string} method - "highlightAll" or "removeAllHighlights"
- */
- function injectScript(method) {
- // Get current active tab
- chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
- const tabId = tabs[0].id;
- // Use chrome.scripting.executeScript with an inline function
- // that calls window.patentHighlighter[method].
- chrome.scripting.executeScript({
- target: { tabId },
- function: (methodName) => {
- if (window.patentHighlighter && window.patentHighlighter[methodName]) {
- window.patentHighlighter[methodName]();
- } else {
- console.log("No patentHighlighter or method not found:", methodName);
- }
- },
- args: [method]
- });
- });
- }
- // popup-signedin.js
- // Sign Out
- signOutBtn.addEventListener("click", () => {
- auth.signOut()
- .then(() => {
- // After sign out, return to sign in page
- window.location.href = "popup.html";
- });
- });
- // 1) Initialize Firestore
- const db = firebase.firestore();
- const saveProjectBtn = document.getElementById("saveProjectBtn");
- const projectNameInput = document.getElementById("projectName");
- const projectDropdown = document.getElementById("projectDropdown");
- const loadProjectBtn = document.getElementById("loadProjectBtn");
- let currentUser = null;
- auth.onAuthStateChanged(async (user) => {
- if (user) {
- currentUser = user;
- statusDiv.textContent = "Logged in as: " + user.email;
- // Populate the dropdown with existing projects
- populateProjectDropdown(user.uid);
- } else {
- // If no user, go back to sign in
- window.location.href = "popup.html";
- }
- });
- // 2) Save Current Patent to Project
- saveProjectBtn.addEventListener("click", () => {
- const projectName = projectNameInput.value.trim();
- if (!projectName) {
- alert("Please enter a project name.");
- return;
- }
- // Get the active tab's patent URL
- chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
- const patentUrl = tabs[0].url; // e.g., https://patents.google.com/patent/US12345...
- savePatentToProject(currentUser.uid, projectName, patentUrl);
- });
- });
- // 3) Load Project
- loadProjectBtn.addEventListener("click", () => {
- const selectedProject = projectDropdown.value;
- if (!selectedProject) {
- alert("Select a project first.");
- return;
- }
- // For example, open a new window with the project's patents,
- // or show them in the popup, etc. We'll just console.log for now:
- loadProject(currentUser.uid, selectedProject).then((docData) => {
- console.log("Project data:", docData);
- if (docData && docData.patentUrls) {
- alert("Patents in this project:\n" + docData.patentUrls.join("\n"));
- } else {
- alert("No data for this project or it doesn't exist.");
- }
- });
- });
- // 4) Save Patent to Firestore
- async function savePatentToProject(uid, projectName, patentUrl) {
- try {
- // We'll store each project in "users/{uid}/projects/{projectName}"
- const projectRef = db.collection("users").doc(uid)
- .collection("projects").doc(projectName);
- // Transaction or simpler approach:
- await db.runTransaction(async (transaction) => {
- const docSnap = await transaction.get(projectRef);
- let newData = { name: projectName, updatedAt: Date.now() };
- if (!docSnap.exists) {
- // Create a new doc
- newData.patentUrls = [patentUrl];
- } else {
- // Update existing
- const existingData = docSnap.data();
- const existingUrls = existingData.patentUrls || [];
- if (!existingUrls.includes(patentUrl)) {
- existingUrls.push(patentUrl);
- }
- newData.patentUrls = existingUrls;
- }
- transaction.set(projectRef, newData, { merge: true });
- });
- alert(`Saved ${patentUrl} to project "${projectName}"`);
- // Refresh the dropdown if new project was created
- populateProjectDropdown(uid);
- } catch (err) {
- console.error("Error saving patent:", err);
- alert("Failed to save patent: " + err.message);
- }
- }
- // 5) Populate the Project Dropdown
- async function populateProjectDropdown(uid) {
- try {
- // Clear existing options
- projectDropdown.innerHTML = '<option value="">--Select Project--</option>';
- const snapshot = await db.collection("users").doc(uid)
- .collection("projects").get();
- snapshot.forEach((doc) => {
- const option = document.createElement("option");
- option.value = doc.id; // doc.id = projectName
- option.textContent = doc.id; // or doc.data().name
- projectDropdown.appendChild(option);
- });
- } catch (err) {
- console.error("Error populating projects:", err);
- }
- }
- // 6) Load Project
- async function loadProject(uid, projectName) {
- try {
- const docRef = db.collection("users").doc(uid)
- .collection("projects").doc(projectName);
- const docSnap = await docRef.get();
- if (docSnap.exists) {
- return docSnap.data();
- } else {
- return null;
- }
- } catch (err) {
- console.error("Error loading project:", err);
- return null;
- }
- }
- console.log("Firebase apps:", firebase.apps);
- console.log("Firestore instance:", db);
- // toggle thru highlight sections
- const highlightRandomBtn = document.getElementById("highlightRandomBtn");
- const prevHighlightBtn = document.getElementById("prevHighlightBtn");
- const nextHighlightBtn = document.getElementById("nextHighlightBtn");
- const addNoteBtn = document.getElementById("noteBtn");
- // Initially hide buttons
- document.getElementById("highlightRandomBtn").classList.add("hidden");
- document.getElementById("prevHighlightBtn").classList.add("hidden");
- document.getElementById("nextHighlightBtn").classList.add("hidden");
- document.getElementById("noteBtn").classList.add("hidden")
- // Function to handle project selection
- loadProjectBtn.addEventListener("click", () => {
- const selectedProject = projectDropdown.value;
- if (selectedProject) {
- console.log("Project loaded:", selectedProject);
- // Show the buttons when a project is loaded
- highlightRandomBtn.classList.remove("hidden");
- prevHighlightBtn.classList.remove("hidden");
- nextHighlightBtn.classList.remove("hidden");
- addNoteBtn.classList.remove("hidden");
- } else {
- console.log("No project selected");
- // Hide the buttons if no project is selected
- highlightRandomBtn.classList.add("hidden");
- prevHighlightBtn.classList.add("hidden");
- nextHighlightBtn.classList.add("hidden");
- addNoteBtn.classList.add("hidden");
- }
- });
- projectDropdown.addEventListener("change", () => {
- const selectedProject = projectDropdown.value;
- if (!selectedProject) {
- console.log("No project selected. Hiding buttons.");
- highlightRandomBtn.classList.add("hidden");
- prevHighlightBtn.classList.add("hidden");
- nextHighlightBtn.classList.add("hidden");
- }
- });
- // // Highlight 15 random sentences
- // highlightRandomBtn.addEventListener("click", () => {
- // // 1) Get the active tab
- // chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
- // const tabId = tabs[0].id;
- // // 2) First inject the content script (which defines window.patentHighlighter)
- // chrome.scripting.executeScript(
- // {
- // target: { tabId },
- // files: ["content-script.js"],
- // },
- // () => {
- // // 3) Now that the script is injected,
- // // we can call the function it defines
- // chrome.scripting.executeScript({
- // target: { tabId },
- // function: () => {
- // // This runs in the page context, after content-script.js is loaded
- // if (window.patentHighlighter && window.patentHighlighter.highlightRandomSentences) {
- // window.patentHighlighter.highlightRandomSentences(15);
- // } else {
- // console.error("patentHighlighter not defined yet");
- // }
- // },
- // });
- // }
- // );
- // });
- // });
- // Highlighting 5 things in the passage:
- document.getElementById('highlightRandomBtn').addEventListener('click', () => {
- const paragraph = document.querySelector('.fitEntirePage p');
- if (!paragraph) return;
- // Clear previous highlights
- paragraph.innerHTML = paragraph.textContent;
- // Keywords to highlight
- const keywords = [
- "AI-driven summarization",
- "trend analysis",
- "transformer-based AI model",
- "USPTO patent",
- "citation networks"
- ];
- let text = paragraph.innerHTML;
- // Reset state
- highlightedElements = [];
- currentHighlightIndex = -1;
- // Highlight up to 5 keywords
- for (let i = 0; i < keywords.length; i++) {
- const word = keywords[i];
- const regex = new RegExp(`(${word})`, 'i');
- text = text.replace(regex, '<span class="highlight">$1</span>');
- }
- paragraph.innerHTML = text;
- // Re-collect highlights
- highlightedElements = Array.from(paragraph.querySelectorAll('.highlight'));
- if (highlightedElements.length > 0) {
- currentHighlightIndex = 0;
- scrollToHighlight(currentHighlightIndex);
- }
- });
- nextHighlightBtn.addEventListener("click", () => {
- if (highlightedElements.length === 0) return;
- currentHighlightIndex = (currentHighlightIndex + 1) % highlightedElements.length;
- scrollToHighlight(currentHighlightIndex);
- });
- currentHighlightIndex = (currentHighlightIndex - 1 + highlightedElements.length) % highlightedElements.length;
- addNoteBtn.addEventListener("click", () => {
- document.getElementById("notePopup").style.display = "flex";
- });
- document.querySelector("#notePopup .cancel").addEventListener("click", () => {
- document.getElementById("notePopup").style.display = "none";
- });
- // Helper to scroll and style the current highlight
- function scrollToHighlight(index) {
- if (highlightedElements.length === 0) return;
- highlightedElements.forEach((el, i) => {
- el.style.backgroundColor = i === index ? '#ffc107' : 'yellow';
- });
- highlightedElements[index].scrollIntoView({ behavior: 'smooth', block: 'center' });
- }
- nextHighlightBtn.addEventListener("click", () => {
- if (highlightedElements.length === 0) return;
- currentHighlightIndex = (currentHighlightIndex + 1) % highlightedElements.length;
- scrollToHighlight(currentHighlightIndex);
- });
- // Navagate between highlights
- nextHighlightBtn.addEventListener("click", () => {
- console.log("Next Highlight button clicked (popup console)");
- chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
- chrome.scripting.executeScript({
- target: { tabId: tabs[0].id },
- function: () => {
- console.log("Calling window.patentHighlighter.nextHighlight()");
- if (window.patentHighlighter?.nextHighlight) {
- window.patentHighlighter.nextHighlight();
- }
- },
- });
- });
- });
- prevHighlightBtn.addEventListener("click", () => {
- console.log("Previous Highlight button clicked (popup console)");
- chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
- chrome.scripting.executeScript({
- target: { tabId: tabs[0].id },
- function: () => {
- console.log("Calling window.patentHighlighter.previousHighlight()");
- if (window.patentHighlighter?.previousHighlight) {
- window.patentHighlighter.previousHighlight();
- }
- },
- });
- });
- });
- document.querySelectorAll('.icon-button img').forEach((icon) => {
- const defaultSrc = icon.src; // Original image source
- const hoverSrc = defaultSrc.replace('.png', '-hover.png'); // Assuming hover images follow the "-hover.png" naming convention
- // Change to hover image on mouseover
- icon.addEventListener('mouseover', () => {
- icon.src = hoverSrc;
- });
- // Revert to default image on mouseout
- icon.addEventListener('mouseout', () => {
- icon.src = defaultSrc;
- });
- });
- // Adjusting the position of the add comment button
- document.getElementById("noteBtn").addEventListener("click", () => {
- const popup = document.getElementById("notePopup");
- const button = document.getElementById("noteBtn");
- // Toggle visibility
- if (popup.style.display === "block") {
- popup.style.display = "none";
- } else {
- // Position and show
- const rect = button.getBoundingClientRect();
- popup.style.position = "fixed";
- popup.style.top = `${rect.bottom + 8}px`;
- popup.style.left = `${rect.left}px`;
- popup.style.display = "block";
- }
- });
- // Cancel button closes popup
- document.querySelector("#notePopup .cancel").addEventListener("click", () => {
- document.getElementById("notePopup").style.display = "none";
- });
- const textarea = document.getElementById('commentInput');
- textarea.addEventListener('input', () => {
- textarea.style.height = 'auto'; // Reset height
- textarea.style.height = textarea.scrollHeight + 'px'; // Expand to fit content
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement