forked from smogon/pokemon-showdown-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crossprotocol.html
61 lines (54 loc) · 1.41 KB
/
crossprotocol.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<!DOCTYPE html>
<meta charset="utf-8" />
<script src="/js/lib/jquery-2.1.4.min.js"></script>
<script>
var yourOrigin = 'http://play.pokemonshowdown.com';
var myOrigin = 'https://play.pokemonshowdown.com';
function postMessage(message) {
return window.parent.postMessage(message, yourOrigin);
}
function messageHandler(e) {
if (e.origin !== yourOrigin) return;
var data = e.data;
// data's first char:
// T: store teams
// P: store prefs
// R: GET request
// S: POST request
switch (data.charAt(0)) {
case 'T':
localStorage.setItem('showdown_teams', data.substr(1));
break;
case 'P':
localStorage.setItem('showdown_prefs', data.substr(1));
break;
case 'R':
case 'S':
var rq = JSON.parse(data.substr(1));
$[(data.charAt(0) === 'R' ? 'get' : 'post')](
rq[0],
rq[1],
function(ajaxdata) {
postMessage('r' + JSON.stringify([rq[2], ajaxdata]));
},
rq[3]
);
break;
}
}
window.addEventListener('message', messageHandler);
var testVal = '' + Date.now();
try {
localStorage.setItem('showdown_allow3p', testVal);
} catch (err) {}
if (localStorage.getItem('showdown_allow3p') === testVal) {
postMessage('a1');
postMessage('p' + localStorage.getItem('showdown_prefs'));
postMessage('t' + localStorage.getItem('showdown_teams'));
} else {
postMessage('a0');
}
if (location.protocol + '//' + location.hostname !== myOrigin) {
// This happens sometimes, but we'll pretend it doesn't
}
</script>