Sefaria Source Sheet Newline Cleaner

Remove blank lines from all Sefaria source texts on Source Sheet, button at top

当前为 2025-08-11 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Sefaria Source Sheet Newline Cleaner
// @namespace    http://binjomin.hu/
// @version      0.2
// @license      MIT
// @description  Remove blank lines from all Sefaria source texts on Source Sheet, button at top
// @author       Binjomin Szanto-Varnagy
// @match        https://*.sefaria.org/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Check if any source span contains blank lines
    function hasBlankLines(text) {
        const lines = text.split('\n');
        return lines.some(line => line.trim() === '');
    }

    // Check if *any* source in the document has blank lines
    function sourcesHaveBlankLines() {
        const textSpans = document.querySelectorAll('.sourceContentText span[data-slate-string="true"]');
        for (const span of textSpans) {
            if (hasBlankLines(span.textContent)) {
                return true;
            }
        }
        return false;
    }

    // Clean all .sourceContentText containers
    function cleanAllSources() {
        const containers = document.querySelectorAll('.sourceContentText');
        for (const container of containers) {
            const textSpans = container.querySelectorAll('span[data-slate-string="true"]');
            for (const span of textSpans) {
                let lines = span.textContent.split('\n');
                // Keep spaces inside text, only remove fully empty lines
                lines = lines.filter(line => line.trim().length > 0);
                span.textContent = lines.join('\n');
            }
        }
    }

    // Create the cleanup button at top-left
    function createButton() {
        if (document.getElementById('sefaria-clean-btn')) return; // prevent duplicates

        const btn = document.createElement('button');
        btn.id = 'sefaria-clean-btn';
        btn.textContent = 'Remove Blank Lines from All Sources';
        btn.style.position = 'fixed';
        btn.style.top = '10px';
        btn.style.left = '10px';
        btn.style.zIndex = '9999';
        btn.style.padding = '8px 12px';
        btn.style.fontSize = '14px';
        btn.style.backgroundColor = '#007bff';
        btn.style.color = 'white';
        btn.style.border = 'none';
        btn.style.borderRadius = '4px';
        btn.style.cursor = 'pointer';
        btn.style.boxShadow = '0 2px 6px rgba(0,0,0,0.2)';
        btn.title = 'Click to remove blank lines from all Sefaria sources';

        btn.addEventListener('click', () => {
            cleanAllSources();
            btn.disabled = true;
            btn.textContent = 'Blank Lines Removed!';
            btn.style.backgroundColor = '#28a745';
        });

        document.body.appendChild(btn);
    }

    // Initialize after content loads
    function init() {
        if (sourcesHaveBlankLines()) {
            createButton();
        }
    }

    // Run after short delay (Sefaria loads dynamically)
    setTimeout(init, 1500);

    // Watch for dynamically loaded sources (optional)
    const observer = new MutationObserver(() => {
        if (sourcesHaveBlankLines()) {
            createButton();
        }
    });
    observer.observe(document.body, { childList: true, subtree: true });

})();