From: Anders Kaseorg Date: Fri, 13 Mar 2015 04:03:26 +0000 (-0400) Subject: Remove quadratic-complexity keylogger from Konami easter egg X-Git-Tag: 0.1.44~1 X-Git-Url: http://xvm.mit.edu/gitweb/invirt/packages/invirt-web.git/commitdiff_plain/2c11b1e0044eb9f74912f6cbd1b4c6bd3d50d294 Remove quadratic-complexity keylogger from Konami easter egg Replace it with the obvious 10-state Knuth–Morris–Pratt FSM. Signed-off-by: Anders Kaseorg --- diff --git a/code/static/nc.js b/code/static/nc.js index 4ea168f..b82ed8b 100644 --- a/code/static/nc.js +++ b/code/static/nc.js @@ -1,9 +1,12 @@ -var kkeys = [], konami = "38,38,40,40,37,39,37,39,66,65"; +var konami = [38, 38, 40, 40, 37, 39, 37, 39, 66, 65], + konami_jump = [-1, 0, 1, 0, 0, 0, 0, 0, 0, 0], + konami_state = 0; document.onkeydown = function (e) { - kkeys.push(e.keyCode); - var idx = kkeys.toString().indexOf( konami ); - if (idx >= 0 && idx != kkeys.toString().length - konami.length) { - kkeys = []; - window.location.assign("http://nyan.cat/"); + while (konami_state != -1 && e.keyCode != konami[konami_state]) { + konami_state = konami_jump[konami_state]; + } + if (++konami_state == konami.length) { + window.location.assign("http://nyan.cat/"); + konami_state = 0; } };