Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save yujinlin0224/161893f118dea3fbe6fe3c55ba258d12 to your computer and use it in GitHub Desktop.
Save yujinlin0224/161893f118dea3fbe6fe3c55ba258d12 to your computer and use it in GitHub Desktop.
DMHY Add Download Torrent File Column
// ==UserScript==
// @name DMHY Add Download Torrent File Column
// @version 1.3
// @namespace https://yurina.dev/
// @downloadURL https://gist.github.com/yujinlin0224/161893f118dea3fbe6fe3c55ba258d12/raw/DmhyAddDownloadTorrentFileColumn.user.js
// @homepageURL https://github.com/yujinlin0224
// @author yujinlin0224
// @description DMHY Add Download Torrent File Column
// @include /^https?://((www|share)\.)?dmhy\.org(/.*)?$/
// @require https://raw.github.com/emn178/hi-base32/master/build/base32.min.js
// ==/UserScript==
const topicListTable = document.getElementById('topic_list');
if (topicListTable) {
const topicListTableHeadR = topicListTable.tHead.rows[0];
const magnetH = topicListTableHeadR.cells[3];
const torrentH = magnetH.cloneNode(true);
torrentH.width = '45px';
torrentH.innerHTML = '<span class="title">Torrent</span>';
topicListTableHeadR.insertBefore(torrentH, magnetH);
const magnetBase32Regexp = /^magnet:\?xt=urn:btih:([A-Z2-7=]+)/g;
const topicListTableBodyRs = topicListTable.tBodies[0].rows;
for (const topicListTableBodyR of topicListTableBodyRs) {
const date = topicListTableBodyR.cells[0].children[0].innerText.split(' ')[0];
const magnetD = topicListTableBodyR.cells[3];
const magnetMatched = magnetD.children[0].href.matchAll(magnetBase32Regexp).next();
if (magnetMatched.value) {
const magnetBase32 = magnetMatched.value[1];
const magnetHex = Array.from(
base32.decode.asBytes(magnetBase32), (b) => ('0' + b.toString(16)).slice(-2),
).join('');
const torrentD = magnetD.cloneNode(true);
const torrentA = torrentD.children[0];
torrentA.classList.replace('arrow-magnet', 'arrow-torrent');
torrentA.href = `https://dl.dmhy.org/${date}/${magnetHex}.torrent`;
torrentA.title = 'Torrent';
topicListTableBodyR.insertBefore(torrentD, magnetD);
} else {
topicListTableBodyR.insertBefore(magnetD.cloneNode(false), magnetD);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment