-
Notifications
You must be signed in to change notification settings - Fork 1
/
squawker.js
55 lines (46 loc) · 1.37 KB
/
squawker.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
window.Squawker = function() {
this.gmail = Gmail();
var self = this;
this.start = function() {
this.addObservers();
if(this.isInEmail()) this.emailOpened();
};
this.isInEmail = function() {
return this.gmail.check.is_inside_email();
};
this.addObservers = function() {
console.log("Woodchuck >> initializing squawker observers");
this.gmail.observe.on('open_email', function(id) {
setTimeout(function() { self.emailOpened(); }, 200);
});
};
this.eventNameFor = function(eventName) {
return "woodchuck:" + eventName.camelize(false);
};
this.emailOpened = function() {
var data = this.gmail.get.email_data();
if (data && data.threads) {
var currentUserEmail = this.gmail.get.user_email();
var userToLookUp = Object.values(data.threads)
.sortBy('datetime')
.exclude(function(value) {
return value.from_email === currentUserEmail;
})
.first();
if(userToLookUp) {
var name = userToLookUp.from || userToLookUp.from_email;
var email = userToLookUp.from_email;
window.postMessage({
type: this.eventNameFor('open_email'),
data: { name: name, email: email }
}, "*");
}
}
};
return this;
};
$(function() {
console.log("Woodchuck >> initializing squawker");
var squawker = new Squawker();
squawker.start();
});