forked from taylorhakes/postmessage-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPMPlus.js
234 lines (214 loc) · 6.66 KB
/
PMPlus.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
(function (global, undefined) {
'use strict';
/**
* Send and Receive message from other windows
* @param [options] {object}
* @param [options.listenDomain] {string|Array} Domain(s) to listen to messages from
* @param [options.sendDomain] {string} Default send domain. Can specify message specific domain in `send` method
* @param [options.timeout] {number} Milliseconds to wait for a response when sending a message
* @constructor
*/
function PMPlus(options) {
options = options || {};
if (options.listenDomain && Object.prototype.toString.call(options.listenDomain) !== '[object Array]') {
options.listenDomain = [options.listenDomain];
}
this._timeout = options.timeout || 3000;
this._listenTimeout = options.listenTimeout || 100;
this._sendD = options.sendDomain;
this._listenD = options.listenDomain || [];
this._listeners = {};
this._rListeners = {};
}
/**
* Send a message to another window
* @param params.w {Window} Window to send message
* @param params.channel {string} Channel to send the message
* @param params.data {*} Data to send
* @param [params.domain] {string} Override default send domain
* @param [params.callback] {Function} Callback to be executed when other window responds
* Has the form: function(success, data) { }
*/
PMPlus.prototype.send = function (params) {
var info = {
id: Math.random(),
channel: params.channel,
data: params.data,
type: 'message'
}, me = this, domain = params.domain || this._sendD, timeout, listenTimeout;
if (!this._sendD && !params.domain) {
throw new Error('Must specify domain to send message. Default in Constructor or message specific.');
}
if (!this._rListeners[params.channel]) {
this._rListeners[params.channel] = {};
}
addEvent.call(this);
if (!this._rListeners[params.channel][info.id] && params.callback) {
timeout = setTimeout(function () {
clearTimeout(listenTimeout);
params.callback(false, 'Timeout');
delete me._rListeners[params.channel][info.id];
}, this._timeout);
listenTimeout = setTimeout(function () {
clearTimeout(timeout);
params.callback(false, 'No listener');
delete me._rListeners[params.channel][info.id];
}, this._listenTimeout);
this._rListeners[params.channel][info.id] = {
timeout: timeout,
listenTimeout: listenTimeout,
callback: params.callback,
domain: domain
};
}
sendMessage(params.w, info, domain);
};
/**
* Listen to a specific channel
* @param channel {string} Channel to listen
* @param callback {Function} Function to call when someone sends a message on the channel
* has the form: function(data {*}, respond {Function}) { ... }
*/
PMPlus.prototype.listen = function (channel, callback) {
if(!this._listenD.length) throw new Error('Not listening to any domains. Specify domains in Constructor.');
addEvent.call(this);
if (!this._listeners[channel]) {
this._listeners[channel] = [];
}
this._listeners[channel].push(callback);
};
/**
* Remove all events and stop listening to messages
* @type {Function}
*/
PMPlus.prototype.destroy = removeEvent;
PMPlus.prototype._onMessage = function (e) {
var info, response, update, k, len, keys, i, onRespond,
responded = false,
w = e.source,
origin = e.origin,
isListen = checkListenDomain.call(this, origin),
isResponse = checkResponseDomain.call(this, origin);
// Ignore messages from bad domains
if (!isListen && !isResponse) {
return;
}
try {
info = JSON.parse(e.data);
} catch (error) {
// Some other way of sending data, ignore
return;
}
if (info.type === 'message' && isListen) {
onRespond = function (success, data) {
// Don't respond twice
if (responded) return;
responded = true;
response.success = success;
response.data = data;
sendMessage(w, response, origin);
};
response = {
id: info.id,
channel: info.channel,
type: 'response',
success: true
};
if (!this._listeners[info.channel]) {
onRespond(false, 'Invalid Channel: `' + info.channel + '`');
return;
}
for (i = 0, len = this._listeners[info.channel].length; i < len; i++) {
try {
this._listeners[info.channel][i](info.data, onRespond);
// Update the sender to say their message was received and is processing
if(!responded) {
update = {
id: info.id,
channel: info.channel,
type: 'listening'
};
sendMessage(w, update, origin);
}
} catch (error) {
onRespond(false, 'Unknown Error');
}
}
} else if(this._rListeners[info.channel] && this._rListeners[info.channel][info.id] && this._rListeners[info.channel][info.id].domain === origin) {
if(info.type === 'listening') {
clearTimeout(this._rListeners[info.channel][info.id].listenTimeout);
} else if(info.type === 'response') {
clearAllTimeouts(this._rListeners[info.channel][info.id]);
this._rListeners[info.channel][info.id].callback(info.success, info.data);
delete this._rListeners[info.channel][info.id];
keys = false;
for (k in this._rListeners[info.channel]) {
keys = true;
break;
}
if (!keys) delete this._rListeners[info.channel];
}
}
};
function sendMessage(w, info, domain) {
w.postMessage(JSON.stringify(info), domain);
}
function clearAllTimeouts(obj) {
clearTimeout(obj.listenTimeout);
clearTimeout(obj.timeout);
}
function checkListenDomain(domain) {
/*jshint validthis: true */
for (var i = 0, len = this._listenD.length; i < len; i++) {
if (domain === this._listenD[i] ||
(this._listenD[i] instanceof RegExp && domain.match(this._listenD[i]))) {
return true;
}
}
return false;
}
function checkResponseDomain(domain) {
/*jshint validthis: true */
if (this._sendD === domain) return true;
for (var channel in this._rListeners) {
for (var message in this._rListeners[channel]) {
if (this._rListeners[channel][message].domain === domain) {
return true;
}
}
}
return false;
}
function addEvent() {
/*jshint validthis: true */
var me = this;
if (this._eventFn) {
return;
}
this._eventFn = function (e) {
me._onMessage(e);
};
if (window.addEventListener) {
window.addEventListener('message', this._eventFn);
} else {
window.attachEvent('onmessage', this._eventFn);
}
}
function removeEvent() {
/*jshint validthis: true */
if (!this._eventFn) {
return;
}
if (window.removeEventListener) {
window.removeEventListener('message', this._eventFn);
} else {
window.detachEvent('onmessage', this._eventFn);
}
this._eventFn = undefined;
}
if(typeof module !== 'undefined' && module.exports) {
module.exports = PMPlus;
} else {
global.PMPlus = PMPlus;
}
})(this, void 0);