This repository has been archived by the owner on Feb 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
messenger.js
269 lines (219 loc) · 7.15 KB
/
messenger.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
// ==UserScript==
// @name MessengerPG
// @namespace http://messengerpg.tech/
// @version 0.0.4
// @description PGP encrypt your messages
// @author Petar Segina, Stanko Krtalic Rusendic, Luka Strizic, Marko Bozac
// @match https://www.messenger.com/*
// @require https://code.jquery.com/jquery-2.1.3.min.js
// @grant none
// @run-at document-start
// ==/UserScript==
var incomingMessageBubbleClass = "_3oh-";
var outputMessageTransformer = function(input) {
var recipientExtractor = new ReipientExtractor();
var recipients = {
names: recipientExtractor.names()
};
var mgp = new MPG();
return mgp.encrypt(input, recipients);
};
var inputMessageTransformer = function(body) {
var mgp = new MPG();
return mgp.decrypt(body);
};
// -------------------------------------------------
// WATCH & UNWATCH
// -------------------------------------------------
if (!Object.prototype.watch) {
Object.defineProperty(Object.prototype, "watch", {
enumerable: false
, configurable: true
, writable: false
, value: function (prop, handler) {
var
oldval = this[prop]
, newval = oldval
, getter = function () {
return newval;
}
, setter = function (val) {
oldval = newval;
return newval = handler.call(this, prop, oldval, val);
}
;
if (delete this[prop]) { // can't watch constants
Object.defineProperty(this, prop, {
get: getter
, set: setter
, enumerable: true
, configurable: true
});
}
}
});
}
// object.unwatch
if (!Object.prototype.unwatch) {
Object.defineProperty(Object.prototype, "unwatch", {
enumerable: false
, configurable: true
, writable: false
, value: function (prop) {
var val = this[prop];
delete this[prop]; // remove accessors
this[prop] = val;
}
});
}
// -------------------------------------------------
// MESSENGER PRIVACY GUARD
// -------------------------------------------------
console.log("Adding PGP support...");
window.watch("__d", function(id, oldVal, newVal) {
return function(sa, ta, ua, va) {
if(sa == "MessengerComposerInput.react") {
var oldUa = ua;
var newUa = function(b, c, d, e, f, g, h, i) {
var oldC = c;
// Patch the factory function
c = function(name) {
var thing = oldC(name);
var oldClassCreator = thing.createClass;
// Patch the create class method so we can modify object maps on the fly
thing.createClass = function(obj) {
// Replace the getValue property if it is present
if(obj.displayName === "MessengerInput" && obj.hasOwnProperty("_handleReturn") && obj.hasOwnProperty("getValue")) {
var oldGetValue = obj.getValue;
obj.getValue = function() {
var actualValue = oldGetValue.bind(this)();
if(this.state.editorState.clearText == actualValue) {
var result = this.state.editorState.cipherText;
return result;
}
return actualValue;
};
obj._handleReturn = (function(event) {
if (c('isSoftNewlineEvent')(event)) {
var m = c('handleSoftNewlineForEmoticon')(this.state.editorState);
if (m === this.state.editorState) return false;
this.setState({
editorState: m
});
return true;
}
if (this.state.editorState.clearText !== this.getValue().trim()) {
this.state.editorState.clearText = this.getValue().trim();
this.state.editorState.cipherText = outputMessageTransformer(this.state.editorState.clearText);
}
if (this.getValue().trim().length > 0) this._sendMessage();
return true;
});
}
return oldClassCreator(obj);
};
// Patch the createElement method so that we can change element renderings on the fly
if(name === "React") {
var oldElementCreator = thing.createElement;
if(oldElementCreator && oldElementCreator.length === 3) {
thing.createElement = function() {
if(arguments.length > 1) {
var elementArgs = arguments[1];
if(elementArgs && elementArgs.hasOwnProperty("className") && elementArgs.className === incomingMessageBubbleClass && elementArgs.body) {
elementArgs.body = inputMessageTransformer(elementArgs.body);
}
}
return oldElementCreator.apply(this, arguments);
};
}
}
return thing;
};
return oldUa(b, c, d, e, f, g, h, i);
};
ua = newUa;
}
newVal(sa, ta, ua, va);
};
});
console.log("Added PGP support!");
MPG = function() {
this.message = '';
this.recipients = [];
this.url = {
encrypt: 'https://pgp.messenger.com/encrypt',
decrypt: 'https://pgp.messenger.com/decrypt'
};
};
MPG.prototype.encrypt = function(message, recipients) {
this.message = message;
if (recipients) {
this.recipients = recipients;
}
var data = {
message: this.message,
recipients: this.recipients.names
};
var encryptedMessage = this.ajax(this.url.encrypt, data, 'POST');
if (message === encryptedMessage) {
if (confirm('Unable to encrypt message! Missing public key for one or more recipients. Do you wish to send this message any way?')) {
return message;
}
else {
return '';
}
}
return encryptedMessage;
};
MPG.prototype.decrypt = function(message) {
this.message = message;
if(this.isPgpMessage(message)) {
return this.ajax(this.url.decrypt, { message: message }, 'POST');
} else {
return message;
}
};
MPG.prototype.isPgpMessage = function(message) {
return message.match(/-----BEGIN\sPGP.*?\sMESSAGE-----(\n|.)*?-----END\sPGP\sMESSAGE-----/);
};
MPG.prototype.extractMessages = function(message) {
var messages = this.isPgpMessage();
if (messages && messages.length > 0) {
return messages;
}
return;
};
MPG.prototype.ajax = function(url, body, verb) {
var request = new XMLHttpRequest();
request.open(verb, url, false);
request.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
request.send(JSON.stringify(body));
if (request.status === 200) {
var message = '';
try {
message = JSON.parse(request.responseText);
return message.message;
}
catch(e) {
alert(e);
}
}
else {
alert('No response from server!');
}
return null;
};
var ReipientExtractor = function() {
this.window = window;
this.activeConversationClass = '_5l-3 _1ht1 _1ht2 _23_m';
this.personClass = '._5l37';
this.nameContainerClass = '._364g';
};
ReipientExtractor.prototype.names = function() {
var $people = $(this.personClass + ' ' + this.nameContainerClass);
var names = [];
$people.each(function(i, o) {
names = names.concat($(o).text());
});
return names;
};