Skip to content

Commit 4e31810

Browse files
committed
Add comments and clarify auto-archiving code.
1 parent 9e1de7b commit 4e31810

File tree

2 files changed

+32
-83
lines changed

2 files changed

+32
-83
lines changed

background.js

+31-83
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
import { Snapshot, addToArchiveBox } from "./utils.js";
22

3-
chrome.runtime.onMessage.addListener(async (message) => {
4-
const options_url = chrome.runtime.getURL('options.html') + `?search=${message.id}`;
5-
console.log('i ArchiveBox Collector showing options.html', options_url);
6-
if (message.action === 'openOptionsPage') {
7-
await chrome.tabs.create({ url: options_url });
8-
}
9-
});
10-
113
// Checks if URL should be auto-archived based on regex patterns and configuration settings.
124
async function shouldAutoArchive(url) {
135
try {
@@ -106,24 +98,28 @@ async function autoArchive(tabId, changeInfo, tab) {
10698
// enabled and disabled
10799
let tabUpdateListener = null;
108100

109-
async function setupAutoArchiving() {
101+
// Checks if we should be auto-archiving, and manages the listener accordingly. If the user has
102+
// given the required permissions and enabled it through the UI, then we'll listen for tab updates
103+
// and attempt to automatically archive the desired URLs.
104+
async function configureAutoArchiving() {
110105
console.debug('[Auto-Archive Debug] Setting up auto-archiving...');
111106

112107
const hasPermission = await chrome.permissions.contains({ permissions: ['tabs'] });
113108
console.debug(`[Auto-Archive Debug] Has tabs permission: ${hasPermission}`);
114109

115110
if (!hasPermission) {
116111
console.log('Tabs permission not granted, auto-archiving disabled');
117-
// Reset the toggle if permission was not granted
118112
chrome.storage.local.set({ enable_auto_archive: false });
119113
return;
120114
}
121115

116+
// To prevent extra listeners being registered every time the auto-archiving checkbox is toggled,
117+
// we remove any existing listener first.
122118
if (tabUpdateListener) {
123119
try {
124120
chrome.tabs.onUpdated.removeListener(tabUpdateListener);
125121
tabUpdateListener = null;
126-
} catch (error) {
122+
} catch {
127123
// ignore
128124
}
129125
}
@@ -132,51 +128,7 @@ async function setupAutoArchiving() {
132128
console.debug(`[Auto-Archive Debug] enable_auto_archive setting: ${enable_auto_archive}`);
133129

134130
if (enable_auto_archive) {
135-
tabUpdateListener = async (tabId, changeInfo, tab) => {
136-
console.debug(`[Auto-Archive Debug] Tab updated - tabId: ${tabId}, status: ${changeInfo.status}, url: ${tab?.url}`);
137-
138-
// Only process when the page has completed loading
139-
if (changeInfo.status === 'complete' && tab.url) {
140-
console.debug(`[Auto-Archive Debug] Tab load complete, checking if URL should be archived: ${tab.url}`);
141-
142-
// Check if URL is already archived locally
143-
const { snapshots = [] } = await chrome.storage.local.get('snapshots');
144-
const isAlreadyArchived = snapshots.some(s => s.url === tab.url);
145-
146-
if (isAlreadyArchived) {
147-
console.debug(`[Auto-Archive Debug] URL already archived, skipping: ${tab.url}`);
148-
return;
149-
}
150-
151-
const shouldArchive = await shouldAutoArchive(tab.url);
152-
console.debug(`[Auto-Archive Debug] shouldAutoArchive result: ${shouldArchive}`);
153-
154-
if (shouldArchive) {
155-
console.log('Auto-archiving URL:', tab.url);
156-
157-
const snapshot = new Snapshot(
158-
tab.url,
159-
['auto-archived'],
160-
tab.title,
161-
tab.favIconUrl,
162-
);
163-
164-
console.debug('[Auto-Archive Debug] Created new snapshot, saving to storage');
165-
snapshots.push(snapshot);
166-
await chrome.storage.local.set({ snapshots });
167-
console.debug('[Auto-Archive Debug] Snapshot saved to local storage');
168-
169-
try {
170-
console.debug(`[Auto-Archive Debug] Calling addToArchiveBox with URL: ${snapshot.url}, tags: ${snapshot.tags.join(',')}`);
171-
await addToArchiveBox([snapshot.url], snapshot.tags);
172-
console.log(`Automatically archived ${snapshot.url}`);
173-
} catch (error) {
174-
console.error(`Failed to automatically archive ${snapshot.url}: ${error.message}`);
175-
}
176-
}
177-
}
178-
};
179-
131+
tabUpdateListener = autoArchive;
180132
console.debug('[Auto-Archive Debug] Adding tab update listener');
181133
chrome.tabs.onUpdated.addListener(tabUpdateListener);
182134
console.log('Auto-archiving enabled with tabs permission');
@@ -186,35 +138,15 @@ async function setupAutoArchiving() {
186138
}
187139

188140
// Initialize auto-archiving setup on extension load
189-
setupAutoArchiving();
141+
configureAutoArchiving();
190142

191143
// Listen for changes to the auto-archive setting
192144
chrome.storage.onChanged.addListener((changes, area) => {
193145
if (area === 'local' && changes.enable_auto_archive) {
194-
setupAutoArchiving();
146+
configureAutoArchiving();
195147
}
196148
});
197149

198-
chrome.action.onClicked.addListener(async (tab) => {
199-
const snapshot = new Snapshot(
200-
tab.url,
201-
[],
202-
tab.title,
203-
tab.favIconUrl,
204-
);
205-
206-
// Save the snapshot first
207-
const { snapshots = [] } = await chrome.storage.local.get('snapshots');
208-
snapshots.push(snapshot);
209-
await chrome.storage.local.set({ snapshots });
210-
211-
// Inject scripts - CSS now handled in popup.js
212-
await chrome.scripting.executeScript({
213-
target: { tabId: tab.id },
214-
files: ['popup.js']
215-
});
216-
});
217-
218150
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
219151
if (message.type === 'archivebox_add') {
220152
try {
@@ -237,18 +169,34 @@ chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
237169
return true;
238170
});
239171

172+
chrome.runtime.onMessage.addListener(async (message) => {
173+
const options_url = chrome.runtime.getURL('options.html') + `?search=${message.id}`;
174+
console.log('i ArchiveBox Collector showing options.html', options_url);
175+
if (message.action === 'openOptionsPage') {
176+
await chrome.tabs.create({ url: options_url });
177+
}
178+
});
240179

180+
chrome.runtime.onInstalled.addListener(function () {
181+
chrome.contextMenus.removeAll();
182+
chrome.contextMenus.create({
183+
id: 'save_to_archivebox_ctxmenu',
184+
title: 'Save to ArchiveBox',
185+
});
186+
});
187+
188+
// Context menu button
241189
chrome.contextMenus.onClicked.addListener((item, tab) =>
242190
chrome.scripting.executeScript({
243191
target: { tabId: tab.id },
244192
files: ['popup.js']
245193
})
246194
);
247195

248-
chrome.runtime.onInstalled.addListener(function () {
249-
chrome.contextMenus.removeAll();
250-
chrome.contextMenus.create({
251-
id: 'save_to_archivebox_ctxmenu',
252-
title: 'Save to ArchiveBox',
196+
// Toolbar button
197+
chrome.action.onClicked.addListener((tab) => {
198+
chrome.scripting.executeScript({
199+
target: { tabId: tab.id },
200+
files: ['popup.js']
253201
});
254202
});

utils.js

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export function updateStatusIndicator(indicator, textElement, success, message)
5151
textElement.className = success ? 'text-success' : 'text-danger';
5252
}
5353

54+
// Archive URLs on the configured ArchiveBox server instance.
5455
export async function addToArchiveBox(urls, tags = [], depth = 0, update = false, update_all = false) {
5556
const formattedTags = tags.join(',');
5657
console.log(`i Adding urls ${urls} and tags ${formattedTags} to ArchiveBox`);

0 commit comments

Comments
 (0)