-
Notifications
You must be signed in to change notification settings - Fork 1
/
menu.js
72 lines (64 loc) · 1.85 KB
/
menu.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
/* global chrome */
var text;
function fetchNotes(cb) {
chrome.storage.local.get(['epoch', 'notes'], (localData) => {
chrome.storage.sync.get(['epoch', 'notes'], (syncData) => {
return cb((localData.epoch || 0) > (syncData.epoch || 0) ? localData : syncData);
});
});
}
chrome.contextMenus.removeAll(() => {
chrome.contextMenus.create({
id: 'assignmentTrackerPage',
contexts: ['page'],
title: "Link current page to Assignment Tracker#"
})
chrome.contextMenus.create({
id: 'assignmentTrackerLink',
contexts: ['link'],
title: "Add link to Assignment Tracker#"
})
chrome.contextMenus.create({
id: 'assignmentTrackerSelectionText',
contexts: ['selection'],
title: "Add '%s' as text"
})
chrome.contextMenus.create({
id: 'assignmentTrackerSelectionLink',
contexts: ['selection'],
title: "Add '%s' as link"
})
});
chrome.contextMenus.onClicked.addListener((info, tab) => {
fetchNotes((data) => {
const notes = JSON.parse(data.notes);
switch (info.menuItemId) {
case 'assignmentTrackerPage':
{
notes.ops.push({ insert: `\n\n${tab.title}`, attributes: { link: info.pageUrl } });
}
break;
case 'assignmentTrackerLink':
{
notes.ops.push({ insert: `\n\n${info.linkUrl}`, attributes: { link: info.linkUrl } });
}
break;
case 'assignmentTrackerSelectionText':
{
const text = info.selectionText;
notes.ops.push({ insert: `\n\n${text}` });
}
break;
case 'assignmentTrackerSelectionLink':
{
const text = info.selectionText;
notes.ops.push({ insert: `\n\n${text}`, attributes: { link: info.pageUrl } });
}
break;
}
chrome.storage.sync.set({
'epoch': data.epoch + 1,
'notes': JSON.stringify(notes)
});
});
});