-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.js
More file actions
58 lines (52 loc) · 1.45 KB
/
proxy.js
File metadata and controls
58 lines (52 loc) · 1.45 KB
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
var $ = require('jquery-browserify');
var json = typeof JSON === 'object' ? JSON : require('jsonify');
var proto = require('dnode-protocol');
var bus = require('postmessage');
function copyLocation () {
var keys = [
'hash', 'host', 'hostname', 'href', 'origin', 'pathname', 'port',
'protocol', 'search'
];
var res = {};
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
res[key] = window.location[key];
}
return res;
}
var client = proto(function (remote, conn) {
this.location = copyLocation();
this.run = function (src, vars) {
if (!vars) vars = {};
var args = [], names = [];
for (var key in vars) {
names.push(key);
args.push(vars[key]);
}
$(function () {
var fn = Function(names, 'return ' + src);
fn.apply(null, args)(window, $);
});
};
}).create();
client.on('request', function (req) {
bus.postMessage(
json.stringify([ '__testling_message', req ]),
'*',
window.parent
);
});
if (window.parent !== window) {
bus.receiveMessage(function (ev) {
try {
var msg = json.parse(ev.data);
if (msg[0] === '__testling_message') client.handle(msg[1]);
}
catch (err) {}
});
bus.postMessage(
json.stringify([ '__testling_open', copyLocation() ]),
'*', window.parent
);
client.start();
}