// ==UserScript==
// @name 复制qBittorrent种子列表
// @name:en qBitorrent webUI torrent list copy
// @namespace localhost
// @version 0.1
// @description 在webUI的右键菜单中增加批量复制字段的功能,可直接粘贴到excel
// @description:en add a contextmenu item to batch copy torrent information in order to pasting to Excel
// @author flashlab
// @match http://127.0.0.1:8080/
// @icon https://www.qbittorrent.org/favicon.ico
// @license MIT
// @run-at document-end
// @downloadURL none
// ==/UserScript==
// 完整字段请参考官方api https://github.com/qbittorrent/qBittorrent/wiki/WebUI-API-(qBittorrent-4.1)#get-torrent-list
// 仅在最新版qBitorrent中测试!
(function() {
'use strict';
const newItem = '
' +
' ' +
'
批量复制' +
' ' +
' ' +
'';
function getDomainBody(string) {
const match = string.match(/:\/\/([^\/]+\.)?([^\/\.]+)\.[^\/\.:]+/i)
if (match != null && match.length > 2 && typeof match[2] === 'string' && match[2].length > 0) {
return match[2]
} else {
return ''
}
}
window.addEventListener('load', function () {
// append html
const newmenu = document.getElementById('torrentsTableMenu');
if (newmenu == null) return;
newmenu.insertAdjacentHTML("beforeend", newItem)
new ClipboardJS('#copyall', {
text: function() {
return copyAllSelected();
}
});
// listen to option event
const options = document.querySelectorAll("#multicp a:not([role])");
options.forEach(function(op) {
op.addEventListener("click", function(e) {
e.stopPropagation();
e.target.firstElementChild.checked ^= 1;
});
});
const optionT = document.getElementById('copyswitch');
optionT.addEventListener("click", function(e) {
e.stopPropagation();
const isChecked = optionT.classList.toggle("checkall")
options.forEach(function(op) {
op.firstElementChild.checked = isChecked;
});
});
// copy steps
function copyAllSelected() {
const tablekeys = [];
const tablehead = [];
options.forEach(function(op) {
const key = op.href.split('#')[1];
if (op.firstElementChild.checked && key) {
tablekeys.push(key)
tablehead.push(op.textContent.trim())
}
});
const selectedRows = torrentsTable.selectedRowsIds();
if (selectedRows.length <= 0) return;
let tablebody = "";
const rows = torrentsTable.getFilteredAndSortedRows();
selectedRows.forEach(function(hash) {
const fulldata = rows[hash].full_data;
const tablerow = [];
tablekeys.forEach(function(k) {
switch (k) {
//github.com/qbittorrent/qBittorrent/blob/5db2c2c2be422bffca693f7d5620ee74970acd71/src/webui/www/private/scripts/dynamicTable.js#L1230
case "added_on":
case "completion_on":
tablerow.push(fulldata[k] <0 ? '' : new Date(fulldata[k] * 1000).toLocaleString());
break;
//github.com/qbittorrent/qBittorrent/blob/5db2c2c2be422bffca693f7d5620ee74970acd71/src/webui/www/private/scripts/misc.js#L56
case "size":
case "total_size":
case "uploaded":
tablerow.push(unsafeWindow.qBittorrent.Misc.friendlyUnit(fulldata[k]));
break;
case "tracker":
tablerow.push(getDomainBody(fulldata[k]));
break;
case "progress":
tablerow.push(unsafeWindow.qBittorrent.Misc.friendlyPercentage(fulldata[k]));
break;
default:
tablerow.push(fulldata[k]);
}
});
tablebody += tablerow.join("\t") + "\n";
});
return tablehead.join("\t") + "\n" + tablebody;
};
}, false);
})();