+22
-22
src/background/index.ts
+22
-22
src/background/index.ts
···
1
1
import { MessageRequest } from "../utils"
2
2
import { WebsiteStore } from "../storage/db"
3
3
4
-
function storeWebsite(tab: chrome.tabs.Tab, db: WebsiteStore, sendResponse: any): Promise<void> {
5
-
const requestOptions = {
6
-
method: 'GET',
7
-
redirect: 'follow'
8
-
};
9
-
4
+
// this is meant to be called async
5
+
function storeWebsites(tabs: chrome.tabs.Tab[], db: WebsiteStore, sendResponse: any): Promise<void[]> {
6
+
// FIXME: delegate this to db
10
7
// FIXME: add some guarantees that this won't randomly crash
11
-
return fetch(`https://cardyb.bsky.app/v1/extract?url=${encodeURIComponent(tab.url)}`,
12
-
requestOptions)
13
-
.then(response => response.json())
8
+
const promiseArray = tabs.map(tab => fetch(`https://cardyb.bsky.app/v1/extract?url=${encodeURIComponent(tab.url)}`,
9
+
{
10
+
method: 'GET',
11
+
redirect: 'follow'
12
+
})
13
+
.then(response => { const res = response.json(); console.log(res); return res })
14
14
.then(result => {
15
15
return {
16
16
url: tab.url,
···
22
22
};
23
23
})
24
24
.then(website => {
25
-
db.store(website)
25
+
db.store([website])
26
26
.then(res => sendResponse(res))
27
27
.catch(err => {
28
28
console.log(err)
···
30
30
});
31
31
})
32
32
.catch(error => {
33
-
console.log('error', error)
34
-
// FIXME: temp
35
-
db.store({
33
+
// just use info at hand if OG information cannot be retrieved
34
+
db.store([{
36
35
url: tab.url,
37
36
name: tab.title,
38
37
faviconUrl: tab.favIconUrl,
39
38
savedAt: Date.now(),
40
39
openGraphImageUrl: null,
41
40
description: null,
42
-
})
41
+
}])
43
42
.then(res => sendResponse(res))
44
43
.catch(err => {
45
44
console.log(err)
46
45
sendResponse(err)
47
46
});
48
-
});
47
+
}));
48
+
49
+
return Promise.all(promiseArray);
49
50
}
50
51
51
52
chrome.runtime.onInstalled.addListener(async () => {
···
67
68
switch (request.type) {
68
69
case MessageRequest.SAVE_TAB:
69
70
chrome.tabs.query({ active: true, lastFocusedWindow: true },
70
-
([tab]) => storeWebsite(tab, db, sendResponse));
71
+
(tabs) => storeWebsites(tabs, db, sendResponse));
71
72
break;
72
73
case MessageRequest.GET_ALL_ITEMS:
73
74
db.getAllWebsites()
···
174
175
case MessageRequest.SAVE_WINDOW_TO_NEW_PROJECT:
175
176
chrome.tabs.query({ windowId: sender.tab.windowId })
176
177
.then(tabs => {
177
-
let websites: string[] = [];
178
-
for (const tab of tabs) {
179
-
// store website async
180
-
storeWebsite(tab, db, sendResponse);
181
-
websites.push(tab.url);
182
-
}
178
+
let websites: string[] = tabs.map(tab => tab.url);
179
+
// store websites async
180
+
storeWebsites(tabs, db, sendResponse);
181
+
183
182
if (!("newProjectName" in request)) {
184
183
sendResponse({
185
184
error: "projectName required"
186
185
});
187
186
}
187
+
// FIXME: remove websites to see if that fixes double website store
188
188
db.createNewActiveProject(request.newProjectName, websites)
189
189
.then((res) => sendResponse(res))
190
190
.catch(err => {