-
Notifications
You must be signed in to change notification settings - Fork 8
/
inject.js
59 lines (43 loc) · 1.27 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
var port = chrome.runtime.connect({name: "channel"});
var ids = {};
var processMessages = function() {
var newMessages = [];
var bubbles = $$('.bubble-text');
for(var i = bubbles.length - 1; i >= 0; i--) {
var bubble = $$(bubbles[i]);
var reactid = bubble.data('reactid');
if (reactid in ids) {
break;
}
ids[reactid] = true;
var text = bubble.find('.message-text .emojitext').text();
newMessages.push({
reactid: reactid,
text: text,
});
}
return newMessages;
}
var messages = processMessages();
console.log('Existing messages: ', messages);
setInterval(function() {
console.log("Pooling for new messages...");
var newMessages = processMessages();
for (newMessage of newMessages) {
messages.unshift(newMessage);
var text = newMessage.text
console.log("New message, posting to extension: " + text);
port.postMessage({message: text});
}
}, 500)
port.onMessage.addListener(function(data) {
var msg = data.message;
console.log("Received reply from extension: " + msg);
$$('div.input').html(msg);
var event = document.createEvent('Event');
event.initEvent('input', true, true);
$$('div.input')[0].dispatchEvent(event);
setTimeout(function() {
$$('.icon-send').click();
}, 50);
});