Created
December 22, 2018 20:11
-
-
Save robertfairley/53afafb567cb432f24bcc8dae1b6ede8 to your computer and use it in GitHub Desktop.
Quick function to use in dev tools to improve reading experience on old HTML articles/papers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Gives the browser window a better midly altered reading view for | |
* pages that use unaltered styles for margin, etc. Limits line length, | |
* slightly reduces contrast, increases line height. Great for old HTML | |
* formatted research papers. | |
* | |
* @param {number?} level - Set the level of the contrast. Defaults to 0. | |
* @returns {boolean} - True if the function succeeds, false if not. | |
*/ | |
function setReaderStyles(level = 0) { | |
const setLevel = level || 0; // Older browsers | |
const levels = [ | |
{ | |
"level": 0, | |
"backgroundColor": "rgb(245, 245, 245)", | |
"color": "#444", | |
}, | |
]; | |
if (setLevel > (levels.length - 1) || setLevel < 0)) | |
throw new Error("Level not within range"); | |
if (!document) | |
throw new Error("Not a browser"); | |
if (!document.body) | |
throw new Error("Body element not found"); // Don't support too-old browsers | |
try { | |
document.body.style.maxWidth = "70%"; | |
document.body.style.marginLeft = "15%"; | |
document.body.style.lineHeight = 1.3; | |
document.body.style.backgroundColor = levels[setLevel]["backgroundColor"]; | |
document.body.style.color = levels[setLevel]["color"]; | |
} catch (e) { | |
return false; | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment