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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
/**
* ==VimperatorPlugin==
* @name LDR unread counter
* @description Display unread count of LDR to statusbar
* @description-ja ���ơ������С���LDR��̤�ɷ�����ɽ��
* @version 0.1a
* @author teramako teramako@gmail.com
* ==/VimperatorPlugin==
*
* �ޤ��ǽ���
* let livedoor_id = "<livedoor ID>"
* �ȥ桼��ID�����ꤷ�Ƥ���������
*/
liberator.plugins.ldrUnreadCounter = (function(){
const defaultNameSpace = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
const gIntervalName = "ldr_check_intervals";
const userIdName = "livedoor_id";
var statusPanel, canvas;
var icon_image = new Image();
icon_image.src = "data:image/png;base64," +
"iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAQ5JREFUOMul" +
"kz1OAzEQhT87hihN2kUU3opjRELaiCYVgstQIKXLEdJT09IgrVJwi4SG7ehIgbR/Zik2WLblCCn7" +
"JMuemeenmbFHALDsOk7BUghx+Xji5QPUpGEQVOjYrUyUePUwivqlqTvcNdVzG5zqubV3K0PINXWH" +
"+mqFn9LtBjDOGWuHXABl6uP1hbEYV5bVcYH92rBfG1tOtAcAZQXJd7+7guPkxp4vrjc27vJl2kLa" +
"wofq97R15Be5Fdk+GRt3+fYZ/y6+OwJNCWS5baKLtO25gjv/J/48+2R5P+LsLaP6fPV8AJxHPpJb" +
"NwCznGaWM04yz6dLKAChF8NmQTIQAuC/LIpDW3QwDsWLEL8RGH9NPOjBNQAAAABJRU5ErkJggg==";
/**
* initial function
*/
function init(){
createStatusButton();
manager.start();
}
/**
* create Status Icon to status line
*/
function createStatusButton(){
function createElement(localName, attrs, namespace){
var elm = document.createElementNS(namespace ? namespace : defaultNameSpace, localName);
for (var name in attrs) elm.setAttribute(name, attrs[name]);
return elm;
}
// FIXME: onclick����LDR���褦������
statusPanel = createElement("statusbarpanel", {
id: "ldr_unread_count_panel",
tooltiptext: "Count: " });
canvas = createElement("canvas", {
id: "ldr_unread_count_image",
width: "24", height: "16"
},"http://www.w3.org/1999/xhtml");
statusPanel.appendChild(canvas);
// Icon�����ǽ������褬���Ƥ���
var ctx = canvas.getContext("2d");
icon_image.onload=function(){ ctx.drawImage(icon_image,0,0); };
//ctx.drawImage(icon_image,0,0);
document.getElementById('status-bar').insertBefore( statusPanel,
document.getElementById('security-button').nextSibling);
}
function updateTooltip(str){
statusPanel.setAttribute("tooltiptext",str);
}
/**
* Update StatusIcon
* @param {String|Number} count
*/
function updateCanvasCount(count){
count = "" + count;
var ctx = canvas.getContext("2d");
ctx.clearRect(0,0,24,16);
ctx.save();
// LDR Icon ������
ctx.drawImage(icon_image,0,0);
var width = ctx.canvas.width;
var height = ctx.canvas.height;
var len = ctx.mozMeasureText(count);
// ̤�ɷ������طʤ��Ť�����
// XXX: ���ä��ɤ����罸
ctx.save();
ctx.fillStyle = "rgba(48,48,48,0.75)";
ctx.fillRect(width-len-1,4,len+1,12);
ctx.restore();
// ̤�ɷ���������
// XXX: ���ä��ɤ����罸
ctx.fillStyle = "Cyan";
ctx.mozTextStyle = "12px sans-serif";
ctx.translate(width - len-1 ,height-1);
ctx.mozDrawText(count);
ctx.restore();
}
/**
* Draw Stop mark
*/
function canvasDrawStop(){
var ctx = canvas.getContext("2d");
ctx.clearRect(0,0,24,16);
ctx.drawImage(icon_image,0,0);
ctx.save();
//�ػߥޡ���������
ctx.strokeStyle = "Red";
ctx.lineWidth = "2";
ctx.beginPath();
ctx.arc(17,9,6,0,Math.PI*2,false);
ctx.stroke();
ctx.translate(17,9);
ctx.rotate(Math.PI/4);
ctx.beginPath();
ctx.moveTo(-6,0);
ctx.lineTo(6,0)
ctx.stroke()
ctx.restore();
}
var unreadCount = 0;
function setCount(count){
unreadCount = count;
updateTooltip("Unread: " + count);
updateCanvasCount(count);
}
function checkCount(id){
try {
var xhr = new XMLHttpRequest();
xhr.mozBackgroundRequest = true;
xhr.open("GET", "http://rpc.reader.livedoor.com/notify?user=" + id, false);
xhr.send(null);
setCount(xhr.responseText.split("|")[1]);
} catch (e) {
liberator.log(e,0);
liberator.echoerr("LDR Unread Counter: " + e.message);
}
}
var timer = null;
function startup(id){
setTimeout(function(){
checkCount(id);
timer = setTimeout(arguments.callee, manager.interval);
}, 1000);
}
// ---------------------------------------------------
// PUBLIC
// ---------------------------------------------------
var manager = {
get user_id() {
return liberator.globalVariables[userIdName];
},
get interval() {
var interval = liberator.globalVariables[gIntervalName];
if (interval){
interval = parseInt(interval,10);
}
return (isNaN(interval) ? 120 : interval) * 1000;
},
set interval(value){
value = parseInt(value,10);
if (isNaN(value)) return null;
liberator.globalVariables[gIntervalName] = value;
return value;
},
get count(){
return unreadCount;
},
start: function(){
if (!this.user_id) {
liberator.echoerr("LDR Unread Counter: Please :let "+ userIdName + " = <livedoor ID>");
// FIXME: �ʤ��顼���Ф롣�������ɤ�ʬ������
//this.stop();
return;
}
if (timer === null)
startup(this.user_id);
else
liberator.echoerr("LDR Unread Counter has already started");
},
stop: function(){
window.clearTimeout(timer);
timer = null;
canvasDrawStop();
updateTooltip("LDR Unread Counter: stoped");
},
open: function(){
liberator.open(['http://reader.livedoor.com/reader/']);
}
};
init();
return manager;
})();
// vim: sw=4 ts=4:
|