From c894dd37e05818c743ab2ae4b45b68f3bdcba11a Mon Sep 17 00:00:00 2001 From: Graham V Date: Sun, 20 Oct 2024 10:00:00 -0400 Subject: [PATCH] Added alt-w shortcut to search Perplexity for the current page --- background.js | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++- manifest.json | 12 +++++++++-- 2 files changed, 65 insertions(+), 3 deletions(-) diff --git a/background.js b/background.js index 3191bf9..95c2488 100644 --- a/background.js +++ b/background.js @@ -25,8 +25,15 @@ chrome.action.onClicked.addListener(() => { }); chrome.commands.onCommand.addListener((command) => { + console.log("Command received:", command); // Add this line if (command === "toggle-search") { toggleSearch(); + } else if (command === "search-page-content") { + console.log("Extracting page content..."); // Add this line + extractPageContent((content) => { + console.log("Content extracted:", content.substring(0, 50) + "..."); // Add this line + performSearch(content); + }); } }); @@ -53,4 +60,51 @@ function performSearch(query) { chrome.tabs.update({ url: url }); } }); -} \ No newline at end of file +} + +function extractPageContent(callback) { + chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) { + if (tabs.length > 0) { + chrome.scripting.executeScript({ + target: { tabId: tabs[0].id }, + function: () => { + // Get the page title + const pageTitle = document.title; + + // Remove script and style elements + const elementsToRemove = document.querySelectorAll('script, style, nav, header, footer'); + elementsToRemove.forEach(el => el.remove()); + + // Get the main content + const mainContent = document.querySelector('main') || document.body; + + // Extract and process the text + let text = mainContent.innerText; + + // Remove extra whitespace and newlines + text = text.replace(/\s+/g, ' ').trim(); + + // Limit the length (e.g., to 1000 characters) + const maxLength = 1000; + if (text.length > maxLength) { + text = text.substring(0, maxLength) + '...'; + } + + // Combine title, content, and URL + return { title: pageTitle, content: text, url: window.location.href }; + } + }, (result) => { + if (chrome.runtime.lastError) { + console.error(chrome.runtime.lastError); + callback(''); + } else { + const { title, content, url } = result[0].result; + const formattedContent = `${title}\n\n${content}\n\n${url}`; + callback(formattedContent); + } + }); + } else { + callback(''); + } + }); +} diff --git a/manifest.json b/manifest.json index 3c6264b..6a57890 100644 --- a/manifest.json +++ b/manifest.json @@ -5,6 +5,7 @@ "description": "Quickly search Perplexity with a keyboard shortcut or context menu", "permissions": [ "activeTab", + "scripting", "storage", "contextMenus" ], @@ -33,11 +34,18 @@ "suggested_key": { "default": "Alt+Q" }, - "description": "Toggle Perplexity search input" + "description": "Toggle search" + }, + "search-page-content": { + "suggested_key": { + "default": "Alt+W", + "mac": "Alt+W" + }, + "description": "Search page content" } }, "options_ui": { "page": "settings.html", "open_in_tab": true } -} \ No newline at end of file +}