-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblocksdk.js
130 lines (114 loc) · 3.23 KB
/
blocksdk.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* Copyright (c) 2018, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
var SDK = function (whitelistOverride, sslOverride) {
// the custom block should verify it is being called from
// the marketing cloud
this._validateOrigin = function (origin) {
// Make sure to escape periods since these strings are used in a regular expression
var allowedDomains = whitelistOverride || ['marketingcloudapps\\.com', 'blocktester\\.herokuapp\\.com'];
for (var i = 0; i < allowedDomains.length; i++) {
// Makes the s optional in https
var optionalSsl = sslOverride ? '?' : '';
var whitelistRegex = new RegExp('^https' + optionalSsl + '://([a-zA-Z0-9-]+\\.)*' + allowedDomains[i] + '(:[0-9]+)?$', 'i');
if (whitelistRegex.test(origin)) {
return true;
}
}
return false;
};
this._messageId = 1;
this._messages = {
0: function () {}
};
this._receiveMessage = function (message) {
message = message || {};
var data = message.data || {};
if (data.method === 'handShake') {
if (this._validateOrigin(data.origin)) {
this._parentOrigin = data.origin;
return;
}
}
// if the message is not from the validated origin it gets ignored
if (!this._parentOrigin || this._parentOrigin !== message.origin) {
return;
}
// when the message has been received, we execute its callback
(this._messages[data.id || 0] || function () {})(data.payload);
delete this._messages[data.id];
};
window.addEventListener('message', this._receiveMessage.bind(this), false);
this._postToEditor = function (payload, callback, ttl) {
var self = this;
// we only message up if we have
// validated the origin
if (!this._parentOrigin) {
if (ttl === undefined || ttl > 0) {
window.setTimeout(function () {
self._postToEditor(payload, callback, (ttl || 5) - 1);
}, 20);
}
return;
}
this._messages[this._messageId] = callback;
payload.id = this._messageId;
this._messageId += 1;
// the actual postMessage always uses
// the validated origin
window.parent.postMessage(payload, this._parentOrigin);
};
this.getContent = function (cb) {
this._postToEditor({
method: 'getContent'
}, cb);
};
this.setContent = function (content, cb) {
this._postToEditor({
method: 'setContent',
payload: content
}, cb);
};
this.setSuperContent = function (content, cb) {
this._postToEditor({
method: 'setSuperContent',
payload: content
}, cb);
};
this.getData = function (cb) {
this._postToEditor({
method: 'getData'
}, cb);
};
this.setData = function (dataObj, cb) {
this._postToEditor({
method: 'setData',
payload: dataObj
}, cb);
};
this.getCentralData = function (cb) {
this._postToEditor({
method: 'getCentralData'
}, cb);
};
this.setCentralData = function (dataObj, cb) {
this._postToEditor({
method: 'setCentralData',
payload: dataObj
}, cb);
};
window.parent.postMessage({
method: 'handShake',
origin: window.location.origin
}, '*');
};
if (typeof(window) === 'object') {
window.sfdc = window.sfdc || {};
window.sfdc.BlockSDK = SDK;
}
if (typeof(module) === 'object') {
module.exports = SDK;
}