Files
snippets/miniflux_scripts/auto-summary-styler.js

77 lines
2.7 KiB
JavaScript

(function() {
'use strict';
if (!window.location.href.match(/\/entry\//)) return;
function styleAutoSummaries() {
const content = document.querySelector('.entry-content');
if (!content) return;
// Find all paragraphs that contain RÉSUMÉ AUTO
const allElements = content.querySelectorAll('*');
allElements.forEach(element => {
if (!element || element.dataset.processed) return;
const txt = element.textContent || '';
const m = txt.match(/---\s*(RÉSUMÉ AUTO|RECOMMANDATIONS)\s*---/i);
if (!m) return;
let container = element;
while (container && !['P', 'DIV', 'ARTICLE', 'SECTION', 'LI'].includes(container.tagName)) {
container = container.parentElement;
}
if (!container || container.dataset.processed) return;
const type = m[1].toUpperCase().includes('RÉSUMÉ') ? 'resume' : 'reco';
const wrapper = document.createElement('div');
container.parentElement.insertBefore(wrapper, container);
wrapper.appendChild(container);
while (wrapper.nextElementSibling && ['P','DIV','UL','OL','PRE','BLOCKQUOTE','TABLE'].includes(wrapper.nextElementSibling.tagName)) {
const next = wrapper.nextElementSibling;
if ((next.textContent || '').match(/---\s*(RÉSUMÉ AUTO|RECOMMANDATIONS)\s*---/i)) break;
wrapper.appendChild(next);
}
wrapper.dataset.processed = 'true';
Object.assign(wrapper.style, {
margin: '12px 0',
padding: '10px',
background: type === 'resume' ? 'rgb(51 85 67)' : 'rgb(46 61 88)',
borderLeft: type === 'resume' ? '3px solid rgb(71 180 103)' : '3px solid rgb(99 155 255)',
fontFamily: 'Geist Mono',
whiteSpace: 'pre-wrap'
});
const textIterator = document.createNodeIterator(wrapper, NodeFilter.SHOW_TEXT);
let t;
while ((t = textIterator.nextNode())) {
t.nodeValue = t.nodeValue
.replace(/---\s*RÉSUMÉ AUTO\s*---/gi, '')
.replace(/^\s*RÉSUMÉ AUTO\s*/gi, '')
.replace(/---\s*RECOMMANDATIONS\s*---/gi, '')
.replace(/^\s*RECOMMANDATIONS\s*/gi, '');
}
const header = document.createElement('div');
header.textContent = type === 'resume' ? 'RÉSUMÉ AUTO' : 'RECOMMANDATIONS';
Object.assign(header.style, {
fontWeight: 'bold',
marginBottom: '8px',
color: type === 'resume' ? 'rgb(71 180 103)' : 'rgb(99 155 255)',
fontSize: '0.9em'
});
wrapper.insertBefore(header, wrapper.firstChild);
});
}
if (document.readyState === 'complete') {
styleAutoSummaries();
} else {
window.addEventListener('load', styleAutoSummaries);
}
})();