Bilibili-Evolved/min/aria2-rpc.min.js
2019-07-29 22:52:22 +08:00

1 line
1.3 KiB
JavaScript

(()=>{return(t,o)=>{function e(){const o=t.aria2RpcOption;const e=o.host.match(/^http[s]?:\/\//)?o.host:"http://"+o.host;const r="aria2.addUri";return{option:o,host:e,methodName:r}}async function r(t,o=false){try{let e=await t();if(typeof e==="string"){e=JSON.parse(e)}if(e.error!==undefined){if(e.error.code===1){logError(`请求遭到拒绝, 请检查您的密钥相关设置.`)}else{logError(`请求发生错误, code = ${e.error.code}, message = ${e.error.message}`)}return false}if(!o){Toast.success(`成功发送了请求, GID = ${e.result}`,"aria2 RPC",5e3)}return true}catch(t){logError(`无法连接到RPC主机.`);return false}}async function n(t,o=false){const{option:n,host:a,methodName:s}=e();return await r(async()=>{const o=window.btoa(unescape(encodeURIComponent(JSON.stringify(t.params))));const e=`${a}:${n.port}/jsonrpc?method=${s}&id=${t.id}&params=${o}`;console.log(`RPC request:`,e);return await Ajax.getJson(e)},o)}async function a(t,o=false){const{option:n,host:a,methodName:s}=e();return await r(async()=>{const o=`${a}:${n.port}/jsonrpc`;return await Ajax.postJson(o,{method:s,id:t.id,params:t.params})},o)}async function s(o,e=false){const r=t.aria2RpcOption;for(const t of o){let o;if(r.method==="get"){o=await n(t,e)}else{o=await a(t,e)}if(e===true&&o===false){logError(`${decodeURIComponent(t.id)} 导出失败`)}}}return{export:{sendRpc:s}}}})();