-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkarma.js
82 lines (75 loc) · 2.97 KB
/
karma.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
function getUserInfo(data, type) {
return parseInt($(data).find(type).text()).toString() === 'NaN' ? '' : $(data).find(type).text();
}
if (!document.getElementById('habratooltip')) {
////$(document).ready(function(){
//
// logged in user karma
//
var nickName = $('div.top a.username.dark');
if (nickName.text() != '') {
chrome.extension.sendRequest(
{
'action': 'getKarma',
'user': nickName.text()
},
function(data) {
var rating = getUserInfo(data, 'rating');
var karma = getUserInfo(data, 'karma');
nickName.after(' <span style="color: #C6C">' + rating + '<span>');
nickName.after(' <span style="color: #6C6">' + karma + '<span>');
}
);
}
//
// authors karma
// appear only when visible to prevent multiple xhr to server
// server response 503 if more then 3-5 requests in one time
//
$(window).scroll(function() {
if ($('div.author a').size() == $('.karmaloaded').size()) {
$(window).unbind('scroll');
}
$('div.author a:not(.karmaloaded)').each(function() {
// console.log($(window).scrollTop(), $(window).height(), $(window).scrollTop() + $(window).height(), $(this).offset().top, $(this).height());
// console.log($(window).scrollTop() > ($(this).offset().top + $(this).height()));
if ($(window).scrollTop() + $(window).height() > $(this).offset().top) {
var nickName = $(this);
nickName.addClass('karmaloaded');
chrome.extension.sendRequest(
{'action': 'getKarma', 'user': nickName.text()},
function(data) {
var rating = getUserInfo(data, 'rating');
var karma = getUserInfo(data, 'karma');
nickName.after(' <span style="color: #C6C; font-weight: 700;">' + rating + '<span>');
nickName.after(' <span style="color: #6C6; font-weight: 700;">' + karma + '<span>');
});
}
});
});
$(window).scroll();
//
// all users on page tooltips with karma
//
$('body').append('<div id="habratooltip"><span id="htkarma" /> <span id="htrating" /></div>');
$('a.username, a.user_name, dt.who a').hover(function(ev) {
var el = $(this);
chrome.extension.sendRequest(
{
'action': 'getKarma',
'user': el.text()
},
function(data) {
$('#htkarma').html(getUserInfo(data, 'karma'));
$('#htrating').html(getUserInfo(data, 'rating'));
$('#habratooltip').css({
'left': ev.pageX ,
'top': ev.pageY - 30
}).show();
}
);
}, function() {
$('#habratooltip').hide();
});
//});
}