在 qBittorrent WebUI 中添加按钮,用于标记tracker状态出错的种子
当前为
// ==UserScript==
// @name qB-WebUI 标记tracker异常(魔改版)
// @name:en qB-WebUI tag trackerERR
// @namespace localhost
// @version 0.1.0
// @author ColderCoder
// @author avatasia
// @author Schalkiii
// @description 在 qBittorrent WebUI 中添加按钮,用于标记tracker状态出错的种子
// @description:en add a button in qBittorrent WebUI to tag torrents with tracker error
// @license MIT
// @run-at document-end
// @match http://127.0.0.1:8080/
// @require https://cdn.bootcdn.net/ajax/libs/jquery/3.6.3/jquery.min.js
// ==/UserScript==
//require qB API v2.3.0 +
const host = window.location.href;
const baseURL = host + 'api/v2/torrents/';
async function getFetch(route) {
try {
const response = await fetch(baseURL + route);
if (!response.ok) {
throw new Error('Error fetching info!');
}
const data = await response.json();
return data;
} catch (error) {
console.error(error);
return null;
}
}
async function processTorrents() {
try {
const torrentList = await getFetch('info');
let count = 0;
let count_torrent = 0;
const keywords = ['registered', 'deleted', 'exist', 'banned']; // Keywords to search for
for (const torrent of torrentList) {
const trackers = await getFetch(`trackers?hash=${torrent.hash}`);
count_torrent++;
console.log(`${count_torrent}`);
for (let i = 0; i < trackers.length; i++) {
const tracker = trackers[i];
if (tracker.status === 4) { //tracker is not working
let torrentTags = ['trackerErr'];
for (const msg of keywords) {
if (tracker.msg.includes(msg)) {
count++;
console.log(`${count}. ${torrent.name}: ${tracker.msg}`);
//replace trackerErr with Unregistered if keyword found
torrentTags.splice(torrentTags.indexOf('trackerErr'), 1, 'Unregistered');
}
}
const tags = torrentTags.join(",");
//const response = await fetch(`${baseURL}addTags?hashes=${torrent.hash}&tags=${tags}`); //GET method. only for qb version under v4.5.0
const url = `${baseURL}addTags`;
const data = new URLSearchParams();
data.append('hashes', torrent.hash);
data.append('tags', tags);
fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: data
})
.then(response => {
console.log(response);
})
.catch(error => {
console.error('Error:', error);
});
break;
}
}
}
console.log('Done.');
} catch (error) {
console.error('Error:', error.message);
}
}
jQuery("#desktopNavbar > ul").append(
"<li><a class='js-modal'><b> 标记tracker异常 </b></a></li>",
);
jQuery(".js-modal").click(async function () {
await processTorrents();
});