Skip to content

Instantly share code, notes, and snippets.

@mvark
Last active November 15, 2023 03:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mvark/91eb4fbb35100495700668d512bb211c to your computer and use it in GitHub Desktop.
Save mvark/91eb4fbb35100495700668d512bb211c to your computer and use it in GitHub Desktop.
Bookmarklet code to calculate Word Count, Estimated Reading Time and Readability Level (Flesch-Kincaid Grade Level)
// Code generated by ChatGPT
javascript:function estimateReadability() {
const wordsPerMinute = 200;
// Function to get word count from text
const getWordCount = text => text ? text.split(/\s+/).length : 0;
// Function to calculate readability level
const getReadabilityLevel = (wordCount, sentenceCount, syllableCount) => {
const averageWordsPerSentence = wordCount / sentenceCount;
const averageSyllablesPerWord = syllableCount / wordCount;
return 0.39 * averageWordsPerSentence + 11.8 * averageSyllablesPerWord - 15.59;
};
// Function to get text from selection or entire article
const getText = () => window.getSelection().toString().trim() || document.body.innerText.trim();
// Get the text and word count
const text = getText();
const wordCount = getWordCount(text);
// Calculate reading time
const readingTime = Math.ceil(wordCount / wordsPerMinute);
// Count sentences and syllables for readability calculation
const sentences = text.split(/[.!?]+/).length;
const syllables = text.split(/[aeiouy]+/i).length - 1;
// Calculate readability level
const readabilityLevel = getReadabilityLevel(wordCount, sentences, syllables);
// Show alert with information
alert(`Word Count: ${wordCount}\nReading Time: ${readingTime} min\nReadability Level (Flesch-Kincaid Grade Level): ${readabilityLevel.toFixed(2)}`);
}
// Call the function to estimate readability
estimateReadability();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment