-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommon.js
122 lines (108 loc) · 3.13 KB
/
common.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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
export const DEFAULT_SERVER = "https://rsstodolist.eu/";
export const DEFAULT_FEED = "somename";
const prefs = {
feed: DEFAULT_FEED,
server: DEFAULT_SERVER,
more: false
}
const addEndingSlash = (s = '') => {
if (s.length !== 0 && !s.endsWith('/')) return `${s}/`
return s
}
export const save = (feed, server, more) => {
prefs.feed = feed || DEFAULT_FEED
prefs.server = addEndingSlash(server) || DEFAULT_SERVER
prefs.more = Boolean(more)
console.log('save', prefs)
chrome.storage.local.set({ prefs })
}
export const load = (cb) => {
const onLoad = (data) => {
prefs.feed = data?.prefs?.feed ?? DEFAULT_FEED
prefs.server = data?.prefs?.server ?? DEFAULT_SERVER
prefs.more = data?.prefs?.more ?? false
console.log('load', prefs)
cb(prefs)
}
chrome.storage.local.get('prefs', onLoad)
}
export const getPrefsFromStorage = () => prefs
export const cleanUrl = (url) => url.replace(/&?utm_.+?(&|$)/g, '').replace(/(\?)$/, '')
export const send = (server, add, feed, url, title, description) => {
if (!server || !feed || !url) {
const message = 'No server, feed or url'
notify(false, message)
return Promise.reject(new Error(message))
}
var url = [
server,
(add ? "add" : "del"),
"?name=",
encodeURIComponent(feed),
"&url=",
encodeURIComponent(cleanUrl(url))
]
if (title) {
url.push(
"&title=",
encodeURIComponent(title)
)
}
if (description) {
url.push(
"&description=",
encodeURIComponent(description)
)
}
console.log('send', url.join(''))
return fetch(url.join(''))
.then(() => { notify(true, `Feed ${feed} updated`) })
.catch(e => {
console.error(e)
notify(false, `Error when updating ${feed}`)
})
}
export const fetchCount = (server, name) => {
const url = `${server}count?n=${name}`
console.log(`fetchCount - ${url}`)
return fetch(url)
.then(response => response.json())
.then(json => formatNumber(json?.count || 0))
.catch(e => {
console.error(e)
return 'N/A'
})
}
export const fetchSuggest = (server, query) => {
const url = `${server}suggest?query=${query}`
console.log(`fetchSuggest - ${url}`)
return fetch(url)
.then(response => response.json())
.catch(e => {
console.error(e)
})
}
export const notify = (success, message) => {
chrome.notifications.create("rsstodolist-notification", {
type: "basic",
title: "rsstodolist : " + (success ? "success" : "error"),
iconUrl: success ? "imgs/ok.png" : "imgs/error.png",
message
});
}
export const throttle = delay => fn => {
let timeout
return e => {
clearTimeout(timeout)
timeout = setTimeout(() => fn(e), delay)
}
}
const MILLION = 1_000_000
const THOUSAND = 1_000
export const formatNumber = nb => {
if (Number.isInteger(nb)) {
if (nb >= MILLION) return `${(nb / MILLION).toFixed(1)}M`
if (nb >= THOUSAND) return `${(nb / THOUSAND).toFixed(1)}K`
}
return nb
}