-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsochatfaviconnotifier.user.js
135 lines (117 loc) · 5.63 KB
/
sochatfaviconnotifier.user.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
// ==UserScript==
// @name Stack Overflow Chat Favicon Notifier
// @namespace Aditya V. Mukherjee
// @namespace Y. Jiang
// @description Watches the SO Chat for new messages, and changes the Favicon accordingly. Fixed to work with Chrome. Also uses CANVAS to draw the favicon, less dependent on images.
// @include http://chat.stackoverflow.com/rooms/*
// @include http://chat.meta.stackoverflow.com/rooms/*
// ==/UserScript==
(function() {
var title = document.title,
SO_favicon_base64 = "data:image/x-icon;base64,AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAAAABMLAAATCwAAAAAAAAAAAAAAAAAAbGxt/2xsbf9sbG3/bGxt/2xsbf9sbG3/bGxt/2xsbf9sbG3/bGxt/2xsbf9sbG3/bGxt/2xsbf8AAAAAAAAAAGxsbf9sbG3/bGxt/2xsbf9sbG3/bGxt/2xsbf9sbG3/bGxt/2xsbf9sbG3/bGxt/2xsbf9sbG3/AAAAAAAAAABsbG3/bGxt/wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABsbG3/bGxt/wAAAAAAAAAAbGxt/2xsbf8AAAAAbGxt/2xsbf9sbG3/bGxt/2xsbf9sbG3/bGxt/2xsbf8AAAAAbGxt/2xsbf8AAAAAAAAAAGxsbf9sbG3/AAAAAGxsbf9s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IQAA+MP///+H////DwAA/g8AAA==",
canvas = document.createElement('canvas'),
ctx, img = new Image();
img.src = SO_favicon_base64;
/**
* thanks to Raynos (http://stackoverflow.com/users/419970/raynos)
*/
var Shape = function() {};
Shape.prototype = {
sin: function(degrees) {
return Math.sin(degrees * Math.PI / 180);
},
cos: function(degrees) {
return Math.cos(degrees * Math.PI / 180);
},
drawStar: function(x, y, radius, points, context) {
var r = Math.sqrt(2 * Math.pow(radius, 2));
context.moveTo(x, y);
for (var i = 0; i < points; i++) {
x += r * this.cos(i * (360 / points));
y += r * this.sin(i * (360 / points));
context.lineTo(x, y);
x += r * this.cos(120 + i * (360 / points));
y += r * this.sin(120 + i * (360 / points));
context.lineTo(x, y);
}
}
};
var max = 30;
function changeFavicon(src) {
var link = document.createElement('link');
link.id = 'favicon';
link.rel = 'shortcut icon';
link.type = "image/png";
link.href = src;
oldLink = document.getElementsByTagName('link');
for (i = 0; i < oldLink.length; i++){
if (oldLink[i].getAttribute('rel').indexOf('shortcut') > -1) { //remove old LINK element
document.head.removeChild(oldLink[i]);
break;
}
}
document.head.appendChild(link);
}
setInterval(function() {
if (title !== document.title) {
title = document.title;
var times, mention = false,
ctitle = title;
if (title.indexOf('(') === 0) {
ctitle = title.substring(1, title.indexOf(')'));
if (ctitle.indexOf('*') > -1) {
mention = true;
if (ctitle.length === 1) {
times = 0;
} else {
times = parseInt(ctitle, 10);
}
} else {
times = parseInt(ctitle, 10);
}
} else {
times = 0;
}
if (times === 0) {
changeFavicon(SO_favicon_base64);
return;
}
if (canvas.getContext) {
canvas.height = canvas.width = 16; // set the size
ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0);
ctx.fillStyle = (mention) ? 'rgba(107, 162, 73, 1)' : 'rgba(227, 66, 52, 1)';
var starDimensions = {
x: function() {
if (times < 10) { return 14; }
else { return 14; }
},
y: function() { return 6; },
r: function() {
if (times < 10 || times > max) { return 1.4; }
else { return 1.6; }
}
};
(new Shape()).drawStar(starDimensions.x(), starDimensions.y(), starDimensions.r(), 16, ctx);
ctx.fill();
ctx.font = 'bold ' + ((times > max) ? 20 : 9) + 'px "Helvetica Neue", sans-serif';
ctx.fillStyle = '#FFF';
var textDimensions = {
x: function() {
if (times < 10) { return 8; }
else if (times < 20) { return 4; }
else if (times > max) { return 6; }
else { return 5; }
},
y: function() {
if (times > max) { return 20; }
else { return 13; }
}
};
ctx.fillText((times > max) ? "*" : times, textDimensions.x(), textDimensions.y());
changeFavicon(canvas.toDataURL('image/png'));
}
} //if
}, 1000);
console.log('SOCFN loaded » ' + (new Date()).getTime());
})();