-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbackground.js
More file actions
56 lines (50 loc) · 1.65 KB
/
background.js
File metadata and controls
56 lines (50 loc) · 1.65 KB
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
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) => {
if (command === "toggle-search") {
toggleSearch();
}
});
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 });
}
});
}