This repository has been archived by the owner on Oct 17, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
github-stars.js
293 lines (234 loc) · 8.28 KB
/
github-stars.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
// ==UserScript==
// @name GitHub Stars
// @namespace https://github.com/larrydarrelc/github-stars
// @version 0.0.1
// @description Manage your github starred projects like a boss.
//
// @match https://github.com/stars*
// @match https://github.com/*
// @require http://code.jquery.com/jquery-2.0.3.min.js
// @permissions tabs
// ==/UserScript==
/*global $*/
(function () {
// Helper Setup
// ------------
// Local storage keys' prefix.
var PREFIX = 'gs-';
// Script running environment (gm or crx).
var MODE_GM = 0,
MODE_CRX = 1,
MODE = MODE_CRX;
if ($.hasOwnProperty('facebox')) {
MODE = MODE_GM;
}
// Templating settings.
var TEMPLATE = {
interpolate: /<%=([\w ]+)%>/g
};
// Add comments box markup.
var ADD_COMMENTS_BOX = '' +
'<h2 class="facebox-header">Add comments</h2>' +
'<p><textarea id="add-comments-inp" class="input-block" placeholder="Add some comments">' +
'<%= comments %>' +
'</textarea></p>' +
'<button type="submit" id="add-comments-btn" class="button button-block">Save</button>' +
'<button class="facebox-close">' +
'<span class="octicon octicon-remove-close"></span>' +
'</button>';
// Edit comments button markup.
var EDIT_COMMENTS_BTN = '' +
'<li>' +
'<div>' +
'<a href="#" class="minibutton with-count upwards unstarred" title="Edit comments">' +
'<span class="octicon octicon-heart"></span><span class="text">Comments</span>' +
'</a>' +
'<a class="social-count unstarred">+</a>' +
'</div>' +
'</li>';
// Comments fuzzy match threshold
var MATCH_THRESHOLD = 0.4;
// Comments fuzzy match shortest length, used for improving performance
var MATCH_LENGTH = 2;
// Utilities
// ---------
// k-v based comments service (using localStorage)
var Comments = {
realName: function (name) {
return PREFIX + name;
},
get: function (projectName) {
projectName = Comments.realName(projectName);
return localStorage[projectName] || '';
},
set: function (projectName, comments) {
projectName = Comments.realName(projectName);
localStorage[projectName] = comments;
}
};
// a not so fast templating helper
var tmpl = function (markup, data) {
var pattern = TEMPLATE.interpolate,
key;
while ((key = pattern.exec(markup)) !== null) {
markup = markup.replace(key[0], data[key[1].trim()]);
}
return markup;
};
// Compare two string and get a score for similarity.
// Original code from https://github.com/joshaven/string_score
// TODO test cases
var cmp = function (a, b) {
if (a === b) return 1;
if (b === "") return 0;
var runningScore = 0, charScore, finalScore,
aString = a.toLowerCase(), aLength = a.length,
bString = b.toLowerCase(), bLength = b.length,
idxOf, startAt = 0,
fuzzies = 1;
for (var i = 0;i < bLength;i++) {
idxOf = aString.indexOf(bString[i], startAt);
if (idxOf === -1) {
return 0;
} else if (startAt === idxOf) {
charScore = 0.7;
} else {
charScore = 0.1;
if (a[idxOf - 1] === ' ') charScore += 0.8;
}
if (a[idxOf] === b[i]) charScore += 0.1;
runningScore += charScore;
startAt = idxOf + 1;
}
finalScore = 0.5 * (runningScore / aLength + runningScore / bLength) / fuzzies;
if (aString[0] === bString[0] && finalScore < 0.85) finalScore += 0.15;
return finalScore;
};
// Run code via `chrome.tabs.executeScript`
var chromeRunCode = function (snippet, cb) {
cb = cb || function () {};
chrome.runtime.sendMessage({
snippet: snippet
}, cb);
};
// Components Setup
// ----------------
var addComments = function (projectName) {
var originalComments = Comments.get(projectName),
boxTmpl = tmpl(ADD_COMMENTS_BOX, { comments: originalComments });
var bindAddBtn = function() {
var addBtn = document.querySelector('#add-comments-btn'),
commentsInp = document.querySelector('#add-comments-inp');
addBtn.addEventListener('click', function () {
var comments = commentsInp.value;
if (comments != originalComments) {
Comments.set(projectName, comments);
}
if (MODE === MODE_GM) {
$(document).trigger('close.facebox');
} else {
chromeRunCode(tmpl('$(document).trigger("close.facebox");'));
}
});
};
if (MODE === MODE_GM) {
$.facebox(boxTmpl);
bindAddBtn();
} else {
chromeRunCode(tmpl("$.facebox('<%= tmpl %>')", {tmpl: boxTmpl}), bindAddBtn);
}
};
// Page Manipuliation
// ------------------
var isStarsPage = function (path) {
return path === 'stars';
};
var isProjectPage = function (path) {
return path.split('/').length === 2;
};
var starsPage = function () {
var starred_repos = document.querySelectorAll('.repo_list li'),
starred_repos_count = starred_repos.length,
repos = [], curRepo,
ele, name;
// TODO loops function oops?
var curried = function (name) {
return function () {
addComments(name);
};
};
for (var i = 0;i < starred_repos_count;i++) {
ele = starred_repos[i];
name = ele.querySelector('h3 a').innerText;
curRepo = {
ele: ele,
name: name,
comments: Comments.get(name)
};
repos.push(curRepo);
// bind the :star: buttons
curRepo.ele
.querySelector('.unstarred')
.addEventListener('click', curried(curRepo.name));
// inject comments
if (curRepo.comments) {
curRepo.ele.appendChild($('<p>' + curRepo.comments + '</p>')[0]);
}
}
// bind search box
var q = document.querySelector('input#star-repo-search');
// TODO performance check
q.addEventListener('keyup', function () {
var needle = q.value;
if (needle.length === 0) {
repos.forEach(function (repo) {
repo.ele.classList.remove('hidden');
})
}
if (needle.length < MATCH_LENGTH) return;
// compare project name and comments
var cmpRepo = function (repo) {
return ([repo.name, repo.comments].filter(function (a) {
return (cmp(a, needle) >= MATCH_THRESHOLD);
}).length > 0);
};
repos.forEach(function (repo) {
if (cmpRepo(repo)) {
repo.ele.classList.remove('hidden');
} else {
repo.ele.classList.add('hidden');
}
});
});
};
var projectPage = function (projectName) {
// inject edit comments box
document
.querySelector('.pagehead-actions')
.appendChild($(EDIT_COMMENTS_BTN)[0]);
// bind the :star: buttons
var btn = document.querySelectorAll('.unstarred'),
l = btn.length,
e;
for (var i = 0;i < l;i++) {
e = btn[i];
e.addEventListener('click', function () {
addComments(projectName);
})
}
};
// Helper Initialization
// ---------------------
var kick = function () {
var path = location.pathname.substring(1);
// faking a route
if (isStarsPage(path)) {
starsPage();
} else if (isProjectPage(path)) {
projectPage(path);
} else {
console.log('reaching ', path);
}
};
kick();
})();