-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
35 lines (33 loc) · 1.11 KB
/
background.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
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === 'saveBookmark') {
chrome.storage.local.get({ bookmarks: [] }, (result) => {
if (result.bookmarks.some(bookmark =>
bookmark.user === request.data.user && bookmark.repo === request.data.repo)) {
sendResponse({ already_exist: true });
return false;
}
const newBookmarks = [...result.bookmarks, request.data];
chrome.storage.local.set({ bookmarks: newBookmarks }, () => {
sendResponse({ success: true });
});
});
return true;
}
if (request.action === 'getBookmarks') {
chrome.storage.local.get({ bookmarks: [] }, (result) => {
sendResponse(result.bookmarks);
});
return true;
}
if (request.action === 'deleteBookmark') {
chrome.storage.local.get({ bookmarks: [] }, (result) => {
const filtered = result.bookmarks.filter(
bookmark => bookmark.url !== request.data.url
);
chrome.storage.local.set({ bookmarks: filtered }, () => {
sendResponse({ success: true });
});
});
return true;
}
});