-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsadinbox.js
136 lines (125 loc) · 4.66 KB
/
sadinbox.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
$(function(){
Backbone.Collection = Backbone.Collection.extend({
// Get an object from the collection using an arbitrary key value pair.
contains: function(key, value){
var index = this.pluck(key).indexOf(value);
return (index > -1) ? this.at(index) : null;
}
});
// Pretty much every model view has been EXACTLY the same except for the
// template and tage names used. Just a little wrapper so you only have
// to define those.
Backbone.ModelView = Backbone.View.extend({
initialize: function(opts){
_.bindAll(this, 'render');
this.model.bind('change', this.render);
this.model.view = this;
},
render: function() {
$(this.el).html(this.template(this.model.toJSON()));
return this;
},
remove: function() {
$(this.el).remove();
}
});
var Options = Backbone.Model.extend({
defaults: {
'customDomain': '',
'newMessageSound': 'http://media.freesound.org/data/19/previews/19446__totya__yeah_preview.mp3',
'noMessagesSound': 'http://media.freesound.org/data/73/previews/73581__Benboncan__Sad_Trombone_preview.mp3'
},
localStorage: new Store('sad-options')
});
var Inbox = Backbone.Model.extend({
defaults: {
'count': 0,
'lastUpdate': new Date(),
'url': 'https://mail.google.com/mail/feed/atom'
},
localStorage: new Store('sad-inbox')
});
// Handles setting up the options list.
var getOptions = function(){
var options = new Options;
options.fetch();
return options;
}
// Used by background.html to monitor updates.
var SadInbox = Backbone.Controller.extend({
initialize: function(){
_.bindAll(this, '_onTabSelectionChanged', '_giveFeedback');
this._options = getOptions();
this._inbox = new Inbox;
this._inbox.fetch();
this._fetchInboxUpdates();
chrome.tabs.onSelectionChanged.addListener(this._onTabSelectionChanged);
},
_fetchInboxUpdates: function(){
console.log('Fetching updates.');
var _inbox = this._inbox;
var _app = this;
$.ajax({'url': this._inbox.get('url'),
'success': function(data){
var newCount = parseInt($(data).find('fullcount').text());
_app._giveFeedback((newCount > _inbox.get('count') ? 'newMessageSound' : 'noMessagesSound'));
_inbox.set({'count': newCount, 'lastUpdate': new Date()});
_inbox.save();
},
'error': function(error){
console.error(error);
}
});
},
_onTabSelectionChanged: function(tabId, changeInfo, tab){
console.log('Tab selection changed.');
_.bindAll(this, '_onTabGet');
console.log('Going to get the tab');
chrome.tabs.get(tabId, this._onTabGet);
},
_onTabGet: function(tab){
console.log('onGetTab');
console.log(tab);
console.log(tab.url);
if(tab.url && tab.url.indexOf('mail.google.com') > -1){
console.log('Page is gmailish. Going to fetch updates');
this._fetchInboxUpdates();
}
else{
console.log('Not a gmail url. No-op.');
}
},
_giveFeedback: function(goodOrBad){
console.log('Giving feedback.' + goodOrBad);
this._playSound(this._options.get(goodOrBad));
},
_playSound: function(url){
console.log('Playsound '+url);
new Audio(url).play();
}
});
window.SadInbox = SadInbox;
var OptionsView = Backbone.ModelView.extend({
el: $('#sad-inbox-options'),
events: {
"submit #options-form": "saveOptions",
},
saveOptions: function(){
this.model.save();
return false;
},
render: function(){
this.template = _.template($('#options-template').html());
$(this.el).html(this.template(this.model.toJSON()));
return this;
}
});
var SadInboxOptions = Backbone.Controller.extend({
initialize: function(){
this._options = getOptions();
this._view = new OptionsView({model: this._options});
this._view.render();
}
});
window.SadInboxOptions = SadInboxOptions;
});