-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathpull.js
More file actions
60 lines (47 loc) · 1.38 KB
/
pull.js
File metadata and controls
60 lines (47 loc) · 1.38 KB
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
/*globals chrome*/
/**
* When in the PR view, record if we see a :+1: from my user
*/
(function () {
// ["/teambox/Teambox-hosted/pull/1155", "teambox", "Teambox-hosted", "1155"]
var matches = location.pathname.match(/\/(.*?)\/(.*?)\/pull\/(\d+)/)
, pr_key = location.pathname;
// Not a PR URL? GTFO
if (!matches) {
return;
}
// Count votes by others
function usersWhoVoted() {
var voted_by = [];
$(".comment-header-author").each(function () {
if ($(this).parents(".discussion-bubble-inner").find("img[title=':+1:']").length) {
voted_by.push($(this).text());
}
});
return _(voted_by).uniq();
}
// Persist the array of users who voted (only if different, to avoid quota limits)
function saveVotesCount() {
var hash = {};
hash[pr_key] = usersWhoVoted();
chrome.storage.sync.get(pr_key, function (stored) {
console.log(hash[pr_key], stored[pr_key], hash[pr_key] !== stored[pr_key]);
if (hash[pr_key] !== stored[pr_key]) {
chrome.storage.sync.set(hash);
}
});
}
$(function () {
saveVotesCount();
// Dirty, but it works: update after submitting a comment
$("form").submit(function () {
setTimeout(function () {
saveVotesCount();
}, 2000);
});
// Save when closing window
$(window).unload(function () {
saveVotesCount();
});
});
}());