-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
94 lines (65 loc) · 2.49 KB
/
script.js
File metadata and controls
94 lines (65 loc) · 2.49 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
let current_bookmarks=[];
let currentBlog="";
async function getCurrentTab() {
let queryOptions = { active: true, lastFocusedWindow: true };
let [tab] = await chrome.tabs.query(queryOptions);
return tab.url;
}
const addNewBookmark = async (bookmarksElement,bookmark) =>{
const tabval = await getCurrentTab();
if(bookmark.url === tabval && bookmark.valf === "true"){
const newBookMark = document.createElement('div');
newBookMark.className="bm";
newBookMark.id=bookmark.iddel;
const bookMarkTitle = document.createElement('div');
bookMarkTitle.textContent = bookmark.desc;
bookMarkTitle.className="book";
const controlsElement = document.createElement('button');
controlsElement.textContent ="-";
controlsElement.className="imag";
controlsElement.id="imag";
newBookMark.appendChild(bookMarkTitle);
newBookMark.appendChild(controlsElement);
bookmarksElement.appendChild(newBookMark);
controlsElement.addEventListener("click", OnDelete);
}
}
async function OnDelete(e) {
const tabval = await getCurrentTab();
const val = e.target.parentElement.id;
console.log("Clicked ",val);
const bookmarksElementDel = document.getElementById(val);
bookmarksElementDel.parentNode.removeChild(bookmarksElementDel);
// remove from chrome storage
// iddel = val
current_bookmarks = current_bookmarks.filter((b) => b.iddel != val);
console.log(current_bookmarks);
chrome.storage.sync.set({
[tabval]:JSON.stringify([...current_bookmarks])
})
}
const viewBookmarks = (current_bookmarks=[])=>{
const bookmarksElement = document.getElementById("cbms");
bookmarksElement.innerHTML="";
// if bookmark present
if(current_bookmarks.length > 0){
for(let i=0;i<current_bookmarks.length;i++){
const bookmark = current_bookmarks[i];
console.log("bookmark - ",bookmark);
addNewBookmark(bookmarksElement,bookmark);
}
}
else{
bookmarksElement.innerHTML ="<i>There are no bookmarks for this page </i>";
}
return;
}
document.addEventListener("DOMContentLoaded", async () =>{
currentBlog = await getCurrentTab();
console.log("Current: ",currentBlog);
chrome.storage.sync.get([currentBlog],(data)=>{
current_bookmarks = data[currentBlog] ? JSON.parse(data[currentBlog]):[];
// view bookmark
viewBookmarks(current_bookmarks);
})
})