-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
37 lines (32 loc) · 918 Bytes
/
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
const urlMap = {
bing: "https://www.bing.com/search?q=",
duckduckgo: "https://duckduckgo.com/?q=",
google: "https://google.com/search?q=",
yahoo: "https://search.yahoo.com/search?q="
};
const searchForText = (selectedText) => {
chrome.storage.sync.get(["searchEngine"], (result) => {
let url;
if (result.searchEngine) {
url = `${urlMap[result.searchEngine]}${encodeURIComponent(selectedText)}`;
} else {
url = `${urlMap["google"]}${encodeURIComponent(selectedText)}`;
}
chrome.tabs.create({
url,
});
});
};
chrome.runtime.onInstalled.addListener(() => {
chrome.contextMenus.create({
id: "search-text-selection",
title: `Search for "%s"`,
contexts: ["selection"],
});
chrome.contextMenus.onClicked.addListener((info) => {
const { menuItemId, selectionText } = info;
if (menuItemId === "search-text-selection" && selectionText) {
searchForText(selectionText);
}
});
});