Re-Library FB2: Export Single Chapter

Экспорт текущей главы в FB2 (только <section>; без шапки). Фильтрация мусора, починка юникода.

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Re-Library FB2: Export Single Chapter
// @namespace    https://re-library.com/
// @version      1.31
// @description  Экспорт текущей главы в FB2 (только <section>; без шапки). Фильтрация мусора, починка юникода.
// @match        https://re-library.com/translations/*/volume-*/chapter-*/*
// @match        https://re-library.com/translations/*/chapter-*/*
// @match        https://re-library.com/translations/*/volume-*/chapter-*
// @match        https://re-library.com/translations/*/chapter-*
// @grant        none
// ==/UserScript==

(() => {
  'use strict';
  if (document.getElementById('fb2-export-btn')) return;

  const entry = document.querySelector('.entry-content');
  const h1 = document.querySelector('h1.entry-title');
  if (!entry || !h1) return;

  function sanitizeText(s) {
    return (s || '')
      .replace(/&/g, '&amp;')
      .replace(/</g, '&lt;')
      .replace(/>/g, '&gt;')
      .replace(/"/g, '&quot;')
      .replace(/'/g, '&apos;')
      .replace(/……/g, '......')
      .replace(/…/g, '...')
      .replace(/[–—]/g, '-')
      .replace(/[“”«»]/g, '"')
      .replace(/\u00A0/g, ' ')
      .replace(/[\u2000-\u200B]/g, ' ')
      .replace(/\s+\n/g, '\n')
      .trim();
  }

  function cleanContainer(root) {
    const junkSel = [
      '.navigation', '.sharedaddy', '.heateor_sss_sharing_container',
      '.wpulike', '.wpdiscuz', '.comments-area', '#comments',
      '.comment-respond', 'form.comment-form', '.sd-sharing', '.tags-links',
      'script', 'style', 'iframe', 'ins', 'nav', 'aside'
    ].join(',');
    root.querySelectorAll(junkSel).forEach(n => n.remove());
  }

  function extractParagraphs(container) {
    const paras = [];
    cleanContainer(container);

    const stop = container.querySelector('.navigation'); // может и не быть
    for (const p of container.querySelectorAll('p')) {
      if (stop) {
        const rel = p.compareDocumentPosition(stop);
        // если stop уже НЕ "после" p — значит мы прошли границу
        if (!(rel & Node.DOCUMENT_POSITION_FOLLOWING)) break;
      }
      const t = (p.textContent || '').trim();
      if (!t) continue;
      // фильтр мусора по тексту
      if (/^leave a comment$/i.test(t)) continue;
      if (/^link here$/i.test(t)) continue;
      if (/donation to this page helps/i.test(t)) continue;
      if (/support your favorite translation groups/i.test(t)) continue;
      paras.push(`<p>${sanitizeText(t)}</p>`);
    }
    return paras;
  }

  function buildSection() {
    const title = sanitizeText(h1.textContent || '');
    const body = extractParagraphs(entry).join('\n');
    return `<section>\n<title><p>${title}</p></title>\n${body}\n</section>`;
  }

  function download(text, name) {
    const blob = new Blob([text], { type: 'application/xml;charset=utf-8' });
    const a = document.createElement('a');
    a.href = URL.createObjectURL(blob);
    a.download = name;
    a.click();
    setTimeout(() => URL.revokeObjectURL(a.href), 4000);
  }

  const btn = document.createElement('button');
  btn.id = 'fb2-export-btn';
  btn.textContent = 'Экспорт FB2';
  btn.style.cssText = 'margin:10px 0;padding:6px 12px;font-weight:600;';

  btn.onclick = () => {
    const section = buildSection();
    const safe = (h1.textContent || 'chapter').replace(/[^\w\d\-]+/g, '_').slice(0, 120);
    download(section, `${safe}.fb2`);
  };

  h1.parentNode.insertBefore(btn, h1.nextSibling);
})();