Skip to content

Commit 738afc3

Browse files
committed
✨feat: auto download
1 parent 34e1c18 commit 738afc3

File tree

6 files changed

+131
-50
lines changed

6 files changed

+131
-50
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
[Changed] Heart Beat
66

77
[Added] 手机端模拟,手机环境下有更多资源可以被下载。
8+
9+
[Added] 自动下载
810
### 1.0.24
911
[Added] 导入/导出配置
1012

css/popup.css

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ body {
3939
.url {
4040
padding: 5px;
4141
}
42-
.ico,.icon {
42+
.ico,
43+
.icon {
4344
padding-left: 2px;
4445
padding-right: 2px;
4546
transition: all 0.1s;
@@ -62,7 +63,8 @@ body {
6263
float: right;
6364
font-weight: bold;
6465
}
65-
#Tips {
66+
#Tips,
67+
#TipsFixed {
6668
left: 0;
6769
right: 0;
6870
text-align: center;
@@ -76,6 +78,10 @@ body {
7678
padding: 0 10px;
7779
margin-bottom: 1px;
7880
}
81+
#TipsFixed {
82+
position: fixed;
83+
display: none;
84+
}
7985
#CloseBtn {
8086
background: #fff;
8187
border: solid 1px #c7c7c7;
@@ -97,21 +103,32 @@ body {
97103
#player video {
98104
max-height: 300px;
99105
}
100-
.button {
106+
.button,
107+
.button2 {
101108
background: #fff;
102109
border: solid 1px #c7c7c7;
103110
border-radius: 4px;
104111
color: rgb(26, 115, 232);
105112
cursor: pointer;
106-
padding: 3px 5px 3px 5px;
107-
margin: 2px 5px 4px 5px;
113+
padding: 3px 3px 3px 3px;
114+
margin: 2px 2px 4px 2px;
108115
}
109116
.button:hover {
110117
background: #f5f5f5;
111118
}
112119
.button:active {
113120
background: #ddd;
114121
}
122+
.button2 {
123+
background: rgb(26 115 232 / 80%);
124+
color: rgb(255 255 255);
125+
}
126+
.button2:hover {
127+
background: rgb(26 115 232);
128+
}
129+
.button2:active {
130+
background: rgb(26 115 232 / 80%);
131+
}
115132
.Tabs {
116133
display: flex;
117134
}

js/background.js

Lines changed: 61 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,16 @@ function findMedia(data, isRegex = false, filter = false) {
150150
SetIcon({ tips: false });
151151
}
152152
}
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+
});
154163
});
155164
}
156165

@@ -168,25 +177,32 @@ chrome.runtime.onMessage.addListener(function (Message, sender, sendResponse) {
168177
sendResponse("OK");
169178
return;
170179
}
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;
190206
}
191207
sendResponse("Error");
192208
});
@@ -202,7 +218,7 @@ chrome.tabs.onActivated.addListener(function (activeInfo) {
202218
}
203219
});
204220
});
205-
//标签更新,清除该标签的记录
221+
// 标签更新 清除数据
206222
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo) {
207223
if (changeInfo.status == "loading") {
208224
chrome.storage.local.get({ MediaData: {} }, function (items) {
@@ -212,13 +228,14 @@ chrome.tabs.onUpdated.addListener(function (tabId, changeInfo) {
212228
});
213229
}
214230
});
215-
//标签关闭,清除该标签的记录
231+
// 标签关闭 清除数据
216232
chrome.tabs.onRemoved.addListener(function (tabId) {
217233
chrome.storage.local.get({ MediaData: {} }, function (items) {
218234
delete items.MediaData["tabId" + tabId];
219235
chrome.storage.local.set({ MediaData: items.MediaData });
220236
});
221237
OffMobileUserAgent(tabId);
238+
OffAutoDown(tabId);
222239
});
223240

224241
//检查扩展名以及大小限制
@@ -312,8 +329,10 @@ function SetIcon(obj) {
312329
chrome.action.setTitle({ title: "抓到 " + obj.number + " 条鱼", tabId: obj.tabId });
313330
}
314331
}
332+
// 手机端模拟
315333
function OnMobileUserAgent(tabId) {
316-
G.MobileTabId.push(tabId);
334+
G.Options.MobileTabId.push(tabId);
335+
chrome.storage.sync.set({ MobileTabId: G.Options.MobileTabId });
317336
chrome.declarativeNetRequest.updateSessionRules({
318337
removeRuleIds: [tabId],
319338
addRules: [{
@@ -327,18 +346,34 @@ function OnMobileUserAgent(tabId) {
327346
}]
328347
},
329348
"condition": {
330-
"tabIds": G.MobileTabId,
349+
"tabIds": G.Options.MobileTabId,
331350
"resourceTypes": ["main_frame", "sub_frame", "stylesheet", "script", "image", "font", "object", "xmlhttprequest", "ping", "csp_report", "media", "websocket", "webtransport", "webbundle", "other"]
332351
}
333352
}]
334353
});
335354
}
336355
function OffMobileUserAgent(tabId) {
337-
const index = G.MobileTabId.indexOf(tabId);
356+
const index = G.Options.MobileTabId.indexOf(tabId);
338357
if (index > -1) {
339-
G.MobileTabId.splice(index, 1);
358+
G.Options.MobileTabId.splice(index, 1);
340359
}
341360
chrome.declarativeNetRequest.updateSessionRules({
342361
removeRuleIds: [tabId]
343362
});
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;
344379
}

js/init.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,19 @@ chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
1010
//设置参数
1111
G.Options = new Object();
1212
//所有设置变量
13-
G.OptionLists = ["Ext", "Debug", "TitleName", "OtherAutoClear", "Potplayer", "Type", "Regex", "ShowWebIco", "MobileUserAgent"];
14-
G.MobileTabId = [];
13+
G.OptionLists = [
14+
"Ext",
15+
"Debug",
16+
"TitleName",
17+
"OtherAutoClear",
18+
"Potplayer",
19+
"Type",
20+
"Regex",
21+
"ShowWebIco",
22+
"MobileUserAgent",
23+
"MobileTabId",
24+
"AutoDownTabId"
25+
];
1526

1627
// Init
1728
InitOptions();
@@ -56,8 +67,7 @@ function GetDefault(Obj) {
5667
{ "type": "image/*", "size": 0, "state": false }
5768
);
5869
const defaultRegex = new Array(
59-
{ "type": "ig", "regex": "video\\.weibocdn\\.com.*\\.mp4", "state": false },
60-
{ "type": "ig", "regex": "btrace\\.video\.qq\\.com.*vurl=([^&]*)", "state": true }
70+
{ "type": "ig", "regex": ".*vurl=([^&]*)", "state": true }
6171
);
6272
switch (Obj) {
6373
case "Ext": return defaultExt;
@@ -70,6 +80,8 @@ function GetDefault(Obj) {
7080
case "ShowWebIco": return false;
7181
case "MobileUserAgent":
7282
return "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1";
83+
case "MobileTabId": return [];
84+
case "AutoDownTabId": return [];
7385
}
7486
}
7587
//初始变量

js/popup.js

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,7 @@ function AddMedia(data) {
4343
}
4444

4545
//添加下载文件名
46-
let downFileName = data.name;
47-
if (G.Options.TitleName) {
48-
downFileName = data.title + '.' + data.ext;
49-
}
46+
let downFileName = G.Options.TitleName ? data.title + '.' + data.ext : data.name;
5047

5148
// 文件大小单位转换
5249
if (data.size) {
@@ -192,7 +189,7 @@ $(function () {
192189
});
193190
//下载选中文件
194191
$('#DownFile').click(function () {
195-
let FileNum = $('.TabShow :checked').size();
192+
let FileNum = $('.TabShow :checked').length;
196193
if (FileNum >= 10 && !confirm("共 " + FileNum + "个文件,是否确认下载?")) {
197194
return;
198195
}
@@ -202,7 +199,7 @@ $(function () {
202199
});
203200
//复制选中文件
204201
$('#AllCopy').click(function () {
205-
let count = $('.TabShow :checked').size();
202+
let count = $('.TabShow :checked').length;
206203
if (count == 0) { return false };
207204
let url = '';
208205
$('.TabShow :checked').each(function () {
@@ -263,6 +260,27 @@ $(function () {
263260
$('#Clear').click();
264261
});
265262
});
263+
264+
// 自动下载
265+
chrome.runtime.sendMessage({ Message: "OffAutoDown", tabId: G.tabId }, function(result){
266+
if(result){
267+
Tips("已关闭自动下载", 500);
268+
}
269+
});
270+
$("#AutoDown").click(function () {
271+
let switchStatus = $(this).data("switch");
272+
if (switchStatus == "OnAutoDown") {
273+
if (confirm("找到资源立刻尝试下载\n开启后,点击扩展图标会关闭自动下载\n是否确认开启?")) {
274+
chrome.runtime.sendMessage({ Message: switchStatus, tabId: G.tabId });
275+
$("#AutoDown").html("关闭下载");
276+
$("#AutoDown").data("switch", "OffAutoDown");
277+
}
278+
} else {
279+
chrome.runtime.sendMessage({ Message: switchStatus, tabId: G.tabId });
280+
$("#AutoDown").html("自动下载");
281+
$("#AutoDown").data("switch", "OnAutoDown");
282+
}
283+
});
266284
});
267285

268286
//html5播放器允许格式
@@ -290,11 +308,6 @@ function UItoggle() {
290308
$("#otherQuantity").text("[" + length + "]");
291309
}
292310

293-
function Tips(text) {
294-
let Original = $('#Tips').html();
295-
$('#Tips').css("position", "fixed");
296-
$('#Tips').html(text).fadeIn(500).delay(200).fadeOut(500, function () {
297-
$(this).css("position", "static");
298-
$('#Tips').html(Original);
299-
});
311+
function Tips(text, delay = 200) {
312+
$('#TipsFixed').html(text).fadeIn(500).delay(delay).fadeOut(500);
300313
}

popup.html

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
</div>
2020
</div>
2121
<div id="Tips">还没闻到味儿~</div>
22+
<div id="TipsFixed">~</div>
2223
<div id="mediaList" class="mediaList TabShow hide"></div>
2324
<div id="otherMediaList" class="mediaList hide"></div>
2425
<div id="down">
@@ -27,8 +28,9 @@
2728
<div id="AllSelect" class="button">全选</div>
2829
<div id="ReSelect" class="button">反选</div>
2930
<div id="Clear" class="button">清空</div>
30-
<div id="MobileUserAgent" class="button" data-switch="OnMobileUserAgent">模拟手机端</div>
31-
<div id="Options" class="button">设置</div>
31+
<div id="MobileUserAgent" class="button2" data-switch="OnMobileUserAgent">模拟手机端</div>
32+
<div id="AutoDown" class="button2" data-switch="OnAutoDown">自动下载</div>
33+
<div id="Options" class="button2">设置</div>
3234
</div>
3335
<div id="player" class="hide">
3436
<video controls width="100%"></video>

0 commit comments

Comments
 (0)