Skip to content

Instantly share code, notes, and snippets.

@aTakaakiSeki
Forked from noromanba/zalgolf-js.md
Last active May 29, 2017 11:35
Show Gist options
  • Save aTakaakiSeki/7ac2df12412638efb8dc3af8d3ce5cd3 to your computer and use it in GitHub Desktop.
Save aTakaakiSeki/7ac2df12412638efb8dc3af8d3ce5cd3 to your computer and use it in GitHub Desktop.
js Zalgo Scrambled Text + slightly code-golf

Zalgolf / Re:Zalgo

js Zalgo Scrambled Text + slightly code-golf

original code and description by @aTakaakiSeki

[].concat(...[...document.all].map(e => [...e.childNodes])).filter(n => n.nodeType === Node.TEXT_NODE).map(n => n.textContent = n.textContent.replace(/([a-zA-Z])/g, (_, c) => c + [...Array(Math.floor(Math.random()*30))].map(() => String.fromCharCode(0x300 + Math.floor(Math.random()*79))).join('')))

WTFPL

Codes below are under the same license

Keep the original functionality

for(e of document.all)for(n of e.childNodes)n.nodeType^3?r=Math.random:n[t='textContent']=n[t].replace(/[a-z]/ig,c=>(f=i=>~i?f(~-i)+String.fromCharCode(768+r()*79):c)(r()*30))

With some fixes

  • There are 112 valid combining characters, not 79
  • All printable characters except space can be accompanied with combining characters
  • Text nodes are tested with its data property
  • script and style are also text nodes, so filter them out
  • Add at least one combining character for each alphanumeric character
for(e of document.all)for(n of e.childNodes)/Sc|ty/.test(e)|!n[d='data']?r=Math.random:n[d]=n[d].replace(/\w/g,c=>(f=i=>~i?f(~-i)+String.fromCharCode(768+r()*112):c)(r()*30))

Save bytes with breaking some features

  • Make the length of added combining characters more random
  • Choose from U+0310-0319,0320-0329,0330-0339,0340-0349,0350-0359,0360-0363
for(e of document.all)for(n of e.childNodes)/Sc|ty/.test(e)|!n[d='data']?0:n[d]=n[d].replace(/\w/g,c=>(f=r=>r()<.06?c:f(r)+eval(`'\\u03${63^r()*54}'`))(Math.random))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment