Skip to content

Commit

Permalink
Fix cross-origin postMessage usage in Chrome Canary
Browse files Browse the repository at this point in the history
  • Loading branch information
Slayer95 committed Mar 19, 2016
1 parent 898debc commit f74584f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
16 changes: 8 additions & 8 deletions crossdomain.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@
var yourOrigin = <?php echo json_encode('http://' . $host) ?>;
var myOrigin = 'https://play.pokemonshowdown.com';

function postMessage (message) {
if (window.parent.postMessage === postMessage) return;
function postReply (message) {
if (window.parent.postMessage === postReply) return;
return window.parent.postMessage(message, yourOrigin);
}
function messageHandler(e) {
Expand Down Expand Up @@ -132,7 +132,7 @@ function messageHandler(e) {
url: rq[0],
data: rq[1],
success: function(ajaxdata) {
postMessage('r' + JSON.stringify([rq[2], ajaxdata]));
postReply('r' + JSON.stringify([rq[2], ajaxdata]));
},
dataType: rq[3]
});
Expand All @@ -141,21 +141,21 @@ function messageHandler(e) {
}

window.addEventListener('message', messageHandler);
if (config.host !== 'showdown') postMessage('c' + config);
if (config.host !== 'showdown') postReply('c' + config);
var storageAvailable = false;
try {
var testVal = '' + Date.now();
localStorage.setItem('showdown_allow3p', testVal);
if (localStorage.getItem('showdown_allow3p') === testVal) {
postMessage('a1');
postMessage('p' + localStorage.getItem('showdown_prefs'));
postMessage('t' + localStorage.getItem('showdown_teams'));
postReply('a1');
postReply('p' + localStorage.getItem('showdown_prefs'));
postReply('t' + localStorage.getItem('showdown_teams'));
storageAvailable = true;
}
} catch (err) {}

if (!storageAvailable) {
postMessage('a0');
postReply('a0');
}

if (location.protocol + '//' + location.hostname !== myOrigin) {
Expand Down
12 changes: 6 additions & 6 deletions crossprotocol.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
var yourOrigin = 'http://play.pokemonshowdown.com';
var myOrigin = 'https://play.pokemonshowdown.com';

function postMessage(message) {
function postReply(message) {
return window.parent.postMessage(message, yourOrigin);
}
function messageHandler(e) {
Expand Down Expand Up @@ -37,7 +37,7 @@
rq[0],
rq[1],
function(ajaxdata) {
postMessage('r' + JSON.stringify([rq[2], ajaxdata]));
postReply('r' + JSON.stringify([rq[2], ajaxdata]));
},
rq[3]
);
Expand All @@ -51,15 +51,15 @@
var testVal = '' + Date.now();
localStorage.setItem('showdown_allow3p', testVal);
if (localStorage.getItem('showdown_allow3p') === testVal) {
postMessage('a1');
postMessage('p' + localStorage.getItem('showdown_prefs'));
postMessage('t' + localStorage.getItem('showdown_teams'));
postReply('a1');
postReply('p' + localStorage.getItem('showdown_prefs'));
postReply('t' + localStorage.getItem('showdown_teams'));
storageAvailable = true;
}
} catch (err) {}

if (!storageAvailable) {
postMessage('a0');
postReply('a0');
}

if (location.protocol + '//' + location.hostname !== myOrigin) {
Expand Down

0 comments on commit f74584f

Please sign in to comment.