-
Notifications
You must be signed in to change notification settings - Fork 0
/
simplerXDM.js
430 lines (379 loc) · 14 KB
/
simplerXDM.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
;(function(window, undefined) {
var document = window.document,
location = window.location,
ft = window.finamtrade
if (!ft) {
window.finamtrade = ft = {}
}
if (!!ft.XDM) {
return
}
function isFunction(f) {
return typeof f == "function"
}
function isString(s) {
return typeof s == "string"
}
// function isObject(obj) {
// return obj != null && typeof obj == "object"
// }
function noop() {
}
//https://developer.mozilla.org/ru/docs/JavaScript/Reference/Global_Objects/Array/isArray
var isArray = Array.isArray
if (!isArray) {
isArray = function (o) {
return Object.prototype.toString.call(o) == "[object Array]"
}
}
var arrayFrom = Array.from
if (!arrayFrom) {
arrayFrom = function (o) {
return Array.prototype.slice.call(o)
}
}
function postMessage(target, data, origin) {
if (window.addEventListener) {
target.postMessage(data, origin)
} else {
//postMessage is synchronous in IE 8, so we wrap it in setTimeout
setTimeout(function () {
target.postMessage(data, origin)
}, 0)
}
}
function addEventListener(target, event, handler) {
if (target.addEventListener) {
target.addEventListener(event, handler, false)
} else {
target.attachEvent("on" + event, handler)
}
}
function removeEventListener(target, event, handler) {
if (target.removeEventListener) {
target.removeEventListener(event, handler, false)
} else {
target.detachEvent("on" + event, handler)
}
}
function getLocation(url) {
if (/^file:/.test(url)) {
throw "The file:// protocol is not supported"
}
var reURI = /^((http.?:)\/\/([^:\/\s]+)(:\d+)*)/, // returns groups for protocol (2), domain (3) and port (4)
m = url.toLowerCase().match(reURI),
proto = m[2], domain = m[3], port = m[4] || ""
if ((proto == "http:" && port == ":80") || (proto == "https:" && port == ":443")) {
port = ""
}
return proto + "//" + domain + port
}
function getOrigin(event) {
if (event.origin) {
// This is the HTML5 property
return getLocation(event.origin)
}
if (event.uri) {
// From earlier implementations
return getLocation(event.uri)
}
if (event.domain) {
// This is the last option and will fail if the
// origin is not using the same schema as we are
return location.protocol + "//" + event.domain
}
throw "Unable to retrieve the origin of the event"
}
function getQuery() {
var data = {},
pair,
input = location.search.substring(1).split("&"),
i = input.length
while (i--) {
pair = input[i].split("=")
data[pair[0]] = decodeURIComponent(pair[1])
}
return data
}
function appendQueryParameters(url, parameters) {
var hash = "",
hashIndex = url.indexOf("#")
if (hashIndex != -1) {
hash = url.substring(hashIndex)
url = url.substring(0, hashIndex)
}
var q = []
for (var key in parameters) {
if (parameters.hasOwnProperty(key)) {
q.push(key + "=" + encodeURIComponent(parameters[key]))
}
}
return url + (url.indexOf("?") == -1 ? "?" : "&") + q.join("&") + hash
}
function attr(elem, attrs) {
for (var prop in attrs) {
if (attrs.hasOwnProperty(prop)) {
elem.setAttribute(prop, attrs[prop])
}
}
}
function apply(destination, source) {
// var prop, member
for (var prop in source) {
if (source.hasOwnProperty(prop)) {
destination[prop] = source[prop]
// if (prop in destination) {
// member = source[prop]
// if (isObject(member)) {
// apply(destination[prop], member)
// } else {
// destination[prop] = source[prop]
// }
// } else {
// destination[prop] = source[prop]
// }
}
}
return destination
}
function createFrame(container, props) {
var frame = document.createElement("IFRAME")
if (!container) {
// This needs to be hidden like this, simply setting display:none and the like will cause failures in some browsers.
apply(frame.style, {
position: "absolute",
top: "-9999px",
// Avoid potential horizontal scrollbar
left: "0"
})
container = document.body
} else if (isString(container)) {
container = document.getElementById(container)
}
// HACK: IE cannot have the src attribute set when the frame is appended
// into the container, so we set it to "javascript:false" as a
// placeholder for now. If we left the src undefined, it would
// instead default to "about:blank", which causes SSL mixed-content
// warnings in IE6 when on an SSL parent page.
var src = props.src,
style = props.style
props.src = "javascript:false"
if (style) {
delete props.style
apply(frame.style, style)
}
frame.frameBorder = 0 //HTML4 only. https://developer.mozilla.org/en-US/docs/HTML/Element/iframe
// frame.border = 0
frame.allowTransparency = true
attr(frame, props)
container.appendChild(frame)
frame.src = src
return frame
}
function resolveUrl(url) {
var reParent = /[\-\w]+\/\.\.\//, // matches a foo/../ expression
reDoubleSlash = /([^:])\/\//g // matches // anywhere but in the protocol
// replace all // except the one in proto with /
url = url.replace(reDoubleSlash, "$1/")
// If the url is a valid url we do nothing
if (!url.match(/^(http||https):\/\//)) {
// If this is a relative path
var path = (url.substring(0, 1) === "/") ? "" : location.pathname
if (path.substring(path.length - 1) !== "/") {
path = path.substring(0, path.lastIndexOf("/") + 1)
}
url = location.protocol + "//" + location.host + path + url
}
// reduce all 'xyz/../' to just ''
while (reParent.test(url)) {
url = url.replace(reParent, "")
}
return url
}
ft.XDM = function (config) {
function onMessage(event) {
var origin = getOrigin(event)
if (origin == targetOrigin && event.data.substring(0, channel.length + 1) == channel + " ") {
incoming(event.data.substring(channel.length + 1))
}
}
function waitForReady(event) {
if (event.data == channel + "-ready") {
// replace the eventlistener
callerWindow = ("postMessage" in frame.contentWindow) ? frame.contentWindow : frame.contentWindow.document
removeEventListener(window, "message", waitForReady)
addEventListener(window, "message", onMessage)
if (isFunction(config.onReady)) {
config.onReady()
}
}
}
function incoming(message) {
var data = JSON.parse(message)
if (data.method) {
// A method call from the remote end
executeLocalMethod(data.id, localMethods[data.method], data.params)
} else {
// A method response from the other end
var callback = callbacks[data.id]
if (data.error) {
callback.error && callback.error(data.error)
} else if (callback.success) {
callback.success(data.result)
}
delete callbacks[data.id]
}
}
function outgoing(data) {
postMessage(callerWindow, channel + " " + JSON.stringify(data), targetOrigin)
}
function createRemoteMethod(method) {
return function () {
var args = arrayFrom(arguments),
length = args.length,
callback,
message = {
method:method
}
if (length > 0 && isFunction(args[length - 1])) {
//with callback, procedure
if (length > 1 && isFunction(args[length - 2])) {
// two callbacks, success and error
callback = {
success:args[length - 2],
error:args[length - 1]
}
message.params = args.slice(0, length - 2)
} else {
// single callback, success
callback = {
success:args[length - 1]
}
message.params = args.slice(0, length - 1)
}
callbacks['' + (++callbackCounter)] = callback
message.id = callbackCounter
} else {
// no callbacks, a notification
message.params = args
}
// Send the method request
outgoing(message)
}
}
function executeLocalMethod(id, fn, params) {
if (!fn) {
if (id) {
outgoing({
id:id,
error:{
code:-32601,
message:"Procedure not found."
}
})
}
return
}
var success,
error
if (id) {
success = function (result) {
success = noop
outgoing({
id:id,
result:result
})
}
error = function (message, data) {
error = noop
var msg = {
id:id,
error:{
code:-32099,
message:message
}
}
if (data) {
msg.error.data = data
}
outgoing(msg)
}
} else {
success = error = noop
}
// Call local method
if (!isArray(params)) {
params = [params]
}
try {
var result = fn.apply(self, params.concat([success, error]))
if (result !== undefined) {
success(result)
}
} catch (e) {
error(e.message)
}
}
function extendRpcMethods(remote, local) {
apply(localMethods, local)
apply(remoteMethods, remote)
for (var name in remote) {
if (remote.hasOwnProperty(name)) {
self[name] = createRemoteMethod(name)
}
}
}
function destroy() {
for (var i = 0; i < remoteMethods.length; i++) {
var method = remoteMethods[i]
delete self[method]
}
removeEventListener(window, "message", waitForReady)
removeEventListener(window, "message", onMessage)
if (frame) {
frame.parentNode.removeChild(frame)
self.frame = frame = null
}
}
var self = this,
callbackCounter = 0,
callbacks = {},
callerWindow,
channel,
targetOrigin, // the domain to communicate with
localMethods = {},
remoteMethods = {}
extendRpcMethods(config.remote, config.local)
if (config.url) {
var url = config.url,
frame
channel = config.channel || ("default_" + Math.floor(Math.random() * 1000000)) // randomize the initial id in case of multiple closures loaded
targetOrigin = getLocation(url)
// add the event handler for listening
addEventListener(window, "message", waitForReady)
// set up the iframe
url = resolveUrl(url)
var parameters = apply({
xdm_e:getLocation(location.href),
xdm_c:channel
}, config.params || {}),
props = apply({
src:appendQueryParameters(url, parameters),
id:"ft_xdm_provider_" + channel
}, config.props || {})
self.frame = frame = createFrame(config.container, props)
self.extendRpcMethods = extendRpcMethods
self.destroy = destroy
} else {
var parent = window.parent,
query = getQuery(),
remoteUrl = query.xdm_e.replace(/["'<>\\]/g, "")
channel = query.xdm_c.replace(/["'<>\\]/g, "") // anti-XSS protection
targetOrigin = getLocation(remoteUrl)
callerWindow = ("postMessage" in parent) ? parent : parent.document // the window that we will call with, parent.document used in opera
addEventListener(window, "message", onMessage)
postMessage(callerWindow, channel + "-ready", targetOrigin)
}
return self
}
})(window)