-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackground.js
182 lines (162 loc) · 5.88 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
chrome.offscreen.createDocument({
url: chrome.runtime.getURL('offscreen.html'),
reasons: [chrome.offscreen.Reason.DOM_PARSER],
justification: 'Parse HTML content for bookmarks',
});
// Use a debounce function for bookmark creation
let debounceTimer;
chrome.bookmarks.onCreated.addListener((id, bookmark) => {
clearTimeout(debounceTimer);
debounceTimer = setTimeout(() => {
if (bookmark.url) openTab(bookmark.url);
}, 500);
});
function openTab(url) {
chrome.tabs.query({ url: url, currentWindow: true }, (tabs) => {
if (tabs.length === 0) {
chrome.tabs.create({ url: url, active: false }, (tab) => attachTabUpdateListener(tab.id, url));
} else {
attachTabUpdateListener(tabs[0].id, url);
}
});
}
function attachTabUpdateListener(tabId, url) {
function updatedTabListener(updatedTabId, changeInfo) {
if (updatedTabId === tabId && changeInfo.status === 'complete') {
chrome.tabs.onUpdated.removeListener(updatedTabListener);
captureTabContent(tabId, url);
}
}
chrome.tabs.onUpdated.addListener(updatedTabListener);
}
function getAllBookmarks() {
return new Promise((resolve) => {
chrome.bookmarks.getTree((bookmarkTreeNodes) => {
const bookmarks = [];
function traverse(node) {
if (node.url) bookmarks.push(node);
if (node.children) node.children.forEach(traverse);
}
bookmarkTreeNodes.forEach(traverse);
resolve(bookmarks);
});
});
}
async function reloadAllBookmarks() {
const allBookmarks = await getAllBookmarks();
let successCount = 0;
let failureCount = 0;
for (const bookmark of allBookmarks) {
if (bookmark.url) {
try {
await openTabAndCapture(bookmark.url);
successCount++;
} catch (error) {
console.error(`Failed to reload bookmark: ${bookmark.url}`, error);
failureCount++;
}
}
}
return { successCount, failureCount, total: allBookmarks.length };
}
function openTabAndCapture(url) {
return new Promise((resolve, reject) => {
chrome.tabs.create({ url: url, active: false }, (tab) => {
const timeout = setTimeout(() => {
chrome.tabs.remove(tab.id);
reject(new Error(`Timeout loading page: ${url}`));
}, 30000);
function listener(tabId, changeInfo) {
if (tabId === tab.id && changeInfo.status === 'complete') {
chrome.tabs.onUpdated.removeListener(listener);
clearTimeout(timeout);
captureTabContent(tab.id, url)
.then((content) => {
storeHTMLAndTitle(url, content.html, content.title);
chrome.tabs.remove(tab.id);
resolve();
})
.catch((error) => {
chrome.tabs.remove(tab.id);
reject(error);
});
}
}
chrome.tabs.onUpdated.addListener(listener);
});
});
}
function captureTabContent(tabId, url) {
chrome.tabs.sendMessage(tabId, { action: "captureHTML" }, (response) => {
if (chrome.runtime.lastError) {
console.error("Error capturing content:", chrome.runtime.lastError);
storeHTMLAndTitle(url, "Error capturing content", "Error");
return;
}
if (response && response.html) {
// Send the captured HTML to the offscreen document for parsing
chrome.runtime.sendMessage({
action: "parseHTML",
html: response.html,
url: url,
title: response.title || "No Title"
});
} else {
console.error("Invalid response from content script");
storeHTMLAndTitle(url, "Invalid response from content script", "Error");
}
});
}
function storeHTMLAndTitle(url, content, title) {
chrome.storage.local.set({
[url]: {
html: content,
title: title,
summarized: false
}
}, () => {
if (chrome.runtime.lastError) {
console.error("Error storing content for", url, ":", chrome.runtime.lastError.message);
} else {
console.log("Content and title stored successfully for", url);
}
});
}
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "reloadAllBookmarks") {
reloadAllBookmarks().then((result) => {
sendResponse({
status: "Bookmarks reloading completed",
successCount: result.successCount,
failureCount: result.failureCount,
total: result.total
});
}).catch((error) => {
sendResponse({ status: "Error reloading bookmarks", error: error.message });
});
return true;
}
});
chrome.runtime.onInstalled.addListener((details) => {
if (details.reason === "install" || details.reason === "update") {
getAllBookmarks().then((bookmarks) => {
bookmarks.forEach((bookmark) => {
if (bookmark.url) {
storeHTMLAndTitle(bookmark.url, "", bookmark.title);
}
});
});
}
});
// Listen for messages from the offscreen document
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === "parsedHTML") {
storeHTMLAndTitle(message.url, message.content, message.title);
}
});
// Add this listener to handle bookmark creation
chrome.bookmarks.onCreated.addListener((id, bookmark) => {
if (bookmark.url) {
openTab(bookmark.url);
}
});