Skip to content

Instantly share code, notes, and snippets.

@azazar
Last active January 31, 2024 02:07
Show Gist options
  • Save azazar/4ad2d4cffab141c2585df907e7ecd75c to your computer and use it in GitHub Desktop.
Save azazar/4ad2d4cffab141c2585df907e7ecd75c to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name flibusta.is качать с названием
// @namespace Violentmonkey Scripts
// @match *://flibusta.is/a/*
// @match *://flibusta.is/s/*
// @match *://flibusta.is/sequence/*
// @grant GM_setClipboard
// @grant GM_download
// @version 1.4
// @author -
// @description 2/11/2023, 9:38:17 PM
// ==/UserScript==
let extMap = {
'fb2': '.fb2.zip',
'epub': '.epub',
'mobi': '.mobi',
};
let author = document.querySelector('h1.title').innerText;
let books = {};
let sequence = null;
if (location.pathname.match(/^\/s\//)) {
sequence = document.querySelector('h1.title').innerText.trim();
}
Array.from(document.querySelectorAll('form[action^="/a/"] a[href], form[action^="/mass/download"] a[href], #main a[href]')).forEach(a => {
let href = a.getAttribute("href");
if (href.startsWith('/s/')) {
sequence = a.innerText.trim();
return;
}
let match = href.match(/^\/b\/(\d+)$/);
if (match) {
let bookId = match[1];
books[bookId] = a.innerText.trim();
if (sequence !== null && a.previousSibling.nodeType === Node.TEXT_NODE) {
match = a.previousSibling.textContent.match(/^ *- (\d+)\. *$/);
if (match) {
let number = match[1];
books[bookId] = `${sequence} ${number}. ${a.innerText.trim()}`;
}
}
return;
}
match = href.match(/^\/b\/(\d+)\/(fb2|epub|mobi|download)$/);
if (match) {
let id = match[1];
let type = match[2];
if (type === 'download' && a.innerText === '(скачать)') {
type = 'fb2';
}
if (books[id]) {
let title = books[id];
if (!/[\.!\?]$/.test(title)) {
title += '.';
}
let ext = extMap[type];
if (ext === undefined) {
ext = type;
}
let filename = `${title} ${author}${ext}`;
a.addEventListener('click', (event) => {
if (type && type !== '') {
event.preventDefault();
let downloadUrl = a.href;
GM_download({
url: downloadUrl,
name: filename,
saveAs: true // optional, to prompt for location
});
}
else {
GM_setClipboard(filename);
}
});
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment