forked from nicolas-t/gAES
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hangoutAES.user.js
280 lines (236 loc) · 13.4 KB
/
hangoutAES.user.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
// ==UserScript==
// @name HangoutAES
// @description Enrypting your Google Hangouts.
// @namespace HangoutAES
// @include https://talkgadget.google.com/*
// @include https://mail.google.com/mail/*
// @require http://code.jquery.com/jquery-1.9.1.min.js
// @require https://raw.github.com/mdp/gibberish-aes/master/dist/gibberish-aes-1.0.0.js
// @grant none
// @version 1
// ==/UserScript==
/**
* Nicolas Turlais
* 2013
* MIT Licence
* https://github.com/nicolas-t/gAES
* uses jQuery
* http://jquery.com/
* uses Gibberish AES
* Mark Percival
* https://github.com/mdp/gibberish-aes
*/
/* hangout : only run in chat frames */
if( (window.location.host == 'talkgadget.google.com' && window.location.hash.substring(0,6) != '#egtn_') ){
return;
}
/* gmail : only run in top window */
if( (window.location.host == 'mail.google.com' && window.top != window.self) ){
return;
}
jQuery(window).load(function(){
/*----- GLOBALS ------------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------------------------------------*/
/* @whiteList : The name of the user you want to chat with,and a random passphrase you share with him */
/*-------------------------------------------------------------------------------------------------------*/
/*-
//WhiteList exemple : 1 user
var whiteList = [{
user : 'Keith Haring',
passphrase : 'a-strong-passphrase'
}];
//WhiteList exemple : 2 users
var whiteList = [{
user : 'Stephen Shore',
passphrase : 'a-strong-passphrase'
},
{
user : 'Andy Warhol',
passphrase : 'another-strong-passphrase'
}];
-*/
var whiteList = [{
user : 'Firstname Surname', /* edit here */
passphrase : 'abcDEF-123456' /* edit here */
}];
/*-------------------------------------------------------------------------------------------------------*/
/* @matcherStart : character for detecting the begining of a encrypted string */
/* @matcherStop : character for detecting the end of a encrypted string */
/* @refresh : duration between two chat refreshing (in ms) */
/*-------------------------------------------------------------------------------------------------------*/
var matcherStart = '#',
matcherStop = '_',
refresh = 1000;
/*----- CHAT EVENTS ----------------------------------------------------------------------------*/
if(window.location.host == 'talkgadget.google.com'){
/*------------------------------------------------------------------------------------------------------*/
/* When the "Enter" key is pressed, if the user is whitelisted... */
/* Instead of sending the message we encrypt it... */
/* Then we submit it encrypted surrounded by the two matchers. */
/*------------------------------------------------------------------------------------------------------*/
jQuery(document).on('keydown', "[role='textbox']", function (e){
if( e.keyCode == 13 ){
var conversation = findConversation(jQuery(this));
var receiver = findReceiver(conversation);
var receiverIndex = chat.isWhiteListed(receiver);
if(receiverIndex < 0 ){ return;}
var receiverPassphrase = whiteList[receiverIndex].passphrase;
var clearText = jQuery(this).text();
var encryptedText = GibberishAES.enc(clearText, receiverPassphrase).replace("\n", "");
jQuery(this).text(matcherStart + encryptedText + matcherStop);
}
});
/*------------------------------------------------------------------------------------------------------*/
/* We refresh the chat box every x miliseconds... */
/* In order to decrypt the encrypted messages (received or sent) */
/*------------------------------------------------------------------------------------------------------*/
setInterval(function(){
chat.updateConversationsContent();
},refresh);
}
/*----- HISTORY EVENTS -------------------------------------------------------------------------*/
/* gmail : history */
if(window.location.host == 'mail.google.com'){
/*------------------------------------------------------------------------------------------------------*/
/* When viewing the chat history the URL changes to #chats/XXXXX */
/* We check is the viewed history belongs to whitelisted user */
/* If so we add a "Decrypt" button */
/*------------------------------------------------------------------------------------------------------*/
jQuery(window).on('hashchange', function() {
if ( window.location.hash.substring(0, 7) == "#chats/" ) {
jQuery.each(whiteList, function(index, value){
if( history.testHistoryUser(value.user) ){
history.createDecryptButton('h1.ha', index);
}
});
}
});
}
/*----- CHAT FUNCTIONS -----------------------------------------------------------------------*/
var chat = {
/*------------------------------------------------------------------------------------------------------*/
/* Decypt all whitelisted conversations messages */
/*------------------------------------------------------------------------------------------------------*/
updateConversationsContent : function(){
var self = this;
findAllConversations().each(function(){/*each conversation*/
var receiver = findReceiver(jQuery(this));
var receiverIndex = self.isWhiteListed(receiver);
if( receiverIndex < 0 ){ return;}
findAllMessages(jQuery(this)).each(function(){ /*each message*/
core.decrypt(whiteList[receiverIndex].passphrase, this);
});
});
},
/*------------------------------------------------------------------------------------------------------*/
/* similar to indexOf or inArray */
/* returns the index of the user @s or -1 if no user found */
/*------------------------------------------------------------------------------------------------------*/
isWhiteListed : function(s) {
var r = -1;
jQuery.each(whiteList, function(index, value){
if( s == value.user ){
r = index;
return false; //break the loop
}
});
return r;
}
}
/*----- HISTORY FUNCTIONS -------------------------------------------------------------------*/
var history = {
/*------------------------------------------------------------------------------------------------------*/
/* Tries to find a whitelisted user name in the <h1> tag of the chat history page */
/*------------------------------------------------------------------------------------------------------*/
testHistoryUser : function(user){
var str = jQuery('h1.ha').text();
var matcher = new RegExp("\\b" + user + "\\b", "g");
if( str.match(matcher) ){
return true;
}
},
/*------------------------------------------------------------------------------------------------------*/
/* Creates a nice google blue button in @parent */
/* Bind a function (decryptHistory()) to click, passing whitelisted user @index as param */
/*------------------------------------------------------------------------------------------------------*/
createDecryptButton : function(parent, index){
jQuery('<span/>',{'class' : 'T-I J-J5-Ji aOA T-I-atl'})
.html('Decrypt')
.appendTo(parent)
.on('click', index, this.decryptHistory)
.css({'position' : 'absolute' , 'top' : '-4px'});
},
/*------------------------------------------------------------------------------------------------------*/
/* loop trought all history messages and call the decrypt function */
/* @event.data contain the whitelisted user index (see createDecryptButton() for binding) */
/*------------------------------------------------------------------------------------------------------*/
decryptHistory : function(event){
var receiverIndex = event.data;
findAllHistoryMessages().each(function(){
core.decrypt(whiteList[receiverIndex].passphrase, this);
});
}
}
/*----- CORE FUNCTIONS ----------------------------------------------------------------------*/
var core = {
/*------------------------------------------------------------------------------------------------------*/
/* uses the two matchers to detect encrypted messages in @str */
/* loops trought result, remove the matcherStart, stores and returns the clean encrypted messages */
/*------------------------------------------------------------------------------------------------------*/
getMatches : function(str){
var matcher = new RegExp(matcherStart + "([\\s\\S]*?)(?=" + matcherStop + ")", "g");
var result = str.match(matcher);
var r = [];
if( result ){
for (var i = 0; i < result.length; i++) {
if( result[i].length > 0 ) {
r.push(result[i].substring(1, result[i].length));
}
}
}
return r;
},
/*------------------------------------------------------------------------------------------------------*/
/* Get encrypted strings with getMatches() in @elem */
/* Then for each one decrypts it with @receiverPassphrase */
/* linkify it and adds it to @elem */
/*------------------------------------------------------------------------------------------------------*/
decrypt : function (receiverPassphrase, elem){
var self = this;
var $message = jQuery(elem);
var clearStr = $message.text();
var encryptedStr = this.getMatches(clearStr);
jQuery.each(encryptedStr, function(k,v){ /*each encrypted string in a message*/
var textDecrypt = GibberishAES.dec(v, receiverPassphrase).replace("\n", "");
var textClear = clearStr.replace(matcherStart + v + matcherStop, textDecrypt);
var textParsed = self.linkify(textClear);
$message.html(textParsed);
});
},
/*------------------------------------------------------------------------------------------------------*/
/* Detects links in @text and replaces it with html markup for links */
/*------------------------------------------------------------------------------------------------------*/
linkify : function(text) {
text = text.replace(/(https?:\/\/\S+)/gi, function (s) {
return '<a href="' + s + '">' + s + '</a>';
});
return text;
}
}
/*----- DOM SELECTIONS ------------------------------------------------------------------------*/
function findReceiver($conversation){
return $conversation.find('.RE.OxDpJ').text();
}
function findAllMessages($conversation){
return $conversation.find(".Mu.SP");
}
function findConversation($children){
return $children.parents(".DH.hh.WQ.X0:visible");
}
function findAllConversations(){
return jQuery(".DH.hh.WQ.X0:visible");
}
function findAllHistoryMessages(){
return jQuery("[data-type='m']").find('td.aOV');
}
});