-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathbackground.js
executable file
·161 lines (133 loc) · 4.43 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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// background.js
import {
addToArchiveBox,
captureScreenshot,
captureDom,
uploadToS3,
readFileFromOPFS,
} from "./utils.js";
chrome.runtime.onInstalled.addListener(function () {
chrome.contextMenus.create({
id: 'save_to_archivebox_ctxmenu',
title: 'Save to ArchiveBox',
});
});
chrome.runtime.onMessage.addListener(async (message) => {
const options_url = chrome.runtime.getURL('options.html') + `?search=${message.id}`;
console.log('i ArchiveBox Collector showing options.html', options_url);
if (message.action === 'openOptionsPage') {
await chrome.tabs.create({ url: options_url });
}
});
// Listeners for user-submitted actions
async function saveEntry(tab) {
const entry = {
id: crypto.randomUUID(),
url: tab.url,
timestamp: new Date().toISOString(),
tags: [],
title: tab.title,
favicon: tab.favIconUrl
};
// Save the entry first
const { entries = [] } = await chrome.storage.local.get('entries');
entries.push(entry);
await chrome.storage.local.set({ entries });
// Inject scripts - CSS now handled in popup.js
await chrome.scripting.executeScript({
target: { tabId: tab.id },
files: ['popup.js']
});
}
chrome.action.onClicked.addListener((tab, data) => saveEntry(tab));
chrome.contextMenus.onClicked.addListener((info, tab) => saveEntry(tab));
// Listeners for messages from other workers and contexts
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.type === 'archivebox_add') {
addToArchiveBox(message.body, sendResponse, sendResponse);
}
return true;
});
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.type === 'capture_screenshot') {
(async ()=> {
try {
const result = await captureScreenshot(message.timestamp);
if (result.ok) {
sendResponse(result);
} else {
throw new Error(result.errorMessage);
}
} catch (error) {
console.log("Failed to capture screenshot: ", error);
sendResponse({ok: false, errorMessage: String(error)});
}
})();
return true;
}
});
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.type === 'capture_dom') {
(async ()=> {
try {
const result = await captureDom(message.timestamp)
if (result.ok) {
sendResponse(result);
} else {
throw new Error(result.errorMessage);
}
} catch (error) {
console.log("Failed to capture DOM: ", error);
sendResponse({ok: false, errorMessage: String(error)});
}
})();
return true;
}
});
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.type === 'save_to_s3') {
(async ()=> {
try {
const data = await readFileFromOPFS(message.path);
if (!data) {
throw new Error('Failed to read file from OPFS');
}
const fileName = message.path.split('/').filter(part=>part.length > 0).pop();
console.log('filename: ', fileName);
const s3Url = await uploadToS3(fileName, data, message.contentType);
sendResponse({ok: true, url: s3Url});
} catch (error) {
console.log('Failed to upload to S3: ', error);
sendResponse({ok: false, errorMessage: String(error)});
}
})();
return true;
}
})
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.type === 'test_s3') {
(async () => {
// Upload test file
try {
const fileName = `.connection_test_${Date.now()}.txt`;
const randomContent = Math.random().toString(36).substring(2, 15);
const testData = new TextEncoder().encode(randomContent);
const s3Url = await uploadToS3(fileName, testData, 'text/plain');
// Verify test file matches
const response = await fetch(s3Url);
if (response.ok) {
const responseText = await response.text();
const testPassed = responseText === randomContent;
sendResponse(testPassed ? 'success' : 'failure');
} else {
console.error(`Failed to fetch test content: ${response.status} ${response,statusText}`);
sendResponse('failure');
}
} catch (error) {
console.error('S3 credential test failed:', error);
sendResponse('failure');
}
})();
return true;
}
});