-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.html
181 lines (151 loc) · 4.82 KB
/
background.html
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
<html>
<head>
<script src="connection.js" type="text/javascript"></script>
<script>
var animationFrames = 36;
var animationSpeed = 10; // ms
var canvas;
var canvasContext;
var loggedInImage;
var pollIntervalMin = 1000 * 60; // 1 minute
var pollIntervalMax = 1000 * 60 * 60; // 1 hour
var requestFailureCount = 0; // used for exponential backoff
var requestTimeout = 1000 * 2; // 5 seconds
var rotation = 0;
var unreadCount = -1;
var loadingAnimation = new LoadingAnimation();
// SalesForce Connection
var tempResult = sforce.connection.login(localStorage.username, localStorage.password + localStorage.securityToken);
var currentUserId = tempResult.userId;
// A "loading" animation displayed while we wait for the first response from
// Salesforce. This animates the badge text with a dot that cycles from left to
// right.
function LoadingAnimation() {
this.timerId_ = 0;
this.maxCount_ = 8; // Total number of states in animation
this.current_ = 0; // Current state
this.maxDot_ = 4; // Max number of dots in animation
}
LoadingAnimation.prototype.paintFrame = function() {
var text = "";
for (var i = 0; i < this.maxDot_; i++) {
text += (i == this.current_) ? "." : " ";
}
if (this.current_ >= this.maxDot_)
text += "";
chrome.browserAction.setBadgeText({text:text});
this.current_++;
if (this.current_ == this.maxCount_)
this.current_ = 0;
}
LoadingAnimation.prototype.start = function() {
if (this.timerId_)
return;
var self = this;
this.timerId_ = window.setInterval(function() {
self.paintFrame();
}, 100);
}
LoadingAnimation.prototype.stop = function() {
if (!this.timerId_)
return;
window.clearInterval(this.timerId_);
this.timerId_ = 0;
}
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo) {
if (changeInfo.url && isSalesforceUrl(changeInfo.url)) {
getInboxCount(function(count) {
updateUnreadCount(count);
});
}
});
function init() {
canvas = document.getElementById('canvas');
loggedInImage = document.getElementById('logged_in');
canvasContext = canvas.getContext('2d');
chrome.browserAction.setBadgeBackgroundColor({color:[23, 151, 192, 255]});
chrome.browserAction.setIcon({path: "icon_19x19.png"});
loadingAnimation.start();
checkTime();
startRequest();
}
// defines how often to ping salesforce for new chanter information
function scheduleRequest() {
var randomness = Math.random() * 2;
var exponent = Math.pow(2, requestFailureCount);
var delay = Math.min(randomness * pollIntervalMin * exponent,
pollIntervalMax);
delay = Math.round(delay);
window.setTimeout(startRequest, delay);
}
// queries Salesforce for the current chatter count for the time interval and updates the number on the icon
function startRequest() {
getInboxCount(
function(count) {
loadingAnimation.stop();
updateUnreadCount(count);
scheduleRequest();
}
);
}
// queries salesforce for the current chatter count since last time to homepage via chatter extension
function getInboxCount(onSuccess) {
var query = sforce.connection.query("SELECT COUNT() FROM NewsFeed WHERE CreatedDate > " + localStorage.lastTime + " AND ParentId != '" + currentUserId + "'");
onSuccess(query.size);
}
// updates the count with the new information
function updateUnreadCount(count) {
if (unreadCount != count) {
unreadCount = count;
animateFlip();
}
}
function ease(x) {
return (1-Math.sin(Math.PI/2+x*Math.PI))/2;
}
function animateFlip() {
rotation += 1/animationFrames;
drawIconAtRotation();
if (rotation <= 1) {
setTimeout("animateFlip()", animationSpeed);
} else {
rotation = 0;
drawIconAtRotation();
chrome.browserAction.setBadgeText({
text: unreadCount != "0" ? unreadCount : ""
});
chrome.browserAction.setBadgeBackgroundColor({color:[23, 151, 192, 255]});
}
}
function drawIconAtRotation() {
canvasContext.save();
canvasContext.clearRect(0, 0, canvas.width, canvas.height);
canvasContext.translate(
Math.ceil(canvas.width/2),
Math.ceil(canvas.height/2));
canvasContext.rotate(2*Math.PI*ease(rotation));
canvasContext.drawImage(loggedInImage,
-Math.ceil(canvas.width/2),
-Math.ceil(canvas.height/2));
canvasContext.restore();
chrome.browserAction.setIcon({imageData:canvasContext.getImageData(0, 0,
canvas.width,canvas.height)});
}
// when it starts check the time
function checkTime() {
if( !localStorage.lastTime )
localStorage.lastTime = sforce.connection.getServerTimestamp().timestamp;
}
// When the popup is clicked reset the time and reload the count
function updateTime() {
localStorage.lastTime = sforce.connection.getServerTimestamp().timestamp;
loadingAnimation.start();
startRequest();
}
</script>
</head>
<body onload="init()">
<img id="logged_in" src="icon_19x19.png">
<canvas id="canvas" width="19" height="19">
</body>
</html>