-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackground.js
39 lines (37 loc) · 1.25 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
chrome.runtime.onInstalled.addListener(() => {
chrome.contextMenus.create({
id: "glean",
title: "Glean",
contexts: ["selection"]
});
});
chrome.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId === "glean" && info.selectionText) {
const gleaning = {
text: info.selectionText.trim(),
url: tab.url,
title: tab.title,
timestamp: new Date().toISOString()
};
chrome.storage.sync.get('gleaned', ({ gleaned = [] }) => {
const updatedGleaned = [...gleaned, gleaning];
chrome.storage.sync.set({ gleaned: updatedGleaned }, () => {
if (chrome.runtime.lastError) {
console.error('Save failed:', chrome.runtime.lastError);
} else {
console.log('Gleaning saved:', gleaning);
}
});
});
}
});
chrome.omnibox.onInputEntered.addListener((text) => {
chrome.storage.sync.get('gleaned', ({ gleaned = [] }) => {
const matches = gleaned.filter(g => g.text.toLowerCase().includes(text.toLowerCase()));
if (matches.length > 0) {
chrome.tabs.create({ url: matches[0].url });
} else {
chrome.tabs.update({ url: `https://www.google.com/search?q=${encodeURIComponent(text)}` });
}
});
});