-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
33 lines (29 loc) · 876 Bytes
/
background.js
File metadata and controls
33 lines (29 loc) · 876 Bytes
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
// add context menu on select
function setupContextMenu(){
chrome.contextMenus.create({
id: "addnote",
title: "Add to Harbor",
contexts: ["selection"]
})
}
chrome.runtime.onInstalled.addListener(() => { setupContextMenu() })
// handle context menu click
chrome.contextMenus.onClicked.addListener((info, tab) => {
console.log(info, tab);
if (info.menuItemId === "addnote") {
console.log(`Adding the note "${info.selectionText}"`);
// open sidepanel to ensure connection is met for event listener
chrome.sidePanel.open({ tabId: tab.id })
// ensure content scripts have time to activate
setTimeout(() => {
chrome.runtime.sendMessage({
content: info.selectionText,
url: info.pageUrl
});
}, 500);
}
})
// open panel onclick
chrome.sidePanel
.setPanelBehavior({ openPanelOnActionClick: true })
.catch((error) => console.error(error))