-
Notifications
You must be signed in to change notification settings - Fork 1
/
HNRelevant.user.js
464 lines (401 loc) · 16.4 KB
/
HNRelevant.user.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
// ==UserScript==
// @name HNRelevant
// @version 1.1.0
// @description Enhance your Hacker News experience and gain new insights by exploring relevant stories and discussions
// @author imdj
// @match *://news.ycombinator.com/item*
// @connect *://hn.algolia.com/*
// @icon https://raw.githubusercontent.com/imdj/HNRelevant/main/icon.png
// @updateURL https://raw.githubusercontent.com/imdj/HNRelevant/main/HNRelevant.user.js
// @downloadURL https://raw.githubusercontent.com/imdj/HNRelevant/main/HNRelevant.user.js
// @license MIT
// @run-at document-start
// @grant none
// @inject-into content
// ==/UserScript==
let searchQuery = {
mode: "auto", // "auto" or "manual"
rawQuery: "",
query: "",
type: "similar", // "similar" or "verbatim"
numOfResults: 15,
date: {
start: 0,
end: Math.floor(new Date().getTime() / 1000)
}
};
let itemId = (new URLSearchParams(document.location.search)).get("id");
function optimizeSearchQuery() {
// Remove punctuation
searchQuery.query = searchQuery.rawQuery.replace(/[,;:.!?'"]/g, '');
searchQuery.query = stripYearFromTitle(searchQuery.query);
// Remove HN common keywords
let HNWords = ['Ask HN', 'Tell HN', 'Show HN', 'Launch HN'];
searchQuery.query = HNWords.reduce((str, word) => str.replace(new RegExp(word, 'gi'), ''), searchQuery.query);
// Tokenize the sentence into words
let words = searchQuery.query.toLowerCase().split(' ');
// Remove stop words
let stopWords = ['a', 'an', 'the', 'is', 'are', 'was', 'and', 'or'];
words = words.filter(w => !stopWords.includes(w));
// Apply stemming to the words
let stems = words.map(w => {
if (w.length < 3) return w;
if (w.endsWith('ies') && w.length > 4) w = w.slice(0, -3) + 'y';
if (w.endsWith('es') && w.length > 3) w = w.slice(0, -2);
if (w.endsWith('s') && w.length > 2) w = w.slice(0, -1);
return w;
});
return stems.join(' ');
}
async function searchHackerNews() {
searchQuery.query = optimizeSearchQuery();
const url = `https://hn.algolia.com/api/v1/search`
+ (searchQuery.type === 'verbatim' ? `?query=${encodeURIComponent(searchQuery.rawQuery)}` : `?similarQuery=${encodeURIComponent(searchQuery.query)}`)
+ `&tags=story`
+ `&hitsPerPage=${searchQuery.numOfResults}`
+ `&filters=NOT objectID:` + itemId // exclude current submission
+ `&numericFilters=created_at_i>${searchQuery.date.start},created_at_i<${searchQuery.date.end}` // filter by date
;
const response = await fetch(url).then(response => response.json());
return response;
}
// Get relative time from timestamp
function timestampToRelativeTime(timestamp) {
const now = new Date();
const date = new Date(timestamp);
const diff = now - date;
let rtf = new Intl.RelativeTimeFormat('en', { numeric: 'always' });
const units = {
year: 365 * 24 * 60 * 60 * 1000,
month: 30 * 24 * 60 * 60 * 1000,
day: 24 * 60 * 60 * 1000,
hour: 60 * 60 * 1000,
minute: 60 * 1000
};
for (const unit in units) {
if (diff > units[unit]) {
const time = Math.round(diff / units[unit]);
return rtf.format(-time, unit);
}
}
return rtf.format(-Math.round(diff / 1000), 'second');
}
// i.e. "Title (2021)" -> "Title"
function stripYearFromTitle(title) {
return title.replace(/\s\(\d{4}\)$/, '');
}
// Render dom element for a search result
function displayResult(object) {
const element = document.createElement('li');
element.className = 'result';
const titleContainer = document.createElement('span');
titleContainer.style.display = 'block';
titleContainer.classList.add('titleline');
const link = document.createElement('a');
link.href = object.url ? object.url : 'item?id=' + object.objectID;
link.textContent = object.title;
link.rel = 'no-referrer';
titleContainer.appendChild(link);
if (object.url) {
const domainContainer = document.createElement('span');
domainContainer.classList.add('sitebit', 'comhead');
const domain = document.createElement('a');
domain.href = 'from?site=' + (new URL(object.url)).hostname.replace('www.', '');
const domainChild = document.createElement('span');
domainChild.classList.add('sitestr');
domainChild.textContent = (new URL(object.url)).hostname.replace('www.', '');
domain.appendChild(domainChild);
domainContainer.appendChild(domain);
domain.insertAdjacentText('beforebegin', ' (');
domain.insertAdjacentText('afterend', ')');
titleContainer.appendChild(domainContainer);
}
element.appendChild(titleContainer);
const description = document.createElement('span');
description.className = 'subtext';
const author = document.createElement('a');
author.href = 'user?id=' + object.author;
author.textContent = object.author;
const comments = document.createElement('a');
comments.href = 'item?id=' + object.objectID;
comments.textContent = object.num_comments + ' comments';
description.insertAdjacentText('afterbegin',
object.points + ' points '
+ 'by '
);
description.appendChild(author);
description.insertAdjacentText('beforeend', ' | ');
const timeurl = document.createElement('a');
timeurl.href = 'item?id=' + object.objectID;
timeurl.title = object.created_at;
const time = document.createElement('time');
time.dateTime = object.created_at;
time.textContent = timestampToRelativeTime(object.created_at);
timeurl.appendChild(time);
description.appendChild(timeurl);
description.insertAdjacentText('beforeend', ' | ');
description.appendChild(comments);
element.appendChild(description);
return element;
}
function updateDateRange() {
const dateRange = document.getElementById('dateRangeDropdown').value;
const startDate = document.getElementById('startDate').value;
const endDate = document.getElementById('endDate').value;
searchQuery.date.end = Math.floor(new Date().getTime() / 1000);
switch (dateRange) {
case 'Past week':
searchQuery.date.start = searchQuery.date.end - 604800;
break;
case 'Past month':
searchQuery.date.start = searchQuery.date.end - 2592000;
break;
case 'Past year':
searchQuery.date.start = searchQuery.date.end - 31536000;
break;
case 'Custom':
searchQuery.date.start = Math.floor(new Date(startDate).getTime() / 1000) || 0;
searchQuery.date.end = Math.floor(new Date(endDate).getTime() / 1000) || searchQuery.date.end;
break;
default:
searchQuery.date.start = 0;
}
}
// Update sidebar content
function updateResults() {
document.getElementById('hnrelevant-results').innerHTML = '';
searchHackerNews().then((result) => {
const list = document.createElement('ul');
list.id = 'hnrelevant-results-list';
// if no results, display a message
if (result.hits.length === 0) {
const element = document.createElement('li');
element.className = 'result';
element.style = 'padding: 5px 0; text-align: center; white-space: pre-line;';
element.textContent = searchQuery.type === 'verbatim' ? 'No matching results found.\r\nTry a different query or switch to \'Similar\' search.' : 'No results found. Try to customize the query.';
list.appendChild(element);
}
else {
result.hits.forEach(hit => {
const element = displayResult(hit);
list.appendChild(element);
});
}
document.getElementById('hnrelevant-results').appendChild(list);
});
}
const style = `
#hnrelevant-controls-container, #hnrelevant-controls {
display: flex;
gap: 10px
}
#hnrelevant-controls-container {
flex-direction: column;
}
#hnrelevant-controls {
flex-direction: row;
flex-wrap: wrap;
align-items: center;
}
#query-customization-container {
display: flex;
flex-direction: row;
align-items: center;
margin: 5px 0;
padding-right: 10px;
}
#queryCustomization {
flex-grow: 1;
}
#hnrelevant-results-list {
list-style: none;
padding: 0;
width: 100%;
display: flex;
flex-direction: column;
}
#hnrelevant-results-list .result {
padding: 5px 0;
min-width: 280px;
}
@media screen and (max-width: 1200px) {
#hnrelevant-results {
display: grid;
}
#hnrelevant-results-list {
flex-direction: row;
gap: 10px;
overflow-x: auto;
}
#hnrelevant-results-list .result {
display: flex;
flex-direction: column;
justify-content: space-between;
border: 1px solid #d3d3d3;
border-radius: 5px;
padding: 5px;
}
}
`;
const relevantContent = `
<h2 id="hnrelevant-header">Relevant Submissions</h2>
<div id="hnrelevant-controls-container">
<div id="query-customization-container">
<input id="queryCustomization" placeholder="${searchQuery.rawQuery}" value="${searchQuery.query}">
<button type="submit" id="submitCustomization" style="margin-left: 5px;">Submit</button>
</div>
<details>
<summary>The results aren't good?</summary>
<p>Try the following:
<ul>
<li>Omit years and numbers</li>
<li>Remove irrelevant words to avoid noise</li>
<li>Scrap the title and use a custom query instead</li>
</ul>
</p>
</details>
<div id="hnrelevant-controls">
<div>
<label for="numOfResultsDropdown">Num of results</label>
<select style="margin-left: 5px;" id="numOfResultsDropdown">
<option value="5">5</option>
<option value="10">10</option>
<option value="15" selected>15</option>
<option value="20">20</option>
<option value="30">30</option>
</select>
</div>
<div>
<label for="dateRangeDropdown">Date</label>
<select style="margin-left: 5px;" id="dateRangeDropdown">
<option value="Past week">Past week</option>
<option value="Past month">Past month</option>
<option value="Past year">Past year</option>
<option value="All time" selected>All time</option>
<option value="Custom">Custom</option>
</select>
</div>
<div id="dateRangeInputContainer" style="display: none;">
<div style="display: flex; flex-direction: row; align-items: center; gap: 5px;">
<label for="startDate">Start</label>
<input type="date" id="startDate" style="margin-left: 5px;">
</div>
<div style="display: flex; flex-direction: row; align-items: center; gap: 5px;">
<label for="endDate">End</label>
<input type="date" id="endDate" style="margin-left: 5px;">
</div>
</div>
<fieldset style="border: none; padding: 0; display: flex; flex-direction: row; align-items: center; justify-content: flex-start; gap: 5px;">
<legend style="float: left; margin-bottom: 5px;">Search type</legend>
<div style="display: inline-block;">
<input type="radio" id="verbatim" name="searchType" value="verbatim" style="margin-left: 5px;">
<label for="verbatim">Verbatim</label>
</span>
<div style="display: inline-block;">
<input type="radio" id="similar" name="searchType" value="similar" checked style="margin-left: 5px;">
<label for="similar">Similar</label>
</span>
</fieldset>
</div>
</div>
<div id="hnrelevant-results" style="width: 100%;">
</div>
`;
function updateData(key, value) {
searchQuery[key] = value;
if (searchQuery.mode === 'auto') {
updateResults();
}
}
function installSection() {
// Submissions and Comments share the same page URL
// Abort if we are not on a submission page
if (!document.querySelector('.fatitem .titleline')) {
return;
}
const hnBody = document.querySelector('#hnmain > tbody');
let NavbarIndex = 0;
const rows = hnBody.querySelectorAll("tr");
// handle special case if death banner is present
if (rows[0].querySelector('td img[src="s.gif"]')) {
rows[0].querySelector("td").setAttribute("colspan", "2");
NavbarIndex = 1;
}
// Since we add a new column to the table for the sidebar, we need to make navbar span all columns
const hnNavBar = hnBody.children[NavbarIndex];
hnNavBar.children[0].setAttribute('colspan', '2');
const hnContent = hnBody.children[NavbarIndex + 2];
searchQuery.rawQuery = hnBody.querySelector('.fatitem .titleline > a').textContent;
// Make sure all table data elements are aligned to the top
// (they're centered vertically by default which causes problem when coupled with long sidebar)
hnBody.querySelectorAll(':scope > tr > td').forEach(td => td.style.verticalAlign = 'top');
if (window.innerWidth < 1200) {
const tr = document.createElement('tr');
const td = document.createElement('td');
td.innerHTML = relevantContent;
td.style = 'padding-top: 1rem;';
tr.innerHTML = '<td></td><td></td>';
tr.appendChild(td);
const submissionMetadata = hnContent.querySelector('table.fatitem > tbody');
submissionMetadata.appendChild(tr);
} else {
const td = document.createElement('td');
td.style = 'min-width: 280px; width: 25%; vertical-align: baseline; padding-left: 10px;';
td.innerHTML = relevantContent;
hnContent.appendChild(td);
}
// inject styles
const styleElement = document.createElement('style');
styleElement.textContent = style;
document.head.appendChild(styleElement);
document.getElementById('queryCustomization').placeholder = searchQuery.rawQuery;
document.getElementById('queryCustomization').value = searchQuery.rawQuery;
document.getElementById('numOfResultsDropdown').value = searchQuery.numOfResults;
document.getElementsByName('searchType').forEach((radio) => {
radio.checked = radio.value === searchQuery.type;
});
if (searchQuery.mode === 'auto') {
updateResults();
}
document.getElementById('numOfResultsDropdown').addEventListener('change', () => {
updateData('numOfResults', document.getElementById('numOfResultsDropdown').value);
});
document.getElementById('dateRangeDropdown').addEventListener('change', () => {
updateDateRange();
updateData('date', searchQuery.date);
});
document.getElementById('startDate').addEventListener('change', () => {
updateDateRange();
updateData('date', searchQuery.date);
});
document.getElementById('endDate').addEventListener('change', () => {
updateDateRange();
updateData('date', searchQuery.date);
});
document.getElementById('dateRangeDropdown').addEventListener('change', (event) => {
if (event.target.value === 'Custom') {
document.getElementById('dateRangeInputContainer').style = 'display: flex; flex-direction: row; gap: 5px;';
} else {
document.getElementById('dateRangeInputContainer').style.display = 'none';
}
});
document.getElementsByName('searchType').forEach((radio) => {
radio.addEventListener('change', (event) => {
updateData('type', event.target.value);
});
});
document.getElementById('submitCustomization').addEventListener('click', () => {
searchQuery.rawQuery = document.getElementById('queryCustomization').value;
updateResults();
});
document.getElementById('queryCustomization').addEventListener('keyup', (event) => {
if (event.key === 'Enter') {
searchQuery.rawQuery = document.getElementById('queryCustomization').value;
updateResults();
}
});
}
window.addEventListener('load', () => {
'use strict';
installSection();
});