Skip to content

Instantly share code, notes, and snippets.

@sairion
Last active April 15, 2017 13:19
Show Gist options
  • Save sairion/a156f18e486f74d31277fb4d3fe068af to your computer and use it in GitHub Desktop.
Save sairion/a156f18e486f74d31277fb4d3fe068af to your computer and use it in GitHub Desktop.
trim and fetch some hn stuff
/*
function setCustomStyle(content) {
let style = q('#custom-style')
if (q('#custom-style') === null) {
style = document.createElement('style')
style.id = 'custom-style'
document.head.appendChild(style)
}
style.textContent = content
}
setCustomStyle(`
.reply {
display: none;
}
`)*/
//const q = (qry) => document.querySelector(qry)
const qs = (qry) => Array.from(document.querySelectorAll(qry))
const has = (target, key) => target.indexOf(key) !== -1
const hasMultiple = (target, keys) => keys.every(key => has(target, key))
let separator = `\n\n${'-'.repeat(25)}\n\n`
function searchAndMerge(searchKeys) {
// Remove comments (w/ depth > 1) and unnecessary contents.
qs('.reply').forEach(e => {
e.remove()
})
qs(
[1,2,3,4,5,6,7,8,9,10].map(e => `img[width="${e * 40}"]`).join(',')
).forEach(e => {
e.closest('.athing').hidden = true
})
const visibleEls = qs('.athing')
.filter(e => !e.hidden)
.map(el => el.querySelector(':scope .comment'))
const merged = visibleEls.reduce((result, el) => {
if (el) {
const content = el.textContent.trim()
if (hasMultiple(content.toLowerCase(), searchKeys)) {
return result + content + separator
}
}
return result
}, '')
return merged
}
copy(searchAndMerge(['javascript']))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment