1
1
import { Snapshot , addToArchiveBox } from "./utils.js" ;
2
2
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
-
11
3
// Checks if URL should be auto-archived based on regex patterns and configuration settings.
12
4
async function shouldAutoArchive ( url ) {
13
5
try {
@@ -106,24 +98,28 @@ async function autoArchive(tabId, changeInfo, tab) {
106
98
// enabled and disabled
107
99
let tabUpdateListener = null ;
108
100
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 ( ) {
110
105
console . debug ( '[Auto-Archive Debug] Setting up auto-archiving...' ) ;
111
106
112
107
const hasPermission = await chrome . permissions . contains ( { permissions : [ 'tabs' ] } ) ;
113
108
console . debug ( `[Auto-Archive Debug] Has tabs permission: ${ hasPermission } ` ) ;
114
109
115
110
if ( ! hasPermission ) {
116
111
console . log ( 'Tabs permission not granted, auto-archiving disabled' ) ;
117
- // Reset the toggle if permission was not granted
118
112
chrome . storage . local . set ( { enable_auto_archive : false } ) ;
119
113
return ;
120
114
}
121
115
116
+ // To prevent extra listeners being registered every time the auto-archiving checkbox is toggled,
117
+ // we remove any existing listener first.
122
118
if ( tabUpdateListener ) {
123
119
try {
124
120
chrome . tabs . onUpdated . removeListener ( tabUpdateListener ) ;
125
121
tabUpdateListener = null ;
126
- } catch ( error ) {
122
+ } catch {
127
123
// ignore
128
124
}
129
125
}
@@ -132,51 +128,7 @@ async function setupAutoArchiving() {
132
128
console . debug ( `[Auto-Archive Debug] enable_auto_archive setting: ${ enable_auto_archive } ` ) ;
133
129
134
130
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 ;
180
132
console . debug ( '[Auto-Archive Debug] Adding tab update listener' ) ;
181
133
chrome . tabs . onUpdated . addListener ( tabUpdateListener ) ;
182
134
console . log ( 'Auto-archiving enabled with tabs permission' ) ;
@@ -186,35 +138,15 @@ async function setupAutoArchiving() {
186
138
}
187
139
188
140
// Initialize auto-archiving setup on extension load
189
- setupAutoArchiving ( ) ;
141
+ configureAutoArchiving ( ) ;
190
142
191
143
// Listen for changes to the auto-archive setting
192
144
chrome . storage . onChanged . addListener ( ( changes , area ) => {
193
145
if ( area === 'local' && changes . enable_auto_archive ) {
194
- setupAutoArchiving ( ) ;
146
+ configureAutoArchiving ( ) ;
195
147
}
196
148
} ) ;
197
149
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
-
218
150
chrome . runtime . onMessage . addListener ( ( message , sender , sendResponse ) => {
219
151
if ( message . type === 'archivebox_add' ) {
220
152
try {
@@ -237,18 +169,34 @@ chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
237
169
return true ;
238
170
} ) ;
239
171
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
+ } ) ;
240
179
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
241
189
chrome . contextMenus . onClicked . addListener ( ( item , tab ) =>
242
190
chrome . scripting . executeScript ( {
243
191
target : { tabId : tab . id } ,
244
192
files : [ 'popup.js' ]
245
193
} )
246
194
) ;
247
195
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' ]
253
201
} ) ;
254
202
} ) ;
0 commit comments