-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrossDomain-rc.js
507 lines (480 loc) · 15.2 KB
/
crossDomain-rc.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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
(function(win, doc, undefined) {
var slice = Array.prototype.slice;
var urlReg = /^[^:]+:\/*[^\/]+/;
var messageSport = !!win.postMessage ? 'postMessage' : 'name';
var hasOwn = Object.prototype.hasOwnProperty;
var emptyFun = function () {};
//============================公共函数===========================
var util = {
addEvent: doc.attachEvent ? function(element, event, fn) {
element.attachEvent('on' + event, fn);
} : function(element, event, fn) {
element.addEventListener(event, fn, false);
},
isSameDomain: function (urlA, urlB) {//简单写的,可能有误
var strA = urlA.match(urlReg)[0].replace(/:80$/,'').replace(/:\/+/,''),strB = urlB.match(urlReg)[0].replace(/:80$/,'').replace(/:\/+/,'');
return strA == strB;
},
/**
* win动态添加ifrme结点,设置url,载入后回调
* @param {window} win window对象
* @param {String} url ifrme地址
* @param {Function} callback?ifrme加载完成,回调方法
* @return {Node} ifrme结点
*/
iframeLoad : function(win, url, callback) {
var doc = win.document;
var frame = doc.createElement("iframe");
if(!!url) {
frame.src = url;
}
frame.style.cssText = "position:absolute;left:-2000px;width:1px;height:1px;";
if(!!callback) {
util.addEvent(frame, 'load', callback);
}
doc.body.appendChild(frame);
return frame;
},
/**
* 对象obj的name方法重写,增加setReady方法
* 在调用setReady之前,调用函数,存储参数入list,
* 在调用setReady时,批量执行list存储的参数
* 在调用setReady之后,调用函数,立即执行,
* @param {Object} obj 对象
* @param {String} name 方法的对应属性名字
* @return {Node} ifrme结点
*/
runReady : function(obj, name) {
var fun = obj[name];
var _ready = false;
var _list = [];
obj[name] = function() {
if(_ready == false) {
_list.push(arguments);
} else {
fun.apply(obj, arguments);
}
};
obj[name].setReady = function() {
if(_ready == false) {
_ready = true;
while(_list.length > 0) {
fun.apply(obj, _list.shift());
}
}
};
}
};
if (!win.getCrossDomain) {
//============================通信对象===========================
//底层通信对象
var uuid = 0;
var prefixMap = {};
var prefixManage = {
set: function (oldPrefix, newPrefix) {
if (!!oldPrefix&&prefixMap[oldPrefix]) {
prefixMap[oldPrefix]--;
}
prefixMap[newPrefix] = prefixMap[newPrefix] || 0;
prefixMap[newPrefix]++;
},
isUnique: function (prefix) {
return prefixMap[prefix] ==1;
},
getNew: function (oldPrefix) {
var prefix = oldPrefix;
while (prefixMap[prefix] > 0) {
uuid++;
prefix = '_'+uuid+'_'+oldPrefix;
}
return prefix;
}
};
function message() {
var hash = '';
var message = {
sport : messageSport,
send : messageSport == 'name'? function(data) {//send(编码)
this.proxy.name = data;
}: function(data) {
this.proxy.postMessage(data, '*');
},
get : function(data) {//get(解码),绑定该get
this.child.get(data);
},
//监听指定window的message事件,或name的改变
bindMessage: function (win) {
if (messageSport == 'postMessage') {
util.addEvent(win,'message', function (e) {
message.get(e.data);
})
} else {
hash = win.name;//忽略页面当前的name值
setInterval(function() {
if(win.name != hash) {
hash = win.name;
message.get(hash);
}
}, 50);
}
},
//设置方法或属性,通过原型继承读取
set: function (name, value) {
if( typeof name == 'string') {
if (name == 'prefix') {
prefixManage.set(this.prefix, value);
}
message[name] = value;
} else {
for (var i in name) {
if(hasOwn.call(name, i)) {
this.set(i, name[i]);
}
}
}
}
};
//默认多参数支持方式,需要JSON支持,需要自行设置,见example
message.set({
params2str: function () {
return slice.call(arguments).join('<{PA}>');
},
str2params: function (str) {
return str.split('<{PA}>');
}
});
return message;
};
//修正IE,使IE支持多消息的支持
function ieMessage (message, ieSeparator) {
ieSeparator || (ieSeparator = ['<{IE}>', '{<IE>}']);
var sendMap = {};//主动发出的消息
var cancelMap = {};//正在处理中的数据,通知不需要再传送
function push(ary, obj) {//obj的值,添加到ary数组中
for(var i in obj) {
if(hasOwn.call(obj, i)) {
ary.push(obj[i]);
}
}
return ary;
}
function postStr(separator) {
var ary = [];
push(ary, sendMap);
push(ary, cancelMap);
return ary.join(separator);
}
var fun = function () {};
fun.prototype = message;
var resultObj = new fun();
message.child = resultObj;
resultObj.set('ieSeparator', ieSeparator);
resultObj.send = function (opt, uid) {
// 12345
//=>S<{IE}>0<{IE}>12345
// 67890
//=>S<{IE}>0<{IE}>12345{<IE>}S<{IE}>1<{IE}>67890
// C,0
//=>S<{IE}>0<{IE}>12345{<IE>}S<{IE}>1<{IE}>67890{<IE>}C<{IE}>0
var data;
var ieSeparator = this.ieSeparator;
if (opt == 'C' && !!uid){//发送“取消发送”的消息
cancelMap[uid] = [opt,uid].join(ieSeparator[0]);
} else {
data = opt;
uid = uuid++;
sendMap[uid] = ['S',uid,data].join(ieSeparator[0]);
}
message.send(postStr(ieSeparator[1]));
};
resultObj.get = function (data) {
// S<{IE}>0<{IE}>12345{<IE>}S<{IE}>1<{IE}>67890{<IE>}C<{IE}>0
//=>S<{IE}>0<{IE}>12345
//=>S<{IE}>1<{IE}>67890
var ieSeparator = this.ieSeparator;
var datas = data.split(ieSeparator[1]), map = {};
var params, opt, uid;
for(var i = 0, len = datas.length; i < len; i++) {
params = datas[i].split(ieSeparator[0]);
opt = params.shift();
uid = params.shift();
data = params.join(ieSeparator[0]);
if(opt == 'S') {
map[uid] = 1;
if (!cancelMap[uid]) { //未处理过,进行处理
this.send('C', uid);
this.child.get(data);
}
} else if(opt == 'C'){
delete sendMap[uid];//消息来源端 接收到C命令,则下次不在传送
}
}
for (var i in cancelMap) {
if(hasOwn.call(cancelMap, i) && !hasOwn.call(map, i)) {
delete cancelMap[i];//没有传过来,表示C命令消息来源端已执行
}
}
};
return resultObj;
}
//消息前缀
function prefixMessage (message, prefix) {
var fun = function () {};
fun.prototype = message;
var resultObj = new fun();
message.child = resultObj;
resultObj.set('prefix', prefix||'_crossD_');//多个使用默认存在问题,建议clientUrl MD5签名
resultObj.send = function (data) {
message.send(this.prefix+data);
};
resultObj.get = function (str) {
if (!!str && str.indexOf(this.prefix) == 0) {//解码成功,且前缀为prefix,对消息进行了过滤
this.child.get(str.substr(this.prefix.length));
}
};
return resultObj;
}
//回调机制的支持,callback进行缓存,对应消息返回时,对消息内容执行对应的callback
function callbackMessage (message, cbSeparator) {
cbSeparator || (cbSeparator = '<{CB}>');
var _callbackMap = {};
var fun = function () {};
fun.prototype = message;
var resultObj = new fun();
message.child = resultObj;
resultObj.set('cbSeparator', cbSeparator);
resultObj.send = function (opt, uid, data) {
// '123456', fn
// =>'S<{CB}>1<{CB}>1<{CB}>123456'
// '123456'
// =>'S<{CB}>1<{CB}>0<{CB}>123456'
// 'B',2,'abcde'
// =>'B<{CB}>2<{CB}>abcde'
var callback, needCallback = 0;
var cbSeparator = this.cbSeparator;
if (opt == 'B' && !!data) {
message.send(['B',uid,data].join(cbSeparator));
} else {
callback = uid;
data = opt;
uid = uuid++;
if (!!callback) {
needCallback = 1;
_callbackMap[uid] = callback;//缓存回调方法
}
message.send(['S',uid,needCallback,data].join(cbSeparator));
}
};
resultObj.get = function (str) {
// 'S<{CB}>1<{CB}>1<{CB}>123456'
//=>123456,function send('B',1, resultStr);
// 'B<{CB}>2<{CB}>abcde'
//=>回调(abcde)
var cbSeparator = this.cbSeparator;
var params = str.split(cbSeparator), callback;
var needCallback, data;
var opt = params.shift(), uid = params.shift();
if(opt == 'S') {
needCallback = params.shift();
data = params.join(cbSeparator);
if (needCallback == 1) {
callback = function () {
resultObj.send('B',uid, message.params2str.apply(message, arguments));
}
} else {
callback = emptyFun;
}
this.child.get(data, callback);
} else {
//回调执行 结果
data = params.join(cbSeparator);
if (!!_callbackMap[uid]) {//多个情况,不一定是存在的
_callbackMap[uid].apply(null, message.str2params(data));
delete _callbackMap[uid];
}
}
};
return resultObj;
}
//处理命令,相关消息处理,统一通过command.exec方法进行执行
function dealMessage (cbmessage, command) {
var fun = function () {};
fun.prototype = cbmessage;
var resultObj = new fun();
cbmessage.child = resultObj;
resultObj.child = null;
resultObj.set('command', command);
resultObj.send = function () {//fnName, params, callback
// 'fnName', 'a', 'b', callback
// =>'fnName<{DEL}>a<{DEL}>b',callback
// 'fnName', 'a'
// =>'fnName<{DEL}>a'
var l = arguments.length, callback = arguments[l - 1];
if( typeof callback == 'function') {
cbmessage.send(cbmessage.params2str.apply(cbmessage, slice.call(arguments, 0, -1)),callback);
} else {
cbmessage.send(cbmessage.params2str.apply(cbmessage, slice.call(arguments)));
}
};
resultObj.get = function (str, callback) {
// '123456<{PA}>abcd',callback
//=>command.exec('123456','abcd',callback)
//=>S<{IE}>1<{IE}>67890
var params = cbmessage.str2params(str);
var command = this.command;
params.push(callback)
command.exec.apply(command, params);
};
return resultObj;
}
//命令对象
function Command () {
this.command = {};
}
Command.prototype = {
/**
* 执行命令函数
* @param {String} name 命令函数名
* @param {String} params{0,} 传递给 目标页面的命令方法 的参数,只支持string
* @param {Function} callback 成功的回调函数,默认为空函数
*/
exec : function() {
var name = arguments[0], args = slice.call(arguments, 1);
var fun = this.command[name];
if (fun !== undefined) {//没有定义的命令,不处理
if( typeof fun == 'function') {
fun.apply(this.command, args);
} else {
//对应的属性是数值或字符串,直接执行回调
var len = arguments.length;
arguments[len-1](fun);
}
}
},
/**
* this.command 添加属性,参数为一个时,则传入Object,将Object的自己的属性扩展到command上
* @param {String|Object} name String时key值
* @param {Function} fun 函数方法
*/
add : function(name, fun) {
if(arguments.length == 1) {
for(var i in name) {
if(hasOwn.call(name, i)) {
this.command[i] = name[i];
}
}
} else {
this.command[name] = fun;
}
return this;
}
};
/**
* 获取跨域对象
* @param {Object} config配置对象
* @return {Object} ifrme结点
*/
var href = location.href;
win.getCrossDomain = function (config) {
var messageObj;
var mainCommand = new Command();//主端命令
var clientCommand = new Command();//客户端命令
var clientUrl = config.clientUrl;
var aboutBlank = config.aboutBlank||'IE';//'IE','ALL'
var isSameDomain = config.isSameDomain === undefined ? util.isSameDomain(href, clientUrl):!!config.isSameDomain;//当前页面和API提供地址是不是同域
//获取消息对象
var getMessaage = messageSport == 'name'? function (isBindMessage) {
var obj = dealMessage(callbackMessage(ieMessage(prefixMessage(message(), config.prefix))));
util.runReady(obj, 'send');
if (isBindMessage !== false) {
obj.bindMessage(win);
}
return obj;
}: function (isBindMessage) {
var obj = dealMessage(callbackMessage(prefixMessage(message(), config.prefix)));
util.runReady(obj, 'send');
if (isBindMessage !== false) {
obj.bindMessage(win);
}
return obj;
};
var isClient = href.indexOf(clientUrl) != -1;
//消息队列,不跨域情况的支持
var commandList = [];
commandList.exec = function () {
var name = arguments[0];
for (var i=0,len=this.length; i<len; i++) {
if (!!this[i].command[name]) {
this[i].exec.apply(this[i], arguments);
return;
}
}
};
var pageType = 0;
if(isClient == true) {//iframe页
pageType = 2;
messageObj = getMessaage();
messageObj.send.setReady();
messageObj.set({proxy:win.parent,command:clientCommand});
clientCommand.add('__setPrefix', function (str, callback) {
callback();
messageObj.set('prefix', str);
});
} else if (isSameDomain) {//没有跨域,没有跨域,多参不需要转成字符串,所以默认就支持多种格式,【测试,没有具体情况】
pageType = 3;
commandList.push(clientCommand,mainCommand);
messageObj = {
send : function() {
var command = this.command, arg;
var callback = arguments[arguments.length-1];
if (typeof callback == 'function') {
command.exec.apply(command, arguments);
} else {
arg = slice.call(arguments);
arg.push(emptyFun);
command.exec.apply(command, arg);
}
},
command: commandList//既是客户端,也是主端
};
} else {
//主端
if (aboutBlank == 'IE' && messageSport == 'postMessage') {
messageObj = getMessaage();
var frame = util.iframeLoad(win, clientUrl, function() {
messageObj.set('proxy', frame.contentWindow);
messageObj.send.setReady();
});
if (!prefixManage.isUnique(messageObj.prefix)) {//不止一个使用该前缀
var prefix = prefixManage.getNew(messageObj.prefix);
messageObj.send('__setPrefix',prefix ,function () {
messageObj.set('prefix', prefix);
})
}
} else if (aboutBlank != 'IE'|| (aboutBlank == 'IE' && messageSport == 'name')) {
messageObj = getMessaage(false);
var frame = util.iframeLoad(win, '', function() {
messageObj.bindMessage(frame.contentWindow);
var proxframe = util.iframeLoad(frame.contentWindow, clientUrl, function() {
messageObj.set('proxy',proxframe.contentWindow);
messageObj.send.setReady();
});
});
}
messageObj.set('command', mainCommand);
pageType = 1;
}
return {
message: messageObj, //通信对象
mainCommand: mainCommand,
clientCommand: clientCommand,
isMain: function () { //判断当前页面主端
return (pageType == 1 || pageType == 3);
},
isClient: function () { //判断当前页面客户端
return (pageType == 2 || pageType == 3);
}
}
};
}
})(this, document);