-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackground.js
More file actions
62 lines (46 loc) · 1.52 KB
/
background.js
File metadata and controls
62 lines (46 loc) · 1.52 KB
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
chrome.contextMenus.create({
id: 'clip-off-menu',
title: 'WebClip this',
contexts: ['selection']
});
async function getCurrentTabval() {
let queryOptions = { active: true, lastFocusedWindow: true };
let [tab] = await chrome.tabs.query(queryOptions);
console.log("Tab taken: ",tab.url);
return tab.url;
}
let current_bookmark = [];
const fetchBookmarks = async () => {
const currentBlog = await getCurrentTabval();
return new Promise((resolve)=>{
chrome.storage.sync.get([currentBlog],(obj)=>{
resolve(obj[currentBlog]? JSON.parse(obj[currentBlog]):[]);
})
})
}
chrome.tabs.onActivated.addListener( async () =>{
current_bookmark = await fetchBookmarks()
}
)
chrome.contextMenus.onClicked.addListener(
({selectionText}) => {
addBookMarkEventHandler(selectionText);
})
const addBookMarkEventHandler = async (selectionText) =>{
const currentBlog = await getCurrentTabval();
const newBookmark = {
text: selectionText,
desc: selectionText.split('.')[0].split(' ').slice(0,3).join(' '),
iddel: selectionText.split('.')[0].split(' ').slice(0,3).join('-'),
url: currentBlog,
valf: "true"
}
console.log("BookmarKs :",newBookmark);
current_bookmark = await fetchBookmarks();
console.log("current bookmark: ",current_bookmark)
chrome.storage.sync.set({
[currentBlog]:JSON.stringify([...current_bookmark,newBookmark])
})
current_bookmark = await fetchBookmarks();
console.log("current bookmark: ",current_bookmark)
}