@@ -150,7 +150,16 @@ function findMedia(data, isRegex = false, filter = false) {
150
150
SetIcon ( { tips : false } ) ;
151
151
}
152
152
}
153
- chrome . runtime . sendMessage ( info ) ;
153
+ // 发送到popup 并检查自动下载
154
+ chrome . runtime . sendMessage ( info , function ( ) {
155
+ if ( G . Options . AutoDownTabId . includes ( G . tabId ) ) {
156
+ let downFileName = G . Options . TitleName ? info . title + '.' + info . ext : info . name ;
157
+ chrome . downloads . download ( {
158
+ url : data . url ,
159
+ filename : "CatCatch-" + G . tabId + "/" + downFileName
160
+ } ) ;
161
+ }
162
+ } ) ;
154
163
} ) ;
155
164
}
156
165
@@ -168,25 +177,32 @@ chrome.runtime.onMessage.addListener(function (Message, sender, sendResponse) {
168
177
sendResponse ( "OK" ) ;
169
178
return ;
170
179
}
171
- if ( Message . Message == "getRulesTabId" ) {
172
- sendResponse ( G . MobileTabId ) ;
173
- return ;
174
- }
175
- if ( Message . Message == "OnMobileUserAgent" ) {
176
- OnMobileUserAgent ( Message . tabId ) ;
177
- chrome . tabs . reload ( Message . tabId ) ;
178
- sendResponse ( "OK" ) ;
179
- return ;
180
- }
181
- if ( Message . Message == "OffMobileUserAgent" ) {
182
- OffMobileUserAgent ( Message . tabId ) ;
183
- chrome . tabs . reload ( Message . tabId ) ;
184
- sendResponse ( "OK" ) ;
185
- return ;
186
- }
187
- if ( Message . Message == "HeartBeat" ) {
188
- console . log ( "HeartBeat OK" ) ;
189
- sendResponse ( "HeartBeat OK" ) ;
180
+ switch ( Message . Message ) {
181
+ case "getRulesTabId" :
182
+ sendResponse ( G . Options . MobileTabId ) ;
183
+ break ;
184
+ case "OnMobileUserAgent" :
185
+ OnMobileUserAgent ( Message . tabId ) ;
186
+ chrome . tabs . reload ( Message . tabId ) ;
187
+ sendResponse ( "OK" ) ;
188
+ break ;
189
+ case "OffMobileUserAgent" :
190
+ OffMobileUserAgent ( Message . tabId ) ;
191
+ chrome . tabs . reload ( Message . tabId ) ;
192
+ sendResponse ( "OK" ) ;
193
+ break ;
194
+ case "getAutoDownTabId" :
195
+ sendResponse ( G . Options . AutoDownTabId ) ;
196
+ break ;
197
+ case "OnAutoDown" :
198
+ OnAutoDown ( Message . tabId ) ; sendResponse ( "OK" ) ;
199
+ break ;
200
+ case "OffAutoDown" :
201
+ sendResponse ( OffAutoDown ( Message . tabId ) ) ;
202
+ break ;
203
+ case "HeartBeat" :
204
+ sendResponse ( "HeartBeat OK" ) ; console . log ( "HeartBeat OK" ) ;
205
+ break ;
190
206
}
191
207
sendResponse ( "Error" ) ;
192
208
} ) ;
@@ -202,7 +218,7 @@ chrome.tabs.onActivated.addListener(function (activeInfo) {
202
218
}
203
219
} ) ;
204
220
} ) ;
205
- //标签更新,清除该标签的记录
221
+ // 标签更新 清除数据
206
222
chrome . tabs . onUpdated . addListener ( function ( tabId , changeInfo ) {
207
223
if ( changeInfo . status == "loading" ) {
208
224
chrome . storage . local . get ( { MediaData : { } } , function ( items ) {
@@ -212,13 +228,14 @@ chrome.tabs.onUpdated.addListener(function (tabId, changeInfo) {
212
228
} ) ;
213
229
}
214
230
} ) ;
215
- //标签关闭,清除该标签的记录
231
+ // 标签关闭 清除数据
216
232
chrome . tabs . onRemoved . addListener ( function ( tabId ) {
217
233
chrome . storage . local . get ( { MediaData : { } } , function ( items ) {
218
234
delete items . MediaData [ "tabId" + tabId ] ;
219
235
chrome . storage . local . set ( { MediaData : items . MediaData } ) ;
220
236
} ) ;
221
237
OffMobileUserAgent ( tabId ) ;
238
+ OffAutoDown ( tabId ) ;
222
239
} ) ;
223
240
224
241
//检查扩展名以及大小限制
@@ -312,8 +329,10 @@ function SetIcon(obj) {
312
329
chrome . action . setTitle ( { title : "抓到 " + obj . number + " 条鱼" , tabId : obj . tabId } ) ;
313
330
}
314
331
}
332
+ // 手机端模拟
315
333
function OnMobileUserAgent ( tabId ) {
316
- G . MobileTabId . push ( tabId ) ;
334
+ G . Options . MobileTabId . push ( tabId ) ;
335
+ chrome . storage . sync . set ( { MobileTabId : G . Options . MobileTabId } ) ;
317
336
chrome . declarativeNetRequest . updateSessionRules ( {
318
337
removeRuleIds : [ tabId ] ,
319
338
addRules : [ {
@@ -327,18 +346,34 @@ function OnMobileUserAgent(tabId) {
327
346
} ]
328
347
} ,
329
348
"condition" : {
330
- "tabIds" : G . MobileTabId ,
349
+ "tabIds" : G . Options . MobileTabId ,
331
350
"resourceTypes" : [ "main_frame" , "sub_frame" , "stylesheet" , "script" , "image" , "font" , "object" , "xmlhttprequest" , "ping" , "csp_report" , "media" , "websocket" , "webtransport" , "webbundle" , "other" ]
332
351
}
333
352
} ]
334
353
} ) ;
335
354
}
336
355
function OffMobileUserAgent ( tabId ) {
337
- const index = G . MobileTabId . indexOf ( tabId ) ;
356
+ const index = G . Options . MobileTabId . indexOf ( tabId ) ;
338
357
if ( index > - 1 ) {
339
- G . MobileTabId . splice ( index , 1 ) ;
358
+ G . Options . MobileTabId . splice ( index , 1 ) ;
340
359
}
341
360
chrome . declarativeNetRequest . updateSessionRules ( {
342
361
removeRuleIds : [ tabId ]
343
362
} ) ;
363
+ chrome . storage . sync . set ( { MobileTabId : G . Options . MobileTabId } ) ;
364
+ }
365
+ // 自动下载
366
+ function OnAutoDown ( tabId ) {
367
+ G . Options . AutoDownTabId . push ( tabId ) ;
368
+ chrome . storage . sync . set ( { AutoDownTabId : G . Options . AutoDownTabId } ) ;
369
+ }
370
+ function OffAutoDown ( tabId ) {
371
+ if ( G . Options . AutoDownTabId . length == 0 ) { return false ; }
372
+ const index = G . Options . AutoDownTabId . indexOf ( tabId ) ;
373
+ if ( index > - 1 ) {
374
+ G . Options . AutoDownTabId . splice ( index , 1 ) ;
375
+ chrome . storage . sync . set ( { AutoDownTabId : G . Options . AutoDownTabId } ) ;
376
+ return true ;
377
+ }
378
+ return false ;
344
379
}
0 commit comments