diff --git a/app/_locales/en/messages.json b/app/_locales/en/messages.json index cf099bc..32a1c59 100644 --- a/app/_locales/en/messages.json +++ b/app/_locales/en/messages.json @@ -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:" } } diff --git a/app/_locales/ja/messages.json b/app/_locales/ja/messages.json index 8a868b0..2fd3e60 100644 --- a/app/_locales/ja/messages.json +++ b/app/_locales/ja/messages.json @@ -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:" } } diff --git a/app/_locales/zh_CN/messages.json b/app/_locales/zh_CN/messages.json index 3562f0e..f5070e7 100644 --- a/app/_locales/zh_CN/messages.json +++ b/app/_locales/zh_CN/messages.json @@ -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:" } } diff --git a/app/_locales/zh_TW/messages.json b/app/_locales/zh_TW/messages.json index 3b06532..631fa83 100644 --- a/app/_locales/zh_TW/messages.json +++ b/app/_locales/zh_TW/messages.json @@ -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:" } } diff --git a/app/scripts.babel/background.js b/app/scripts.babel/background.js index 26b6752..c7e126a 100644 --- a/app/scripts.babel/background.js +++ b/app/scripts.babel/background.js @@ -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] } @@ -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) => { @@ -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') { diff --git a/app/scripts.babel/options.js b/app/scripts.babel/options.js index c588dbe..8cc73ed 100644 --- a/app/scripts.babel/options.js +++ b/app/scripts.babel/options.js @@ -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: { @@ -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 } } })