Skip to content

Instantly share code, notes, and snippets.

@Sg4Dylan
Created October 18, 2020 04:08
Show Gist options
  • Save Sg4Dylan/c33fe3049862031eecc0508ce4a2c0a7 to your computer and use it in GitHub Desktop.
Save Sg4Dylan/c33fe3049862031eecc0508ce4a2c0a7 to your computer and use it in GitHub Desktop.
配合 codeskyblue/gohttpserver 使用,aria2 直接下载目录并保持结构
// ==UserScript==
// @name GoHFS-Exporter
// @namespace http://tampermonkey.net/
// @version 2020.10.18(0.1)
// @description Export link from Go HFS to aria2
// @author SgDylan
// @match https://example.com/*
// ==/UserScript==
let downloadFileUrl = [];
let basic_auth = "BASIC AUTH BASE64 CODE";
const aria2_rpc = "http://127.0.0.1:6800/jsonrpc";
addCallerLink();
// 绑定界面
function addCallerLink() {
// 放置按钮
var download_btn = document.createElement("button");
download_btn.id = "ariaGo";
download_btn.className = "btn btn-xs btn-default";
download_btn.innerHTML = "Download via Aria";
document.getElementsByTagName('tr')[0].children[0].children[0].appendChild(download_btn);
// 绑定事件
document.getElementById('ariaGo').addEventListener('click', callAria2, false);
}
// 响应 Download via Aria 按钮事件
function callAria2() {
downloadFileUrl = [];
parseFolder(window.location.href);
let aria2 = new ARIA2(aria2_rpc);
downloadFileUrl.forEach(function(v) {
aria2.addUri(v.url, {out: v.path, header: 'Authorization: Basic '+basic_auth});
});
alert("已将当前文件夹内容添加至下载队列:\n"+decodeURIComponent(window.location.pathname).split('/').pop());
}
// 递归遍历目录
function parseFolder(target_url) {
let xhr = new XMLHttpRequest();
let api_url = target_url + "?json=true";
console.log("当前处理:"+api_url);
xhr.onreadystatechange = function (e) {
if (xhr.readyState == 4 && xhr.status == 200) {
let resp = JSON.parse(xhr.responseText);
resp.files.forEach(function(v) {
// 如果是文件就添加进待下载列表
if (v.type == "file") {
let temp_item = {};
temp_item.url = window.location.origin + "/" + v.path;
let this_dir = decodeURIComponent(window.location.pathname).split('/').pop(); // 当前文件夹名称
temp_item.path = safeName(v.path.substring(v.path.indexOf(this_dir)));
downloadFileUrl.push(temp_item);
}
// 如果是目录就递归
else {
parseFolder(window.location.origin + "/" + v.path);
}
});
}
};
xhr.open("GET", api_url, false);
xhr.send();
}
// 过滤不合规的字符
function safeName(nameDesu) {
return decodeURIComponent(nameDesu).replace(/[\\\|\:\*\"\?\<\>]/g,"_");
}
var ARIA2 = (function() {
const jsonrpc_version = '2.0';
function get_auth(url) {
return url.match(/^(?:(?![^:@]+:[^:@\/]*@)[^:\/?#.]+:)?(?:\/\/)?(?:([^:@]*(?::[^:@]*)?)?@)?/)[1];
}
function request(jsonrpc_path, method, params) {
let xhr = new XMLHttpRequest();
let auth = get_auth(jsonrpc_path);
jsonrpc_path = jsonrpc_path.replace(/^((?![^:@]+:[^:@\/]*@)[^:\/?#.]+:)?(\/\/)?(?:(?:[^:@]*(?::[^:@]*)?)?@)?(.*)/, '$1$2$3'); // auth string not allowed in url for firefox
let request_obj = {
jsonrpc: jsonrpc_version,
method: method,
id: (new Date()).getTime().toString(),
};
if (params) request_obj.params = params;
if (auth && auth.indexOf('token:') === 0) params.unshift(auth);
xhr.open("POST", jsonrpc_path+"?tm="+(new Date()).getTime().toString(), true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
if (auth && auth.indexOf('token:') !== 0) {
xhr.setRequestHeader("Authorization", "Basic "+btoa(auth));
}
xhr.send(JSON.stringify(request_obj));
}
return function(jsonrpc_path) {
this.jsonrpc_path = jsonrpc_path;
this.addUri = function (uri, options) {
request(this.jsonrpc_path, 'aria2.addUri', [[uri, ], options]);
};
return this;
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment