-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
69 lines (59 loc) · 1.76 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
'use strict';
(function() {
const tabStorage = {};
const networkFilters = {
urls: [
"<all_urls>"
]
};
chrome.webRequest.onBeforeRequest.addListener((details) => {
const {
tabId,
requestId
} = details;
if (!tabStorage.hasOwnProperty(tabId)) {
return;
}
tabStorage[tabId].requests[requestId] = {
requestId: requestId,
url: details.url,
startTime: details.timeStamp,
status: 'pending'
};
var url = details.url;
console.log(url);
console.log(tabStorage[tabId].requests[requestId]);
var urlRegex = /(?:[^/][\d\w\.]+)$(?<=\.\w{3,4})/;
var fileTypeRegex = /\.[0-9a-z]+$/;
if(url.match(urlRegex) != null){
var fileName = url.match(urlRegex)[0];
console.log(fileName);
var typeName = (fileName.match(fileTypeRegex)[0]).substring(1, );;
console.log(typeName);
chrome.downloads.download({
url: url,
filename: typeName + "/" + fileName // Optional
});
console.log("cancelling");
} else {
console.log("url null");
}
}, networkFilters, ["blocking"]);
chrome.tabs.onActivated.addListener((tab) => {
const tabId = tab ? tab.tabId : chrome.tabs.TAB_ID_NONE;
if (!tabStorage.hasOwnProperty(tabId)) {
tabStorage[tabId] = {
id: tabId,
requests: {},
registerTime: new Date().getTime()
};
}
});
chrome.tabs.onRemoved.addListener((tab) => {
const tabId = tab.tabId;
if (!tabStorage.hasOwnProperty(tabId)) {
return;
}
tabStorage[tabId] = null;
});
}());