Skip to content

Instantly share code, notes, and snippets.

@lewdev
Last active November 12, 2024 19:39
Show Gist options
  • Select an option

  • Save lewdev/44885938b00f605c36add2750f3e7bcf to your computer and use it in GitHub Desktop.

Select an option

Save lewdev/44885938b00f605c36add2750f3e7bcf to your computer and use it in GitHub Desktop.
πŸš‚ Random Text Generator in a Bookmark (bookmarklet)

πŸš‚ Random Text Generator in a Bookmark (bookmarklet)

Do you need to quickly generate random text?

Use this bookmarklet to quickly gen up some text. This uses words found in the keys of the window object and splits Camel-cased names and removes on at the start and single-character words. Then randomly puts 5-9 words into 5-9 sentences in 5-9 paragraphs.

Object.keys(window).reduce((a,b)=>[...a,...b.split(/^on|(?=[A-Z])/).filter(a=>a.length>1)],[])

πŸ”– Bookmarklet (346b)

Copy-paste this into your address bar to try it out. You can also save it as a bookmark.

data:text/html,<title>Gen Text</title><body id=o onload="w=Object.keys(window).reduce((a,b)=>[...a,...b.split(/^on|(?=[A-Z])/).filter(a=>a.length>1)],[]);r=(d,e=0)=>(Math.random()*d>>1)+e;g=(a,b=' ')=>new Array(r(9,5)).fill().map(a).join(b);a=_=>(s=g(_=>w[r(w.length)])).charAt(0).toUpperCase()+s.substr(1)+'.';o.innerHTML=g(_=>g(a),'<br><br>')">

πŸ‘¨β€πŸ’» Original Code

This is the original code I wrote before changing it to a bookmarklet.

<p id=o></p><script>
//array of words found in `window`, splits camel cased words, removes single-character words
w=Object.keys(window).reduce((a,b)=>[...a,...b.split(/^on|(?=[A-Z])/).filter(a=>a.length>1)],[])

//gets a random integer from `e` to `d`
r=(d,e=0)=>(Math.random()*d>>1)+e

//does something 5-9 times
g=(a,b=' ')=>new Array(r(9,5)).fill().map(a).join(b)

//generates a sentence w/5-9 words (capitalizes the first character and adds a period)
a=_=>(s=g(_=>w[r(w.length)])).charAt(0).toUpperCase()+s.substr(1)+'.'

//generates paragraphs w/5-9 sentences
o.innerHTML=g(_=>g(a),'<br><br>')
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment