This repository has been archived by the owner on Nov 21, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
nspp.js
55 lines (55 loc) · 1.77 KB
/
nspp.js
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
function getNSPPAPI() {
var api = {};
var iframe = document.getElementById("nspp_api_iframe");
api._user = null;
api._auth = null;
if (typeof iframe === "undefined" || iframe === null) {
iframe = document.createElement("iframe");
iframe.id = "nspp_api_iframe";
iframe.src = "https://nationstatesplusplus.net/api.html";
iframe.style.display = "none";
document.body.appendChild(iframe);
}
api.getUserDetails = function(callback) {
if (this._user == null) {
var api = this;
var receiveMessage = function(event) {
console.log("logging message received at website: " + event.data);
console.log(event);
if (event.data.user != null) {
api._user = event.data.user;
api._auth = event.data.auth;
window.removeEventListener("message", receiveMessage);
if (typeof callback != "undefined" && callback != null) {
callback(api._user, api._auth);
}
}
};
window.addEventListener("message", receiveMessage, false);
var checkIframe = function(api) {
if (api._user == null) {
console.log("Checking iframe");
var iframe = document.getElementById('nspp_api_iframe');
iframe.contentWindow.postMessage('identify_user','*');
setTimeout(checkIframe, 100, api);
}
};
checkIframe(this);
} else if (typeof callback != "undefined" && callback != null) {
callback(this._user, this._auth);
}
};
api.refreshUser = function() {
this._user = null;
this._auth = null;
var iframe = document.getElementById("nspp_api_iframe");
iframe.parentNode.removeChild(iframe);
iframe = document.createElement("iframe");
iframe.id = "nspp_api_iframe";
iframe.src = "https://nationstatesplusplus.net/api.html";
iframe.style.display = "none";
document.body.appendChild(iframe);
getUserDetails();
}
return api;
}