-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.js
29 lines (28 loc) · 932 Bytes
/
api.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
(async function(){
eval(await fetch("https://apis.google.com/js/api.js").then(res => res.text()))
console.log(gapi);
let mapping = {};
window.addEventListener('message', async event => {
const { origin, source } = event;
const { type, data } = event.data;
if (type in mapping) {
let callback = mapping[type];
source.postMessage({
type: type,
data: await callback(data)
}, origin === 'null' ? '*' : origin);
} else {
console.log('Whoops!');
}
return true;
});
function listen(messageType, callback) {
if (messageType in mapping) throw new Error("You already have that type with a callback");
mapping[messageType] = callback;
}
listen('ping', (data) => {
gapi.auth2.getAuthInstance().signIn();
return data * 2;
});
// end
})()