Skip to content

Instantly share code, notes, and snippets.

@andre-st
Last active January 30, 2021 20:35
Show Gist options
  • Save andre-st/f24e9a44014fedeed5fbd1777b0ff35c to your computer and use it in GitHub Desktop.
Save andre-st/f24e9a44014fedeed5fbd1777b0ff35c to your computer and use it in GitHub Desktop.
Browser bookmarklet "UnCSS" improves readability of long texts by replacing all original styles with better line-heights, font sizes, paragraph margins, few colors etc. Firefox provides this out of the box (Reader Mode). The Brave-browser, however, does not.
javascript: (function() {
const DARKMODE = true;
function every( sel, how, fil ) {
Array.from( document.getElementsByTagName( sel ))
.filter( e => fil ? fil( e ) : true )
.forEach( how );
}
function hasPrevText( e ) {
while( e = e.previousSibling )
if( e.nodeType != Node.COMMENT_NODE ) return true;
return false;
}
function rmAtts( e ) {
while( e.attributes.length > 0 ) e.removeAttribute( e.attributes[0].name );
}
const styled = s => e => e.style.cssText = s;
const hcss = 'line-height: 1.25em; font-size:';
every( 'body', rmAtts );
every( 'font', rmAtts );
every( 'table', rmAtts );
every( 'td', rmAtts );
Array.from( document.styleSheets ).forEach( s => s.disabled = true );
every( '*', styled( 'line-height: inherit; font-size: inherit; padding: 0; margin: 2em 0 1em 0;' ));
every( 'a', styled( DARKMODE ? 'color: #68a6c3;' : 'color: #1079ab' ));
every( 'li', styled( 'margin: 0.25em 0;' ));
every( 'h1', styled( hcss + '1.7em;' ));
every( 'h2', styled( hcss + '1.5em;' ));
every( 'h3', styled( hcss + '1.2em;' ));
every( 'h4', styled( hcss + '1.1em;' ));
every( 'br', e => e.insertAdjacentHTML( 'afterend', '<br>' ), e => hasPrevText( e ));
document.body.style.cssText = 'margin: 2em auto 5em auto; width: 800px; min-width: 0; max-width: 800px; font-size: 16pt; line-height: 1.5em;' +
(DARKMODE ? 'background-color: #131516; color: #CECAC3'
: 'background-color: #efefef; color: #000');
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment