-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
49 lines (40 loc) · 1.17 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
function getUrls(url, callback){
var url = url;
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4)
{
var urls = null;
if(!url.indexOf('file://') || !url.indexOf('ftp://'))
{
if(xhr.status == 0)
urls = xhr.responseText.split('\n');
}
else if(xhr.status == 200)
{
var contentType = xhr.getResponseHeader('Content-Type');
if((contentType && contentType.indexOf('application/x-url-list') != -1))
urls = xhr.responseText.split('\n');
}
callback(urls);
}
}
xhr.open("get", url);
xhr.send();
}
function downloadUrls(urls){
for(var i = 0; i < urls.length; i++)
{
var url = urls[i].trim();
if(url) chrome.downloads.download({url: url});
}
}
function modifyRequest(details){
if(details.tabId == -1) return;
getUrls(details.url, function(urls){
if (urls && urls.length != 0) downloadUrls(urls);
});
return {redirectUrl: "javascript:"};
}
chrome.webRequest.onBeforeRequest.addListener(
modifyRequest, {urls: ['*://*/*.urls?*', '*://*/*.urls', 'ftp://*/*.urls', 'file://*/*.urls']}, ['blocking']);