Remove blank lines from all Sefaria source texts on Source Sheet, button at top
当前为
// ==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 });
})();