mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-20 01:03:23 +03:00
399219b6ea
* Created initial layer0 sdk * Updated layer1 to use layer0 * Updated layer2 to accept and pass blogUrl to layer1 * Updated theme-dropin to pass blogUrl to layer2 * Updated yarn.lock
44 lines
1.0 KiB
JavaScript
44 lines
1.0 KiB
JavaScript
/* globals window */
|
|
module.exports = function layer0(frame) {
|
|
var getuid = (function (i) {
|
|
return function () {
|
|
return i += 1;
|
|
};
|
|
})(1);
|
|
var origin = new URL(frame.getAttribute('src')).origin;
|
|
var handlers = {};
|
|
var listener = function () {};
|
|
|
|
window.addEventListener('message', function (event) {
|
|
if (event.origin !== origin) {
|
|
return;
|
|
}
|
|
if (!event.data || !event.data.uid) {
|
|
return;
|
|
}
|
|
if (event.data.event) {
|
|
return listener(event.data);
|
|
}
|
|
|
|
var handler = handlers[event.data.uid];
|
|
if (!handler) {
|
|
return;
|
|
}
|
|
delete handlers[event.data.uid];
|
|
handler(event.data.error, event.data.data);
|
|
});
|
|
|
|
function call(method, options, cb) {
|
|
var uid = getuid();
|
|
var data = {uid, method, options};
|
|
handlers[uid] = cb;
|
|
frame.contentWindow.postMessage(data, origin);
|
|
}
|
|
|
|
function listen(fn) {
|
|
listener = fn;
|
|
}
|
|
|
|
return {call, listen};
|
|
};
|