Skip to content

Commit

Permalink
add filter
Browse files Browse the repository at this point in the history
  • Loading branch information
jae-jae committed May 9, 2018
1 parent 129a0ec commit bee6b25
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 22 deletions.
4 changes: 4 additions & 0 deletions app/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,9 @@
"optionsWhitelistHint": {
"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\\/",
"description": "Only whitelisted URL downloads will be blocked"
},
"optionsErrorReg": {
"message": "Wrong regular expression:",
"description": "Wrong regular expression:"
}
}
4 changes: 4 additions & 0 deletions app/_locales/ja/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,9 @@
"optionsWhitelistHint": {
"message": "ホワイトリストURLのダウンロードのみがインターセプトされます \r\nURLごとに1行、URLはページリンクか実際のダウンロードリンクのいずれかになり、正規表現をサポートします \r\n例:\r\npan.baidu.com\r\nhttp(s)?:\\/\\/\\w+.baidupcs.com\\/file\\/",
"description": "Only whitelist URL downloads will intercept"
},
"optionsErrorReg": {
"message": "間違った正規表現:",
"description": "Wrong regular expression:"
}
}
4 changes: 4 additions & 0 deletions app/_locales/zh_CN/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,9 @@
"optionsWhitelistHint": {
"message": "只有白名单内的网址下载才会被拦截 \r\n一行一条网址,网址可以是页面链接也可以是真正的下载链接,支持正则表达式 \r\n例子:\r\npan.baidu.com\r\nhttp(s)?:\\/\\/\\w+.baidupcs.com\\/file\\/",
"description": "Only whitelist URL downloads will intercept"
},
"optionsErrorReg": {
"message": "错误的正则表达式:",
"description": "Wrong regular expression:"
}
}
4 changes: 4 additions & 0 deletions app/_locales/zh_TW/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,9 @@
"optionsWhitelistHint": {
"message": "只有白名單內的網址下載才會被攔截 \r\n壹行壹條網址,網址可以是頁面鏈接也可以是真正的下載鏈接,支持正則表達式 \r\n例子:\r\npan.baidu.com\r\nhttp(s)?:\\/\\/\\w+.baidupcs.com\\/file\\/",
"description": "Only whitelist URL downloads will intercept"
},
"optionsErrorReg": {
"message": "錯誤的正則表達式:",
"description": "Wrong regular expression:"
}
}
24 changes: 7 additions & 17 deletions app/scripts.babel/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ let preNum = 0;

var getStorage = (key) => {
if (key === 'size') {
let size = localStorage['size'] === undefined ? 0 : localStorage['size']
let size = localStorage['size'] || 0
size = size * 1024 * 1024
return size
} else if(key === 'path') {
let path = localStorage['path'] === undefined ? 'http://localhost:6800/jsonrpc' : localStorage['path']
return path
return localStorage['path'] || 'http://localhost:6800/jsonrpc'
} else if(key === 'enableFilter') {
let enableFilter = localStorage['enableFilter'] === undefined ? 'disabled' : localStorage['enableFilter']
return enableFilter
return localStorage['enableFilter'] || 'disabled'
} else if(key === 'filterUrlRegs') {
return localStorage['filterUrlRegs'] === undefined ? [] : localStorage['filterUrlRegs'].split(',').map((reg) => eval(reg))
}
return localStorage[key]
}
Expand Down Expand Up @@ -61,17 +61,6 @@ let testUrl = (url, regs) => {
})
}

let getFilterUrls = () => {
let filterUrls = getStorage('filterUrls')
if (filterUrls) {
return filterUrls.split(/\n/).filter(url => url.trim().length > 0).map((url) => {
url = '/' + url.trim() + '/'
url = eval(url)
return url
})
}
return [];
}

setInterval(() => {
getGlobalStatus((data) => {
Expand Down Expand Up @@ -101,7 +90,8 @@ chrome.downloads.onDeterminingFilename.addListener(function (down) {
let tabUrl = tab.url
let downloadUrl = down.finalUrl
let enableFilter = getStorage('enableFilter')
let filterUrls = getFilterUrls()
let filterUrls = getStorage('filterUrlRegs')
console.log(filterUrls)
if (enableFilter === 'disabled') {
add(down)
}else if(enableFilter === 'blacklist') {
Expand Down
34 changes: 29 additions & 5 deletions app/scripts.babel/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ new Vue({
filter: this.getI18nMessage('optionsFilter'),
filterUrls: this.getI18nMessage('optionsFilterUrls'),
blacklistHint: this.getI18nMessage('optionsBlacklistHint'),
whitelistHint: this.getI18nMessage('optionsWhitelistHint')
}
whitelistHint: this.getI18nMessage('optionsWhitelistHint'),
errorReg: this.getI18nMessage('optionsErrorReg')
},
lastUrl: ''
}
},
watch: {
Expand All @@ -52,13 +54,35 @@ new Vue({
valueKey (key, defaultValue) {
return localStorage[key] === undefined ? defaultValue : localStorage[key]
},

parseUrls () {
let filterUrls = this.config.filterUrls
if (filterUrls) {
return filterUrls.split(/\n/).filter(function (url) {
return url.trim().length > 0;
}).map((url) => {
this.lastUrl = url
url = '/' + url.trim() + '/';
url = eval(url);
return url;
});
}
return [];
},

save () {
console.log(this.config)
for (let k in this.config) {
localStorage[k] = this.config[k]
try {
console.log(this.parseUrls())
localStorage['filterUrlRegs'] = this.parseUrls()
for (let k in this.config) {
localStorage[k] = this.config[k]
}
this.saved = true
} catch (error) {
alert(this.i18n.errorReg + ' '+ this.lastUrl)
console.log(error)
}
this.saved = true
}
}
})
Expand Down

0 comments on commit bee6b25

Please sign in to comment.