Skip to content

Instantly share code, notes, and snippets.

@xem
Forked from 140bytes/LICENSE.txt
Last active June 16, 2021 00:05
Show Gist options
  • Save xem/7086007 to your computer and use it in GitHub Desktop.
Save xem/7086007 to your computer and use it in GitHub Desktop.
Challenge: Executing more than 140 JS characters in a tweet!

Hello

This fork isn't really a 140bytes entry, but a challenge!

What if a tweet (140 utf-16 characters) could execute more than 140 characters of JavaScript code?

190 chars:

thanks to subzey's help, a tweet can execute 190 js chars.

Encoder:

z=function(b,c,a,f){c="";f=String.fromCharCode;for(a=0;190>a;a+=2)c+=f(55296+b.charCodeAt(a))+f(56320+b.charCodeAt(a+1));return c}

Decoder / Executer (140 chars): (replace xxxx with your encoded JS string)

eval(unescape(escape("xxxx").replace(/uD./g,'')))

Demo:

http://jsfiddle.net/99GVk/1/

// Encoder:
// @param b: the input code (198 chars)
z = function(b, c, a, f){
// The encoded string begins with "'"
c = "";
// Shortcut
f = String.fromCharCode;
// Loop through the input code
for(a = 0; 190 > a; a += 2){
// Create and append an UTF-16 surrogate pair, containing the ASCII codes at b[a] and b[a+1] in each half's last 7 bits
c += f(55296 + b.charCodeAt(a)) + f(56320 + b.charCodeAt(a + 1));
}
// return the encoded string
return c
}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
@atk
Copy link

atk commented Oct 22, 2013

Alas, this leaks a and i into the global scopes, which is a violation of our rules. Still, it's a pretty interesting feat.

@xem
Copy link
Author

xem commented Oct 23, 2013

Yeah, so, bonus points for those who manage to avoid leaks.

I tried, but my non-leaking version can only "encrypt" 110 characters:

eval("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx".replace(/./g,function(a){return String.fromCharCode((a.charCodeAt(0)&255))}))

@subzey
Copy link

subzey commented Nov 7, 2013

95 × 2 = 190 bytes:

eval(unescape(escape("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx").replace(/uD./g,'')))

Github doesn't allow me to post the comment with encoded string, but I actually tried to tweet that, it works :-)

@xem
Copy link
Author

xem commented Nov 12, 2013

subzey, you're a genius! Thanks a lot and congrats!
I actually tried to use escapes and unescapes too, but it didn't work.
Turns out, it was just the .replace(/uD./g,'') that was missing!

I made a demo for your solution: http://jsfiddle.net/99GVk/1/ - with a dedicated encoder (128 chars)

@xem
Copy link
Author

xem commented Dec 16, 2013

@subzey, I made it to 198!

eval(escape("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx").replace(/%.../g,'\\u00'))

@subzey
Copy link

subzey commented Dec 17, 2013

Uh… Very nice try, but unfortunately, there's an error.

I tried the following:
alert("It works!");;;;;/* … more semicolons fills up to 198 chars*/
and after running the generated code got SyntaxError.

In short, var \u0066\u006f\u006f = 42 would work, but var \u0066\u006f\u006f \u003d 42 wouldn't. Please see http://es5.github.io/#x7.6 for details.

@xem
Copy link
Author

xem commented Dec 18, 2013

oops indeed I should have tested it better... :(

@Cycymomo
Copy link

Hi there !

My 2 cents : (2 chars less)

z=function(b,c,a){f=String.fromCharCode;for(a=c='';190>a;++a)c+=f(55296+b.charCodeAt(a))+f(56320+b.charCodeAt(a+=1));return c}

@xem
Copy link
Author

xem commented Apr 15, 2014

Thanks nick! (warning, your "f" leaks in the global scope)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment