Skip to content
This repository has been archived by the owner on Sep 11, 2019. It is now read-only.

Replaced btoa and atob that have a problem with handling Unicode characters with code by Johan Sundström. #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@
<script src="js/login.js"></script>
<script src="js/player.js"></script>
<script src="js/utils.js"></script>
<script src="js/b64Utils.js"></script>
<script src="js/welcome.js"></script>

<!-- Start the process of loading the Google JavaScript client library -->
Expand Down
31 changes: 31 additions & 0 deletions js/b64Utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Created with Notepad++.
* User: �iga Hajdukovi� (zigah)
* Date: 2013-11-03
* Time: 15:55
*
* 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.
*
* 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 )));
};
2 changes: 1 addition & 1 deletion js/challenge.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion js/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down