"use strict";function add(e){chrome.downloads.cancel(e.id,function(e){}),getUrlCookie(e.url,function(t){var r=combination(e,t),o=postaria2obj(r);"base64_error"==o?notice("Error adding tasks to aria2!","Error"):sendAnimMsg()})}function postaria2obj(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null,r=getStorage("path"),o=parse_url(r),n=o[1];return n&&0==n.indexOf("token:")&&(e.params?e.params.unshift(n):e.params=[n]),fetch(o[0]+"?tm="+(new Date).getTime().toString(),{method:"POST",body:JSON.stringify(e),headers:{Authorization:n,"Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"}}).then(function(e){return e.json()}).then(function(e){t&&t(e)})["catch"](function(t){console.error("Error:",t),console.log("Error aria2 configuration!"),e&&"aria2.addUri"===e.method&&notice("Error adding tasks to aria2,please check the configuration!","Error")}),"ok"}function parse_url(e){function t(e){return e.match(/^(?:(?![^:@]+:[^:@\/]*@)[^:\/?#.]+:)?(?:\/\/)?(?:([^:@]*(?::[^:@]*)?)?@)?/)[1]}function r(e){return e.replace(/^((?![^:@]+:[^:@\/]*@)[^:\/?#.]+:)?(\/\/)?(?:(?:[^:@]*(?::[^:@]*)?)?@)?(.*)/,"$1$2$3")}var o=t(e),n=null;o&&(n=0==o.indexOf("token:")?o:"Basic "+btoa(o));var a=r(e);return[a,n]}function getUrlCookie(e,t){chrome.cookies.getAll({url:e},function(e){var r=[];for(var o in e){var n=e[o];r.push(n.name+"="+n.value)}r=r.join("; "),t(r)})}function combination(e,t){var r=[];if(r.push("Cookie: "+t),r.push("User-Agent: "+navigator.userAgent),r.push("Connection: keep-alive"),r.push("Referer: "+e.referrer),""==e.filename)var o={jsonrpc:"2.0",method:"aria2.addUri",id:(new Date).getTime().toString(),params:[[e.finalUrl],{header:r}]};else var o={jsonrpc:"2.0",method:"aria2.addUri",id:(new Date).getTime().toString(),params:[[e.finalUrl],{out:decodeURIComponent(e.filename),header:r}]};return o}function rightadd(e,t){var r={filename:""};return r.finalUrl=e.linkUrl,r.referrer=e.pageUrl,getStorage("path")?void getUrlCookie(r.finalUrl,function(e){var t=combination(r,e),o=postaria2obj(t);"base64_error"==o?notice("Error adding tasks to aria2!","Error"):sendAnimMsg()}):(alert("Camtd has not been configured"),chrome.tabs.create({url:"options.html"},function(e){}),0)}chrome.runtime.onInstalled.addListener(function(e){console.log("previousVersion",e.previousVersion)});var preNum=0,currentUrl="",getStorage=function getStorage(key){if("size"===key){var size=localStorage.size||0;return size=1024*size*1024}return"path"===key?localStorage.path||"http://localhost:6800/jsonrpc":"enableFilter"===key?localStorage.enableFilter||"disabled":"filterUrlRegs"===key?localStorage.filterUrlRegs?localStorage.filterUrlRegs.split(",").map(function(reg){return eval(reg)}):[]:localStorage[key]},notice=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"Camtd";chrome.notifications.create({type:"basic",iconUrl:"images/default/icon-38.png",title:t,message:e},function(e){setTimeout(function(t){chrome.notifications.clear(e)},5e3)})},sendAnimMsg=function(){console.log("sending msg"),chrome.tabs.query({active:!0},function(e){e.forEach(function(e){chrome.tabs.sendMessage(e.id,{action:"show-add-task-anim"}),console.log("msg send")})})},getGlobalStatus=function(e){postaria2obj({jsonrpc:"2.0",method:"aria2.getGlobalStat",id:(new Date).getTime().toString()},e)},testUrl=function(e,t){return t.some(function(t){return t.test(e)})},getTabUrl=function(){chrome.tabs.query({active:!0},function(e){e.forEach(function(e){currentUrl=e.url})})},isIgnore=function(e){return(!/^http:|^https:|^ftp:|^sftp:/.test(e.finalUrl)||0==getStorage("enabled")||Math.abs(e.fileSize)<getStorage("size"))&&(console.log("Ignore file"),!0)};setInterval(function(){getGlobalStatus(function(e){var t=e.result.numActive;t>0?(chrome.browserAction.setBadgeText({text:t}),chrome.browserAction.setBadgeBackgroundColor({color:"#00CC66"})):chrome.browserAction.setBadgeText({text:""}),preNum>t&&notice("Download completed!"),preNum=t})},1e3),chrome.tabs.onActivated.addListener(function(){getTabUrl()}),chrome.tabs.onUpdated.addListener(function(){getTabUrl()}),chrome.notifications.onClicked.addListener(function(e){chrome.downloads.showDefaultFolder(),chrome.notifications.clear(e,function(e){})}),chrome.downloads.onDeterminingFilename.addListener(function(e){if(!getStorage("path"))return alert("Camtd has not been configured"),chrome.tabs.create({url:"options.html"},function(e){}),0;if(isIgnore(e))return 0;var t=e.finalUrl,r=getStorage("enableFilter"),o=getStorage("filterUrlRegs");"disabled"===r?add(e):"blacklist"===r?testUrl(currentUrl,o)||testUrl(t,o)?console.log("blacklist:",currentUrl,t):add(e):testUrl(currentUrl,o)||testUrl(t,o)?add(e):console.log("not in whitelist:",currentUrl,t)}),chrome.contextMenus.create({title:"Send to Aria2",contexts:["link"],onclick:rightadd});
//# sourceMappingURL=background.js.map
