Repository navigation
Expand file tree
/
Copy pathbackground.js
More file actions
47 lines (43 loc) · 1.53 KB
/
Copy pathbackground.js
File metadata and controls
47 lines (43 loc) · 1.53 KB
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
// background.js
// Export bookmarks logic as a function for testing
async function exportBookmarks() {
try {
const bookmarks = await new Promise((resolve, reject) => {
try {
chrome.bookmarks.getTree(resolve);
} catch (err) {
reject(err);
}
});
const json = JSON.stringify(bookmarks, null, 2);
const dataUrl = 'data:application/json,' + encodeURIComponent(json);
return await new Promise((resolve) => {
chrome.downloads.download({
url: dataUrl,
filename: 'bookmarks.json',
saveAs: true
}, (downloadId) => {
if (chrome.runtime.lastError) {
console.error('Download failed:', chrome.runtime.lastError.message);
resolve({ status: chrome.runtime.lastError.message, json });
} else {
console.log('Download started, ID:', downloadId);
resolve({ status: 'success', json });
}
});
});
} catch (e) {
console.error('Failed to export bookmarks:', e);
return { status: 'error', json: null };
}
}
// Listen for extension button click
if (typeof chrome !== 'undefined' && chrome.action && chrome.action.onClicked) {
chrome.action.onClicked.addListener(async (tab) => {
await exportBookmarks();
});
}
// Export for testing
if (typeof module !== 'undefined') {
module.exports = { exportBookmarks };
}