From 3c97d3f39661030d220508324168db733aa4734c Mon Sep 17 00:00:00 2001 From: zigah Date: Sun, 3 Nov 2013 16:10:36 +0100 Subject: [PATCH 1/2] =?UTF-8?q?Replaced=20btoa=20and=20atob=20that=20have?= =?UTF-8?q?=20a=20problem=20with=20handling=20Unicode=20caharacters=20with?= =?UTF-8?q?=20code=20by=20Johan=20Sundstr=C3=B6m.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.html | 1 + js/b64Utils.js | 31 +++++++++++++++++++++++++++++++ js/challenge.js | 2 +- js/game.js | 2 +- 4 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 js/b64Utils.js diff --git a/index.html b/index.html index 1d862f8..20ab276 100644 --- a/index.html +++ b/index.html @@ -157,6 +157,7 @@ + diff --git a/js/b64Utils.js b/js/b64Utils.js new file mode 100644 index 0000000..98cb835 --- /dev/null +++ b/js/b64Utils.js @@ -0,0 +1,31 @@ +/** + * Created with Notepad++. + * User: Žiga Hajdukoviæ (zigah) + * Date: 2013-11-03 + * Time: 15:55 + * + * This two functions are intended to replace btoa and atob + * that have a problem with handling Unicode caharacters in + * a player's g+ name. + * + * The functions below were effectively copy pasted from ecmanaut.blogspot.com + * as suggested by MDN: https://developer.mozilla.org/en-US/docs/Web/API/window.btoa + * and written by Johan Sundström. + * + * In most browsers, calling window.btoa on a Unicode string + * will cause a Character Out Of Range exception. + * To avoid this, consider this pattern, noted by Johan Sundström: + * http://ecmanaut.blogspot.com/2006/07/encoding-decoding-utf8-in-javascript.html + * + */ +"use strict"; + +var b64Utils = b64Utils || {}; + +b64Utils.utf8_to_b64 = function(str) { + return window.btoa(unescape(encodeURIComponent( str ))); +}; + +b64Utils.b64_to_utf8 = function(str) { + return decodeURIComponent(escape(window.atob( str ))); +}; diff --git a/js/challenge.js b/js/challenge.js index 5b46055..418d4bc 100644 --- a/js/challenge.js +++ b/js/challenge.js @@ -33,7 +33,7 @@ challenge.tryToLoad = function() var challengeString = challenge.getURLParameter('gamedata'); if (challengeString && challengeString != 'null') { console.log("Received challenge string ", challengeString); - var decodedString = atob(challengeString); + var decodedString = b64Utils.b64_to_utf8(challengeString); console.log("Decoded as ", decodedString); var challengeObject = JSON.parse(decodedString); console.log("Parsed into ",challengeObject); diff --git a/js/game.js b/js/game.js index a56289f..2770ad1 100644 --- a/js/game.js +++ b/js/game.js @@ -142,7 +142,7 @@ game.populateBragButton = function() { // Let's JSONify it var challengeString = JSON.stringify(challengeObject); - challengeString = btoa(challengeString); + challengeString = b64Utils.utf8_to_b64(challengeString); var linkUrl = constants.LINK_PAGE_BASE + "?gamedata=" + challengeString; // If we wanted, we could also have a different link for the calltoaction From b08b856a22107894d038a4682dd9a0b88d0db9d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=BDiga=20Hajdukovi=C4=87?= Date: Sun, 3 Nov 2013 16:30:03 +0100 Subject: [PATCH 2/2] fixed a typo in comments. --- js/b64Utils.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/js/b64Utils.js b/js/b64Utils.js index 98cb835..ff10bb5 100644 --- a/js/b64Utils.js +++ b/js/b64Utils.js @@ -1,20 +1,20 @@ /** * Created with Notepad++. - * User: Žiga Hajdukoviæ (zigah) + * User: �iga Hajdukovi� (zigah) * Date: 2013-11-03 * Time: 15:55 * - * This two functions are intended to replace btoa and atob + * These two functions are intended to replace btoa and atob * that have a problem with handling Unicode caharacters in * a player's g+ name. * * The functions below were effectively copy pasted from ecmanaut.blogspot.com * as suggested by MDN: https://developer.mozilla.org/en-US/docs/Web/API/window.btoa - * and written by Johan Sundström. + * and written by Johan Sundstr�m. * * In most browsers, calling window.btoa on a Unicode string * will cause a Character Out Of Range exception. - * To avoid this, consider this pattern, noted by Johan Sundström: + * To avoid this, consider this pattern, noted by Johan Sundstr�m: * http://ecmanaut.blogspot.com/2006/07/encoding-decoding-utf8-in-javascript.html * */