-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathldr_unread_counter.js
203 lines (186 loc) · 5.35 KB
/
ldr_unread_counter.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
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
203
/**
* ==VimperatorPlugin==
* @name LDR unread counter
* @description Display the unread count of LDR to the statusbar
* @description-ja ステータスバーにLDRの未読件数を表示
* @version 0.1b
* @author teramako [email protected]
* ==/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 (let 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){
var xhr;
try {
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: