forked from WorldBrain/Memex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bookmarks.ts
49 lines (40 loc) · 1.15 KB
/
bookmarks.ts
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
import { Bookmarks } from 'webextension-polyfill-ts'
import tabManager from '../activity-logger/background/tab-manager'
import { createPageViaBmTagActs } from './on-demand-indexing'
import { getPage } from './util'
export async function addBookmark({
url,
timestamp = Date.now(),
tabId,
}: {
url: string
timestamp?: number
tabId?: number
}) {
let page = await getPage(url)
if (page == null || page.isStub) {
page = await createPageViaBmTagActs({ url, tabId })
}
page.setBookmark(timestamp)
await page.save()
tabManager.setBookmarkState(url, true)
}
export async function delBookmark({
url,
}: Partial<Bookmarks.BookmarkTreeNode>) {
const page = await getPage(url)
if (page != null) {
page.delBookmark()
// Delete if Page left orphaned, else just save current state
if (page.shouldDelete) {
await page.delete()
} else {
await page.save()
}
tabManager.setBookmarkState(url, false)
}
}
export async function pageHasBookmark(url: string) {
const page = await getPage(url)
return page != null ? page.hasBookmark : false
}