Skip to content

Commit bee6b25

Browse files
committed
add filter
1 parent 129a0ec commit bee6b25

File tree

6 files changed

+52
-22
lines changed

6 files changed

+52
-22
lines changed

app/_locales/en/messages.json

+4
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,9 @@
6363
"optionsWhitelistHint": {
6464
"message": "Only whitelist URL downloads will intercept \r\nOne line per URL, the URL can be either a page link or a real download link, supporting regular expressions \r\nexample:\r\npan.baidu.com\r\nhttp(s)?:\\/\\/\\w+.baidupcs.com\\/file\\/",
6565
"description": "Only whitelisted URL downloads will be blocked"
66+
},
67+
"optionsErrorReg": {
68+
"message": "Wrong regular expression:",
69+
"description": "Wrong regular expression:"
6670
}
6771
}

app/_locales/ja/messages.json

+4
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,9 @@
6363
"optionsWhitelistHint": {
6464
"message": "ホワイトリストURLのダウンロードのみがインターセプトされます \r\nURLごとに1行、URLはページリンクか実際のダウンロードリンクのいずれかになり、正規表現をサポートします \r\n例:\r\npan.baidu.com\r\nhttp(s)?:\\/\\/\\w+.baidupcs.com\\/file\\/",
6565
"description": "Only whitelist URL downloads will intercept"
66+
},
67+
"optionsErrorReg": {
68+
"message": "間違った正規表現:",
69+
"description": "Wrong regular expression:"
6670
}
6771
}

app/_locales/zh_CN/messages.json

+4
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,9 @@
6363
"optionsWhitelistHint": {
6464
"message": "只有白名单内的网址下载才会被拦截 \r\n一行一条网址,网址可以是页面链接也可以是真正的下载链接,支持正则表达式 \r\n例子:\r\npan.baidu.com\r\nhttp(s)?:\\/\\/\\w+.baidupcs.com\\/file\\/",
6565
"description": "Only whitelist URL downloads will intercept"
66+
},
67+
"optionsErrorReg": {
68+
"message": "错误的正则表达式:",
69+
"description": "Wrong regular expression:"
6670
}
6771
}

app/_locales/zh_TW/messages.json

+4
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,9 @@
6262
"optionsWhitelistHint": {
6363
"message": "只有白名單內的網址下載才會被攔截 \r\n壹行壹條網址,網址可以是頁面鏈接也可以是真正的下載鏈接,支持正則表達式 \r\n例子:\r\npan.baidu.com\r\nhttp(s)?:\\/\\/\\w+.baidupcs.com\\/file\\/",
6464
"description": "Only whitelist URL downloads will intercept"
65+
},
66+
"optionsErrorReg": {
67+
"message": "錯誤的正則表達式:",
68+
"description": "Wrong regular expression:"
6569
}
6670
}

app/scripts.babel/background.js

+7-17
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ let preNum = 0;
88

99
var getStorage = (key) => {
1010
if (key === 'size') {
11-
let size = localStorage['size'] === undefined ? 0 : localStorage['size']
11+
let size = localStorage['size'] || 0
1212
size = size * 1024 * 1024
1313
return size
1414
} else if(key === 'path') {
15-
let path = localStorage['path'] === undefined ? 'http://localhost:6800/jsonrpc' : localStorage['path']
16-
return path
15+
return localStorage['path'] || 'http://localhost:6800/jsonrpc'
1716
} else if(key === 'enableFilter') {
18-
let enableFilter = localStorage['enableFilter'] === undefined ? 'disabled' : localStorage['enableFilter']
19-
return enableFilter
17+
return localStorage['enableFilter'] || 'disabled'
18+
} else if(key === 'filterUrlRegs') {
19+
return localStorage['filterUrlRegs'] === undefined ? [] : localStorage['filterUrlRegs'].split(',').map((reg) => eval(reg))
2020
}
2121
return localStorage[key]
2222
}
@@ -61,17 +61,6 @@ let testUrl = (url, regs) => {
6161
})
6262
}
6363

64-
let getFilterUrls = () => {
65-
let filterUrls = getStorage('filterUrls')
66-
if (filterUrls) {
67-
return filterUrls.split(/\n/).filter(url => url.trim().length > 0).map((url) => {
68-
url = '/' + url.trim() + '/'
69-
url = eval(url)
70-
return url
71-
})
72-
}
73-
return [];
74-
}
7564

7665
setInterval(() => {
7766
getGlobalStatus((data) => {
@@ -101,7 +90,8 @@ chrome.downloads.onDeterminingFilename.addListener(function (down) {
10190
let tabUrl = tab.url
10291
let downloadUrl = down.finalUrl
10392
let enableFilter = getStorage('enableFilter')
104-
let filterUrls = getFilterUrls()
93+
let filterUrls = getStorage('filterUrlRegs')
94+
console.log(filterUrls)
10595
if (enableFilter === 'disabled') {
10696
add(down)
10797
}else if(enableFilter === 'blacklist') {

app/scripts.babel/options.js

+29-5
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ new Vue({
2626
filter: this.getI18nMessage('optionsFilter'),
2727
filterUrls: this.getI18nMessage('optionsFilterUrls'),
2828
blacklistHint: this.getI18nMessage('optionsBlacklistHint'),
29-
whitelistHint: this.getI18nMessage('optionsWhitelistHint')
30-
}
29+
whitelistHint: this.getI18nMessage('optionsWhitelistHint'),
30+
errorReg: this.getI18nMessage('optionsErrorReg')
31+
},
32+
lastUrl: ''
3133
}
3234
},
3335
watch: {
@@ -52,13 +54,35 @@ new Vue({
5254
valueKey (key, defaultValue) {
5355
return localStorage[key] === undefined ? defaultValue : localStorage[key]
5456
},
57+
58+
parseUrls () {
59+
let filterUrls = this.config.filterUrls
60+
if (filterUrls) {
61+
return filterUrls.split(/\n/).filter(function (url) {
62+
return url.trim().length > 0;
63+
}).map((url) => {
64+
this.lastUrl = url
65+
url = '/' + url.trim() + '/';
66+
url = eval(url);
67+
return url;
68+
});
69+
}
70+
return [];
71+
},
5572

5673
save () {
5774
console.log(this.config)
58-
for (let k in this.config) {
59-
localStorage[k] = this.config[k]
75+
try {
76+
console.log(this.parseUrls())
77+
localStorage['filterUrlRegs'] = this.parseUrls()
78+
for (let k in this.config) {
79+
localStorage[k] = this.config[k]
80+
}
81+
this.saved = true
82+
} catch (error) {
83+
alert(this.i18n.errorReg + ' '+ this.lastUrl)
84+
console.log(error)
6085
}
61-
this.saved = true
6286
}
6387
}
6488
})

0 commit comments

Comments
 (0)