-
Notifications
You must be signed in to change notification settings - Fork 9
/
popup.js
111 lines (95 loc) · 3.28 KB
/
popup.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
var messagesRegex = /^https?:\/\/www.facebook.com\/messages\/(.*)/;
var messengerRegex = /^https?:\/\/www.messenger.com\/t\/(.*)/;
function getCurrentTabUrl(callback) {
var queryInfo = {
active: true,
currentWindow: true
};
chrome.tabs.query(queryInfo, function(tabs) {
callback(tabs[0]);
});
}
function renderAtId(id, text) {
document.getElementById(id).innerHTML = text;
}
function renderAtAllClasses(className, text) {
Array.from(document.getElementsByClassName(className)).forEach(function(e) {
e.innerHTML = text;
});
}
function formatDate(rawDate) {
return Math.round(rawDate / 1000 / 60 * 100) / 100;
}
function displayCurConvoInfo(e) {
if (!e) {
renderAtId('error', 'Oops something went wrong. Please refresh and try again :(');
return;
}
renderAtId('error', '');
renderAtId('status', 'Out of ' + e.messageNumber + ' most recent messages with ' + e.oppName + ':');
renderAtAllClasses('name_opp', e.oppName);
var cntSession = 0;
var last = -1;
// 0: self, 1: opp
var totReplyTime = [0, 0];
var lastReplyTimestamp = [0, 0];
var totMsgNumber = [0, 0];
for (var i in e.messages) {
var m = e.messages[i];
if (m.type === 'separator') {
// A separator means a start of a convo session. Restart timestamp.
lastReplyTimestamp[0] = 0;
lastReplyTimestamp[1] = 0;
last = -1;
} else {
// Update timestamps otherwise.
var cur = m.source === 'self' ? 0 : 1;
totMsgNumber[cur] ++;
lastReplyTimestamp[cur] = m.time;
if (last !== cur) {
if (lastReplyTimestamp[1 - cur] === 0) {
continue;
}
totReplyTime[cur] += m.time - lastReplyTimestamp[1 - cur];
cntSession ++;
last = cur;
}
}
}
renderAtId('stats_self', formatDate(totReplyTime[0] / cntSession));
renderAtId('stats_opp', formatDate(totReplyTime[1] / cntSession));
renderAtId('n_self', totMsgNumber[0]);
renderAtId('n_opp', totMsgNumber[1]);
var lastMsg = e.messages[e.messages.length - 1];
// Expected reply time should be at least as long as the longest reply time of either side.
var expectReplyTime = formatDate(lastMsg.time + (totReplyTime[1] / cntSession) - Date.now());
if (lastMsg.type === 'separator' || expectReplyTime < 0 || totReplyTime[0] > totReplyTime[1]) {
renderAtId('title', 'YES');
} else {
renderAtId('title', 'NO');
renderAtId('suggestion', 'Maybe in ' + expectReplyTime + ' minutes.');
}
}
document.addEventListener('DOMContentLoaded', function() {
getCurrentTabUrl(function(tab) {
renderAtId('error', 'Loading...');
// Get friend id from route
if (messagesRegex.test(tab.url)) {
chrome.tabs.sendMessage(tab.id, { text: 'getMessages', oppName: tab.url.match(messagesRegex)[1] }, displayCurConvoInfo);
} else if (messengerRegex.test(tab.url)) {
renderAtId(
'error',
'Chatting on messenger.com? Check out <a href=\'https://www.facebook.com/messages/' +
tab.url.match(messengerRegex)[1] +
'\'>here</a> to see if you should reply or not!'
);
} else {
renderAtId('error', 'This extension only works for facebook messages.');
}
});
});
document.addEventListener('click', function(e){
if (e.target.href !== undefined) {
chrome.tabs.create({ url: e.target.href });
}
});