Skip to content

Instantly share code, notes, and snippets.

@samdenty
Last active November 30, 2018 09:00
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 samdenty/fdf4bac682c632650a0d90576ddde36a to your computer and use it in GitHub Desktop.
Save samdenty/fdf4bac682c632650a0d90576ddde36a to your computer and use it in GitHub Desktop.
class Generator {
constructor(
readonly zero: string,
readonly one: string,
readonly separator: string
) {}
encode(data: string) {
return data
.split('')
.map(char => '00'.concat(char.charCodeAt(0).toString(2)).slice(-8))
.join(this.separator)
.split('0')
.join(this.zero)
.split('1')
.join(this.one)
}
decode(data: string) {
return String.fromCharCode(
...(data
.split(this.zero)
.join('0')
.split(this.one)
.join('1')
.split(this.separator)
.map(b => '0b' + b) as any)
)
}
payload(payload: string | Function) {
const data = typeof payload === 'function' ? `(${payload})()` : payload
return `setTimeout(String.fromCharCode(...'${this.encode(data)}'.replace(/${
this.zero
}|${this.one}/g,B=>+('${this.one}'==B)).split\`${
this.separator
}\`.map(b=>'0b'+b)))`
}
}
;(window as any).generator = new Generator('\u200B', '\u200C', '\u200D')
generator.payload(() => console.log('works')) // setTimeout(String.fromCharCode(...'​​‌​‌​​​‍​​‌​‌​​​‍​​‌​‌​​‌‍​​‌​​​​​‍​​‌‌‌‌​‌‍​​‌‌‌‌‌​‍​​‌​​​​​‍​‌‌​​​‌‌‍​‌‌​‌‌‌‌‍​‌‌​‌‌‌​‍​‌‌‌​​‌‌‍​‌‌​‌‌‌‌‍​‌‌​‌‌​​‍​‌‌​​‌​‌‍​​‌​‌‌‌​‍​‌‌​‌‌​​‍​‌‌​‌‌‌‌‍​‌‌​​‌‌‌‍​​‌​‌​​​‍​​‌​​‌‌‌‍​‌‌‌​‌‌‌‍​‌‌​‌‌‌‌‍​‌‌‌​​‌​‍​‌‌​‌​‌‌‍​‌‌‌​​‌‌‍​​‌​​‌‌‌‍​​‌​‌​​‌‍​​‌​‌​​‌‍​​‌​‌​​​‍​​‌​‌​​‌'.replace(/​|‌/g,B=>+('‌'==B)).split`‍`.map(b=>'0b'+b)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment