Skip to content

Instantly share code, notes, and snippets.

@EHfive
Last active August 8, 2017 02:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EHfive/9262e5f65546e53f6fff06fb3b0dc770 to your computer and use it in GitHub Desktop.
Save EHfive/9262e5f65546e53f6fff06fb3b0dc770 to your computer and use it in GitHub Desktop.
demo
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>PostMessage</title>
</head>
<body>
<textarea name="" id="text" cols="30" rows="10">
</textarea><br>
<button id="submit" style="margin-right: 20px">提交</button>
<button id="show">Show</button>
<div>
<iframe id="frame" src="receive.html" frameborder="0"></iframe>
</div>
<script>
var user_json = '{"unit_info":null,"deck_info":null,"removable_info":null}'
var text = document.getElementById('text')
text.value = user_json
var submit_button = document.getElementById('submit')
var show_button = document.getElementById('show')
var frame_ele = document.getElementById('frame')
submit_button.onclick = function () {
user_json = text.value
window.frames[0].postMessage(user_json, '*')
}
show_button.onclick = function () {
window.location = 'show.html'
}
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Get user_json from LLproxy</title>
</head>
<body>
<script>
function Jump2MainPage() {
location = "https://llsifteambuilder.herokuapp.com/build_team/"
}
function fetchData() {
var api_url = "https://llsif.sokka.cn/api/llproxy/unitsExportJSON/?uid=";
var args = new URLSearchParams(location.search.slice(1));
if (!args.has('uid') || ! window.XMLHttpRequest) {
Jump2MainPage()
}
api_url += args.get('uid');
// get uid from URL https://llsifteambuilder.herokuapp.com/build_team/LLproxy_user_json?uid=
var xmlhttp = new XMLHttpRequest();
if (xmlhttp) {
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
var res = JSON.parse(xmlhttp.responseText).result;
// document.write(res.uid + ' - ' + res.update_time);
localStorage.setItem('user_json', res.JSONString);
Jump2MainPage()
}
};
xmlhttp.open('GET', api_url);
xmlhttp.send();
}
}
try {
fetchData()
} catch (e) {
Jump2MainPage()
}
</script>
</body>
</html>
<!-- https://llsifteambuilder.herokuapp.com/build_team/receive_user_json -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Receive</title>
</head>
<body>
<span id="text"></span>
<script>
var text=document.getElementById('text')
window.addEventListener('message', function (e) {
text.innerHTML=e.data
localStorage.setItem('user_json', e.data)
}, true);
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Show</title>
</head>
<body>
<script>
user_json=localStorage.getItem('user_json')
document.write(user_json)
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment