-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
66 lines (57 loc) · 1.67 KB
/
main.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
const makeMenus = (dlist) => {
const nodes = {};
var rootId;
if(dlist.nodes){
dlist.nodes.map(obj => nodes[obj.id] = obj);
chrome.contextMenus.removeAll();
rootId = createMenuItem( nodes, 'root', undefined );
chrome.contextMenus.create({
type: "separator",
parentId: rootId
});
}
chrome.contextMenus.create({
title: "Reload Settings and Doc",
id: 'dynapaste::reload',
parentId: rootId,
onclick: () => checkLoadWait(),
contexts: ['all']
});
};
const pasteFrom = (nodeId, nodes, evt, tab) => {
document.oncopy = function(event) {
event.preventDefault();
if(nodes[nodeId].note && nodes[nodeId].note !== "")
event.clipboardData.setData("text/plain", nodes[nodeId].note);
else
event.clipboardData.setData("text/plain", nodes[nodeId].content);
};
document.execCommand("copy", false, null);
};
const createMenuItem = (nodes, nodeId, parentId) => {
if(!nodes[nodeId].content)
return;
var menuId = chrome.contextMenus.create({
title: nodes[nodeId].content,
id: 'dynapaste::' + nodes[nodeId].id,
parentId: parentId,
onclick: (evt, tab) => pasteFrom(nodeId, nodes, evt),
contexts: ['all']
});
if(nodes[nodeId].children)
nodes[nodeId].children.map(childId => createMenuItem(nodes, childId, menuId) )
return menuId;
};
function checkLoadWait(){
chrome.storage.sync.get(['devkey', 'sourcedoc'], loadUp);
}
checkLoadWait();
function loadUp(options){
fetch("https://dynalist.io/api/v1/doc/read", {
method: "POST",
body: JSON.stringify({
"token": options.devkey,
"file_id": options.sourcedoc
})
}).then(response => response.json()).then(makeMenus);
}