-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblip.js
322 lines (270 loc) · 8.96 KB
/
blip.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
* $Id$
*
*
* Skrypt wstawia na stronie listę ostatnich wpisów w serwisie blip.pl
* Copyright (C) 2009 ^steelman
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Proszę nie usuwać powyższej informacji!
*/
/*
* Sposób użycia:
*
* W wybranym miejscu na stronie, w którym ma się pojawić wklejka
* należy wstawić następujący kod:
*
<script type="text/javascript">
//<![CDATA[
var blip_widget = {
'username' : 'nick',
'entries' : 5,
'heading' : 'na blipie',
'bullet' : '#' //znaczek wyświetlany jako link. domyślnie brak.
'icons' : { // domyślnie puste
film : 'film.png',
muzyka : 'muzyka.png',
obraz : 'obraz.png'
},
date : true //lub funkcja formatująca datę. domyślnie false.
// ZAWANSOWANE. Wstaw tylko jeżeli wiesz co to oznacza
'parent_disp' : 'block'
};
//]]>
</script>
<script type="text/javascript" src="http://twoj.adres/blip.js"></script>
*
* Parametry:
* username - twoja nazwa użytkownika w serwisie Blip
* entries - liczba wpisów pobieranych z serwisu Blip.
* domyślnie 5
* heading - nagłówek wstawiony tuż przed listą wpisów.
* może zawierać kod HTML.
*
* Parametr parent_disp odpowiedzialny jest za manipulację
* właściwością display w stylu elementu nadrzędnego nad wklejką.
* Na przykład jeżeli chcemy umieścić wklejkę jako element listy
*
<ul id="sidebar">
<li>Kategorie
<ul>
<li>kategoria1</li>
<li>kategoria2</li>
</ul>
</li>
<li style="display: none"><!-- wklejka Blip --></li>
<li>Archiwum
<ul>
<li>2008</li>
<li>2009</li>
</ul>
</li>
*
* to w przypadku przeglądarki z wyłączoną obsługą skryptów
* element ten pozostanie ukryty nie zaburzając tym samym
* wyglądu strony. Gdy obsługa skryptów zostanie włączona
* wybrany element zostanie wyświetlony.
*
*/
/* http://blog.versed.se/2008/07/transliterate-in-javascript.html */
String.prototype.tr = function(from, to) {
var fromChars = from.split("");
var toChars = to.split("");
var mapTable = {};
for(i = 0; i < fromChars.length; i++) {
var c = i < toChars.length ? toChars[i] : "";
mapTable[fromChars[i]] = c;
}
var str = this;
var re = new RegExp("[" + fromChars.join("") + "]", "g");
str = str.replace(re, function(c) {
return mapTable[c];
});
return str;
};
var blip_load_data = function(src) {
var s = t = null;
if (document.getElementById('blip') == null) {
alert("Wklejka blip nie została skonfigurowana. Brak elementu docelowego z atrybutem id=\"blip\".");
return false;
}
if ((t = document.getElementById('blip-data')) != null)
t.parentNode.removeChild(t)
s=doc_create_element('script');
s.setAttribute('src', src);
s.setAttribute('type', 'text/javascript');
s.setAttribute('id', 'blip-data');
document.getElementsByTagName('head')[0].appendChild(s);
}
var create_icon = function(type,text) {
var blip_links = {
blip : '[blip]',
link : '[link]',
muzyka : '[muzyka]',
obraz : '[obraz]',
film : '[film]'
};
var icon=null;
if (typeof blip_widget.icons[type] != "undefined") {
icon = doc_create_element('img');
icon.setAttribute('src', blip_widget.icons[type])
icon.setAttribute('alt', blip_links[type] +': '+ text)
} else if (typeof blip_links[type] != "undefined") {
icon = document.createTextNode(blip_links[type]);
} else
icon = document.createTextNode(text);
return icon;
};
var create_link = function(uri, title, type, i) {
var icon=(i || create_icon(type, title));
var link=doc_create_element('a');
link.setAttribute('title', type +': '+ title);
link.setAttribute('href', uri);
link.setAttribute('class', 'blip-link');
link.appendChild(icon)
return link;
};
var parse_date = function(d) {
var da=d.match(/^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/);
return new Date(da[1], da[2]-1, da[3], da[4], da[5], da[6]);
}
var format_date = function(d) {
var a = b = null;
if (typeof blip_widget.date == 'function') {
a = blip_widget.date(d);
} else if (typeof blip_widget.date == 'boolean' && blip_widget.date) {
a = doc_create_element('span');
b = document.createTextNode(
d.getDate() +'.'+
(d.getMonth() + 1) +'\u00a0'+
d.getHours() +':'+
d.getMinutes()
);
a.appendChild(b);
}
if (a != null)
a.setAttribute('class', 'blip-date');
return a;
}
var show_blip = function(b) {
if (typeof blip_widget.username == "undefined") {
alert("Wklejka Blip nie została skonfigurowana."+
" Brak nazwy użytkownika.");
return;
}
var linkre = /http:\/\/[\S_]+(?:\b|$)|\^[A-Za-z0-9]+|#[A-Za-z0-9ĘÓĄŚŁŻŹĆŃęóąśłżźćń_\-]{2,50}/im;
var filmre = /^http:\/\/(?:www\.)?(?:(?:[.\S]+.)?youtube.com\/watch\?v=|video.google.com\/videoplay\?docid=[0-9\-]*|vimeo.com\/\d+)/im;
var musicre = /^http:\/\/(?:www\.)?jamendo.com\/(?:[a-z][a-z]\/)?(?:album|track|playlist)\/(?:\d+)(?:\/.*)?/im;
var blipre = /^http:\/\/(?:www\.)?blip.pl\/(?:dm|s)\/\d+/im;
var blipblock=document.getElementById('blip');
var blipentries=doc_create_element('ul');
for (var i=0; i < b.length; i++) {
/*
* b[i].body - tekst wpisu
* b[i].id - identyfikator do zastosowania w linkach
*/
var node = null;
var message=b[i].body;
var mid=b[i].id;
var mtype=b[i].type.match(/[DPMS]/g).join("").toLowerCase();
var mlink = 'http://blip.pl/'+ mtype +'/'+ mid;
var mdate = parse_date(b[i].created_at);
var li = doc_create_element('li');
li.setAttribute('id', 'blip-status-' + mid);
if ((node = format_date(mdate)) != null)
li.appendChild(node);
while( linkre.exec(message) != null) {
var left=RegExp.leftContext;
var match=RegExp.lastMatch;
var right=RegExp.rightContext;
node = document.createTextNode(left);
li.appendChild(node);
if (match.substring(0,1) == '#') {
match = match.tr('ąćęłńóśźż_-', 'acelnoszz');
node = create_link('http://blip.pl/tags/' + match.substring(1), match, 'tag')
} else if (match.substring(0,1) == '^') {
match = match.tr('ąćęłńóśźż_-', 'acelnoszz');
node = create_link('http://'+ match.substring(1) +'.blip.pl/', match, 'użytkownik')
} else if (match.search(blipre) != -1) {
node = create_link(match, match, 'blip')
} else if (match.search(filmre) == 0) {
node = create_link(match, match, 'film')
} else if (match.search(musicre) != -1) {
node = create_link(match, match, 'muzyka')
} else if (match.substring(0,7) == "http://") {
node = create_link(match, match, 'link')
} else {
node = document.createTextNode(match);
}
li.appendChild(node);
message=right;
}
if (message != null) {
node = document.createTextNode(message);
li.appendChild(node);
}
if (typeof b[i].pictures_path != "undefined") {
node = create_link(mlink, mlink, 'obraz');
li.appendChild(node);
}
if (typeof blip_widget.bullet == "string") {
node = document.createTextNode(blip_widget.bullet);
li.appendChild(create_link(mlink, mlink, 'blip', node));
}
blipentries.appendChild(li);
}
blipblock.innerHTML = blip_widget.heading;
blipblock.appendChild(blipentries);
if (typeof blip_widget.parent_disp == "string")
blipblock.parentNode.style.display = blip_widget.parent_disp;
};
var doc_create_element = null;
(function(){
var ns=null;
//TODO blog
if (document.createElementNS &&
(ns = document.getElementsByTagName('html')[0].getAttribute('xmlns')) != null)
doc_create_element = function(e) {
return document.createElementNS(ns,e);
};
else
doc_create_element = function(e) {
return document.createElement(e);
};
if (typeof blip_widget == "undefined") {
alert("Wklejka blip nie została skonfigurowana.");
} else {
if (typeof blip_widget.heading == "undefined")
blip_widget.heading = "na blipie";
if (typeof blip_widget.entries == "undefined")
blip_widget.entries = 5;
if (typeof blip_widget.icons == "undefined")
blip_widget.icons = {};
var data_src = 'http://api.blip.pl/users/'+ blip_widget.username +
'/statuses.json?callback=show_blip&limit='+ blip_widget.entries +
'nocache=' + Math.round(Math.random()*(1<<30));
/* nocache= zabezpiecza przed zatykaniem cache'u na blipie */
if (document.write && (document.getElementById('blip') == null)) try {
document.write('<div id="blip"></div>');
} catch(e) { }
if (window.attachEvent)
window.attachEvent('onload', function(ev) {
blip_load_data(data_src);
});
else
window.addEventListener('load', function(ev) {
blip_load_data(data_src);
}, false);
}})();