-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathutils.js
116 lines (100 loc) · 3.42 KB
/
utils.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
// Common utility functions
export function filterEntries(entries, filterText) {
if (!filterText) return entries;
const searchTerms = filterText.toLowerCase().split(' ');
return entries.filter(entry => {
const searchableText = [
entry.url,
entry.title,
entry.id,
new Date(entry.timestamp).toISOString(),
...entry.tags
].join(' ').toLowerCase();
return searchTerms.every(term => searchableText.includes(term));
});
}
export async function addToArchiveBox(addCommandArgs) {
try {
const { archivebox_server_url, archivebox_api_key } = await new Promise((resolve, reject) => {
const vals = chrome.storage.local.get([
'archivebox_server_url',
'archivebox_api_key'
]);
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError);
} else {
resolve(vals);
}
});
if (!archivebox_server_url) {
throw new Error('Server not configured.');
}
let response = undefined;
// try ArchiveBox v0.8.0+ API endpoint first
if (archivebox_api_key) {
response = await fetch(`${archivebox_server_url}/api/v1/cli/add`, {
headers: {
'x-archivebox-api-key': `${archivebox_api_key}`
},
method: 'post',
credentials: 'include',
body: addCommandArgs
});
}
// fall back to pre-v0.8.0 endpoint for backwards compatibility
if (response === undefined || response.status === 404) {
const body = new FormData();
const urls = addCommandArgs && addCommandArgs.urls ? addCommandArgs.urls.join("\n") : "";
const tags = addCommandArgs && addCommandArgs.tags ? addCommandArgs.tags : "";
body.append("url", urls);
body.append("tag", tags);
response = await fetch(`${archivebox_server_url}/add/`, {
method: "post",
credentials: "include",
body: body
});
}
if (!response.ok) {
throw new Error(`Request failed with status ${response.status}`);
}
return {ok: response.ok, status: response.status, statusText: response.statusText};
} catch (e) {
return {ok: false, errorMessage: e.message};
}
}
export function downloadCsv(entries) {
const headers = ['id', 'timestamp', 'url', 'title', 'tags', 'notes'];
const csvRows = [
headers.join(','),
...entries.map(entry => {
return [
entry.id,
entry.timestamp,
`"${entry.url}"`,
`"${entry.title || ''}"`,
`"${entry.tags.join(';')}"`,
`"${entry.notes || ''}"`
].join(',');
})
];
const csvContent = csvRows.join('\n');
const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.setAttribute('href', url);
link.setAttribute('download', `archivebox-export-${new Date().toISOString().split('T')[0]}.csv`);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
export function downloadJson(entries) {
const jsonContent = JSON.stringify(entries, null, 2);
const blob = new Blob([jsonContent], { type: 'application/json;charset=utf-8;' });
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.setAttribute('href', url);
link.setAttribute('download', `archivebox-export-${new Date().toISOString().split('T')[0]}.json`);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}