Skip to content

Commit

Permalink
ok
Browse files Browse the repository at this point in the history
  • Loading branch information
jae-jae committed May 9, 2018
1 parent bee6b25 commit f0c29e7
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 79 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,14 @@ $ gulp clean build
$ gulp serve
```

# Changelog

## 0.0.8 - 2017-05-10
### Added
- Intercept filter,blacklist and whitelist

### Changed
- Remove connect max limit

# License
MIT
2 changes: 1 addition & 1 deletion app/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "__MSG_appName__",
"version": "0.0.6",
"version": "0.0.7",
"manifest_version": 2,
"description": "__MSG_appDescription__",
"author": "Jaeger <[email protected]>",
Expand Down
167 changes: 89 additions & 78 deletions app/scripts.babel/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,46 @@ chrome.runtime.onInstalled.addListener(details => {
});

let preNum = 0;
let currentUrl = '';

var getStorage = (key) => {
if (key === 'size') {
let size = localStorage['size'] || 0
size = size * 1024 * 1024
return size
} else if(key === 'path') {
} else if (key === 'path') {
return localStorage['path'] || 'http://localhost:6800/jsonrpc'
} else if(key === 'enableFilter') {
} else if (key === 'enableFilter') {
return localStorage['enableFilter'] || 'disabled'
} else if(key === 'filterUrlRegs') {
} else if (key === 'filterUrlRegs') {
return localStorage['filterUrlRegs'] === undefined ? [] : localStorage['filterUrlRegs'].split(',').map((reg) => eval(reg))
}
return localStorage[key]
}

var notice = (message,title = 'Camtd') => {
var notice = (message, title = 'Camtd') => {
chrome.notifications.create({
type: 'basic',
iconUrl: 'images/default/icon-38.png',
title,
message
},(id) => {
}, (id) => {
setTimeout(_ => {
chrome.notifications.clear(id);
},5000);
}, 5000);
})
}

var sendAnimMsg = () => {
console.log('sending msg');
chrome.tabs.query({active: true}, function (tabs) {
tabs.forEach(function (tab) {
chrome.tabs.sendMessage(tab.id, {
console.log('sending msg');
chrome.tabs.query({ active: true }, function (tabs) {
tabs.forEach(function (tab) {
chrome.tabs.sendMessage(tab.id, {
action: 'show-add-task-anim'
});
console.log('msg send');
})
})
console.log('msg send');
})
})
}

let getGlobalStatus = (callback) => {
Expand All @@ -61,15 +62,23 @@ let testUrl = (url, regs) => {
})
}

let getTabUrl = () => {
chrome.tabs.query({ active: true }, function (tabs) {
tabs.forEach(function (tab) {
currentUrl = tab.url
})
})

}

setInterval(() => {
getGlobalStatus((data) => {
let num = data['result']['numActive']
if (num > 0) {
chrome.browserAction.setBadgeText({text: num});
chrome.browserAction.setBadgeBackgroundColor({color:'#00CC66'});
chrome.browserAction.setBadgeText({ text: num });
chrome.browserAction.setBadgeBackgroundColor({ color: '#00CC66' });
} else {
chrome.browserAction.setBadgeText({text: ''});
chrome.browserAction.setBadgeText({ text: '' });
}
if (preNum > num) {
notice('Download completed!');
Expand All @@ -78,62 +87,64 @@ setInterval(() => {
})
}, 1000)

chrome.notifications.onClicked.addListener(function(itemId){


chrome.tabs.onActivated.addListener(() => {
getTabUrl()
})

chrome.tabs.onUpdated.addListener(() => {
getTabUrl()
})

chrome.notifications.onClicked.addListener(function (itemId) {
// chrome.downloads.show(parseInt(itemId));
chrome.downloads.showDefaultFolder()
chrome.notifications.clear(itemId, function(wasCleared){} );
chrome.notifications.clear(itemId, function (wasCleared) { });
});

chrome.downloads.onDeterminingFilename.addListener(function (down) {
chrome.tabs.query({active: true}, function (tabs) {
tabs.forEach(function (tab) {
let tabUrl = tab.url
let downloadUrl = down.finalUrl
let enableFilter = getStorage('enableFilter')
let filterUrls = getStorage('filterUrlRegs')
console.log(filterUrls)
if (enableFilter === 'disabled') {
add(down)
}else if(enableFilter === 'blacklist') {
if (!testUrl(tabUrl, filterUrls) && !testUrl(downloadUrl,filterUrls)) {
add(down)
} else {
console.log('blacklist:',tabUrl,downloadUrl)
}
} else {
if (testUrl(tabUrl, filterUrls) || testUrl(downloadUrl,filterUrls)) {
add(down)
} else {
console.log('not in whitelist:',tabUrl,downloadUrl)
}
}
})
})
});

function add(down) {
if (!getStorage('path')) {
alert('Camtd has not been configured');
chrome.tabs.create({ 'url': 'options.html' }, function (s) { });
return 0;
}
if (getStorage('enabled') == 0) {
if (getStorage('enabled') == 0 || Math.abs(down.fileSize) < getStorage('size')) {
return 0;
}
if (Math.abs(down.fileSize) > getStorage('size')) {
chrome.downloads.cancel(down.id, function (s) { });
getUrlCookie(down.url, (cookies) => {
var aria2_obj = combination(down,cookies);
var ifpostback = postaria2obj(aria2_obj);
if (ifpostback == 'base64_error') {
notice('Error adding tasks to aria2!','Error')
} else {
// notice('Aria2 is starting to download files.', down.filename)
sendAnimMsg()
}
})


let downloadUrl = down.finalUrl
let enableFilter = getStorage('enableFilter')
let filterUrls = getStorage('filterUrlRegs')
if (enableFilter === 'disabled') {
add(down)
} else if (enableFilter === 'blacklist') {
if (!testUrl(currentUrl, filterUrls) && !testUrl(downloadUrl, filterUrls)) {
add(down)
} else {
console.log('blacklist:', currentUrl, downloadUrl)
}
} else {
if (testUrl(currentUrl, filterUrls) || testUrl(downloadUrl, filterUrls)) {
add(down)
} else {
console.log('not in whitelist:', currentUrl, downloadUrl)
}
}
});

function add(down) {
chrome.downloads.cancel(down.id, function (s) { });
getUrlCookie(down.url, (cookies) => {
var aria2_obj = combination(down, cookies);
var ifpostback = postaria2obj(aria2_obj);
if (ifpostback == 'base64_error') {
notice('Error adding tasks to aria2!', 'Error')
} else {
// notice('Aria2 is starting to download files.', down.filename)
sendAnimMsg()
}
})
}

function postaria2obj(addobj, callback = null) {
Expand All @@ -156,7 +167,7 @@ function postaria2obj(addobj, callback = null) {
httppost.onerror = function () {
console.log('Error aria2 configuration!');
if (addobj[0] && addobj[0].method === 'aria2.addUri') {
notice('Error adding tasks to aria2,please check the configuration!','Error');
notice('Error adding tasks to aria2,please check the configuration!', 'Error');
}
};
httppost.addEventListener('load', function () {
Expand All @@ -175,37 +186,37 @@ function parse_url(url) {
var auth_str = request_auth(url);
var auth = null;
if (auth_str) {
if (auth_str.indexOf('token:') == 0) {
auth = auth_str;
} else {
auth = 'Basic ' + btoa(auth_str);
}
if (auth_str.indexOf('token:') == 0) {
auth = auth_str;
} else {
auth = 'Basic ' + btoa(auth_str);
}
}
var url_path = remove_auth(url);
function request_auth(url) {
return url.match(/^(?:(?![^:@]+:[^:@\/]*@)[^:\/?#.]+:)?(?:\/\/)?(?:([^:@]*(?::[^:@]*)?)?@)?/)[1];
return url.match(/^(?:(?![^:@]+:[^:@\/]*@)[^:\/?#.]+:)?(?:\/\/)?(?:([^:@]*(?::[^:@]*)?)?@)?/)[1];
}
function remove_auth(url) {
return url.replace(/^((?![^:@]+:[^:@\/]*@)[^:\/?#.]+:)?(\/\/)?(?:(?:[^:@]*(?::[^:@]*)?)?@)?(.*)/, '$1$2$3');
return url.replace(/^((?![^:@]+:[^:@\/]*@)[^:\/?#.]+:)?(\/\/)?(?:(?:[^:@]*(?::[^:@]*)?)?@)?(.*)/, '$1$2$3');
}
return [url_path, auth];
}


function getUrlCookie(link, callback) {
chrome.cookies.getAll({ 'url': link }, function(cookies) {
var format_cookies = [];
for (var i in cookies) {
var cookie = cookies[i];
format_cookies.push(cookie.name + '=' + cookie.value);
}
format_cookies = format_cookies.join('; ');
callback(format_cookies)
chrome.cookies.getAll({ 'url': link }, function (cookies) {
var format_cookies = [];
for (var i in cookies) {
var cookie = cookies[i];
format_cookies.push(cookie.name + '=' + cookie.value);
}
format_cookies = format_cookies.join('; ');
callback(format_cookies)
});
}

function combination(down, cookies) {

var header = [];
header.push('Cookie: ' + cookies);
header.push('User-Agent: ' + navigator.userAgent);
Expand Down Expand Up @@ -245,10 +256,10 @@ function rightadd(info, tab) {
return 0;
}
getUrlCookie(down.finalUrl, (cookies) => {
var aria2_obj = combination(down,cookies);
var aria2_obj = combination(down, cookies);
var ifpostback = postaria2obj(aria2_obj);
if (ifpostback == 'base64_error') {
notice('Error adding tasks to aria2!','Error')
notice('Error adding tasks to aria2!', 'Error')
} else {
// notice('Aria2 is starting to download files.', down.filename)
sendAnimMsg()
Expand Down

0 comments on commit f0c29e7

Please sign in to comment.