From f0c29e7b0fec1af29eaefec4721c37cb2607f482 Mon Sep 17 00:00:00 2001 From: Jaeger Date: Thu, 10 May 2018 00:29:38 +0800 Subject: [PATCH] ok --- README.md | 9 ++ app/manifest.json | 2 +- app/scripts.babel/background.js | 167 +++++++++++++++++--------------- 3 files changed, 99 insertions(+), 79 deletions(-) diff --git a/README.md b/README.md index f6c6ac5..20c062b 100644 --- a/README.md +++ b/README.md @@ -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 \ No newline at end of file diff --git a/app/manifest.json b/app/manifest.json index 5f5ebef..b9f1afc 100644 --- a/app/manifest.json +++ b/app/manifest.json @@ -1,6 +1,6 @@ { "name": "__MSG_appName__", - "version": "0.0.6", + "version": "0.0.7", "manifest_version": 2, "description": "__MSG_appDescription__", "author": "Jaeger ", diff --git a/app/scripts.babel/background.js b/app/scripts.babel/background.js index c7e126a..a7220bc 100644 --- a/app/scripts.babel/background.js +++ b/app/scripts.babel/background.js @@ -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) => { @@ -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!'); @@ -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) { @@ -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 () { @@ -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); @@ -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()