// ==UserScript==
// @name Add manga baka-updates links to nyaa
// @namespace Violentmonkey Scripts
// @match https://nyaa.si/*
// @match https://nyaa.land/*
// @match https://nyaa.iss.one/*
// @grant GM.xmlHttpRequest
// @connect www.mangaupdates.com
// @version 2.0
// @author szq2
// @description Adds manga links rating and cover from mangaupdates.com to nyaa.si tracker
// @license 0BSD
// @downloadURL none
// ==/UserScript==
const fetchRating = true; // enable to fetch first series rating and link from search page
const fetchRatingDelay = 2000; //ms
const target = "_blank"; // where to open mangaupdates page, _blank for new tab
const re = /^(?:\[.*\])?\s*([^(\[]+?) *((v(olume)?)?[\d\-+ ]+)?( +[\(\[].*)?$/i; // regex to select book name from torrent name
const fetchJSON = (url, data) => new Promise((resolve, reject) => {
GM.xmlHttpRequest({
method: "POST",
url: url,
headers: {
"Content-Type": "application/json"
},
data: data,
responseType: 'json',
anonymous: true,
onload: response => resolve(response.response),
onerror: err => reject(err),
timeout: 2000
});
});
const delay = (time) => new Promise(res => setTimeout(res,time));
async function fetchSeriesDetails(title) {
const apiEndpoint = 'https://api.mangaupdates.com/v1/series/search';
// Make a POST request to the MangaUpdates API with the search title
const data = await fetchJSON(apiEndpoint, JSON.stringify({
search: title,
stype: 'title',
}));
if (data.total_hits === 0 || !data.results || data.results.length === 0) {
console.log('No series found for the given title.' + title + JSON.stringify(data));
throw 0;
}
// Extract the first result
const firstResult = data.results[0];
const coverPage = firstResult.record.image.url.thumb || firstResult.record.image.url.original;
const seriesTitle = firstResult.hit_title;
const rating = firstResult.record.year + " " + (firstResult.record.bayesian_rating || "");
const seriesLink = firstResult.record.url;
return `
[${rating}] `;
}
(async function () {
'use strict';
// books
let book = 0;
for (let a of document.querySelectorAll("tr > td:nth-child(1) > a[title*='Literature']")) {
a = a.parentElement.nextElementSibling.lastElementChild;
const title = a.innerText.match(re);
//console.log(a.innerText);
if (!title) continue;
//fetchSeriesDetails(title[1]);
//continue;
const url = `https://www.mangaupdates.com/series?search=${encodeURI(title[1])}`;
let mangalink = document.createElement("span");
mangalink.innerHTML = `[m] `;
a.insertAdjacentElement("beforebegin", mangalink);
if (fetchRating) {
delay((book + Math.random()) * fetchRatingDelay)
.then(() => fetchSeriesDetails(title[1]))
.then(doc => {
mangalink.innerHTML = doc;
}).catch(error => {
// Handle any errors that occurred during the download
console.error('Error:', error);
});
}
book++;
}
})();