mirror of
https://github.com/openvk/openvk
synced 2024-12-27 11:01:28 +03:00
54 lines
1.3 KiB
JavaScript
54 lines
1.3 KiB
JavaScript
|
const VKAPI = Object.create(null);
|
||
|
|
||
|
VKAPI._makeRequest = function(type, params) {
|
||
|
return new Promise((succ, fail) => {
|
||
|
let uuid = crypto.randomUUID();
|
||
|
let request = params;
|
||
|
request["@type"] = type;
|
||
|
request.transaction = uuid;
|
||
|
|
||
|
let listener = e => {
|
||
|
if (e.source !== window.parent)
|
||
|
return;
|
||
|
|
||
|
if (e.data.transaction !== uuid)
|
||
|
return;
|
||
|
|
||
|
let resp = e.data;
|
||
|
let ok = resp.ok;
|
||
|
delete resp.transaction;
|
||
|
delete resp.ok;
|
||
|
|
||
|
(ok ? succ : fail)(resp);
|
||
|
window.removeEventListener("message", listener);
|
||
|
};
|
||
|
|
||
|
let origin = document.referrer.split("/").slice(0, 3).join("/");
|
||
|
window.addEventListener("message", listener);
|
||
|
window.parent.postMessage(request, origin);
|
||
|
});
|
||
|
}
|
||
|
|
||
|
VKAPI.getUser = function() {
|
||
|
return VKAPI._makeRequest("UserInfoRequest", {});
|
||
|
}
|
||
|
|
||
|
VKAPI.makePost = function(text) {
|
||
|
return VKAPI._makeRequest("WallPostRequest", {
|
||
|
text: text
|
||
|
});
|
||
|
}
|
||
|
|
||
|
VKAPI.execute = function(method, params) {
|
||
|
return VKAPI._makeRequest("VkApiRequest", {
|
||
|
method: method,
|
||
|
params: params
|
||
|
});
|
||
|
}
|
||
|
|
||
|
VKAPI.buy = function(price, item) {
|
||
|
return VKAPI._makeRequest("PaymentRequest", {
|
||
|
outSum: price,
|
||
|
description: item
|
||
|
});
|
||
|
}
|