-
Notifications
You must be signed in to change notification settings - Fork 3
/
inject.js
91 lines (79 loc) · 3.01 KB
/
inject.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
$(document).ready(function() { var hook = function() {
// Note: The above line is a single line in order to get accurate
// line numbers for error messages.
// TODO(drheld): Add try/catch around everything in this section so this
// can't bust the game with an exception.
var sequence = 0;
var websocket_wrapper;
// Boilerplate to read websocket traffic and pass through to the extension.
function hookWebSocket() {
// Adapted from: http://sla.ckers.org/forum/read.php?6,35771,35771
window.WebSocket = function(oldWebSocket) {
return function WrappedWebSocket(loc) {
this.prototype = new oldWebSocket(loc);
this.__proto__ = this.prototype;
websocket_wrapper = this;
this.onmessage = function(message) {
var data = message.data;
handleMessage(data);
websocket_wrapper.trueonmessage({data: data});
};
this.__defineSetter__('onmessage', function(val) {
websocket_wrapper.trueonmessage = val;
});
this.send = function(data) {
handleMessage(data);
this.prototype.send(data);
};
};
}(window.WebSocket);
}
hookWebSocket();
function handleMessage(raw_data) {
// Pass through to extension.
$('#socket_messages').append(
$('<div id=socket_message_"' + sequence++ + ">").text(raw_data));
// Unfortunately we need to parse the message to figure out enough to send messages.
var msg = $.parseJSON(raw_data);
if (msg.message == 'GameMessage') {
var outerdata = msg.data;
var msgname = outerdata.messageName;
var gmdata = outerdata.data;
if (msgname == 'gameSetup') {
userID = msg.destination;
gameID = msg.source;
myName = gmdata.playerInfos[gmdata.playerIndex].name;
}
}
}
// Pass through log messages as they're added.
Dom.LogManager.prototype.realAddLog = Dom.LogManager.prototype.addLog;
Dom.LogManager.prototype.addLog = function(options) {
$('#logs').append('<div>' + options.text + '</div>');
this.realAddLog(options);
};
// Send / receive events as triggered.
$('#text_to_send').bind('DOMNodeInserted', function(event) {
var node = $(event.target);
websocket_wrapper.send(node.text());
node.remove();
});
$('#text_to_receive').bind('DOMNodeInserted', function(event) {
var node = $(event.target);
websocket_wrapper.prototype.onmessage({data:node.text()});
node.remove();
});
}
// Boilerplate to run in page context (important for hooking the websocket).
var runInPageContext = function(fn) {
var script = document.createElement('script');
script.type = 'text/javascript';
script.textContent = '('+ fn +')();';
document.body.appendChild(script);
}
$('body').append($('<div id="logs">').css('display', 'none'));
$('body').append($('<div id="socket_messages">').css('display', 'none'));
$('body').append($('<div id="text_to_send">').css('display', 'none'));
$('body').append($('<div id="text_to_receive">').css('display', 'none'));
runInPageContext(hook);
});