forked from vaeho/Perplexity_Shortcut
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
110 lines (97 loc) · 3.93 KB
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
chrome.runtime.onInstalled.addListener((details) => {
if (details.reason === "install") {
chrome.tabs.create({ url: "onboarding.html" });
}
chrome.contextMenus.create({
id: "perplexitySearch",
title: "Search Perplexity for '%s'",
contexts: ["selection"]
});
});
function toggleSearch() {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
if (tabs.length > 0) {
chrome.tabs.sendMessage(tabs[0].id, { action: "toggleSearch" })
.catch(error => {
console.log("Unable to toggle search on this page.");
});
}
});
}
chrome.action.onClicked.addListener(() => {
chrome.runtime.openOptionsPage();
});
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);
});
}
});
chrome.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId === "perplexitySearch") {
performSearch(info.selectionText);
}
});
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "search") {
performSearch(request.query);
} else if (request.action === "openShortcutsPage") {
chrome.tabs.create({ url: "chrome://extensions/shortcuts" });
}
});
function performSearch(query) {
chrome.storage.sync.get({ openInNewTab: true }, function (items) {
const url = `https://www.perplexity.ai/search?q=${encodeURIComponent(query)}`;
if (items.openInNewTab) {
chrome.tabs.create({ url: url });
} else {
chrome.tabs.update({ url: url });
}
});
}
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('');
}
});
}