Compare commits

..

2 Commits

Author SHA1 Message Date
1d25b4095f Set branch-specific URIs 2025-04-19 01:07:14 -04:00
a1f4d0ff26 Rewrite 2025-04-19 01:07:12 -04:00
2 changed files with 45 additions and 76 deletions

View File

@ -2,35 +2,30 @@
// @name ycombinator-keys // @name ycombinator-keys
// @namespace https://gitea.amine-bouabdallaoui.fr/AmineB/ycombinator-keys // @namespace https://gitea.amine-bouabdallaoui.fr/AmineB/ycombinator-keys
// @license gpl-3.0-only // @license gpl-3.0-only
// @match *://news.ycombinator.com/* // @match http*://news.ycombinator.com/*
// @icon https://gitea.amine-bouabdallaoui.fr/AmineB/ycombinator-keys/raw/branch/main/icons/48.png // @icon https://gitea.amine-bouabdallaoui.fr/AmineB/ycombinator-keys/raw/branch/main/icons/48.png
// @grant none // @grant none
// @version 4 // @version 4
// @author AmineB // @author AmineB
// @description Ycombinator keyboard nav. // @description Ycombinator keyboard nav.
// @downloadURL https://gitea.amine-bouabdallaoui.fr/AmineB/ycombinator-keys/raw/branch/main/keyboard-watcher.user.js // @downloadURL https://gitea.amine-bouabdallaoui.fr/brian6932/ycombinator-keys/raw/branch/main/keyboard-watcher.js
// @updateURL https://gitea.amine-bouabdallaoui.fr/AmineB/ycombinator-keys/raw/branch/main/keyboard-watcher.user.js // @updateURL https://gitea.amine-bouabdallaoui.fr/brian6932/ycombinator-keys/raw/branch/main/keyboard-watcher.js
// ==/UserScript== // ==/UserScript==
// jshint esversion: 11 // jshint esversion: 11
globalThis.document.styleSheets[0].insertRule('tbody tr.athing>td.title>span.titleline>a:focus,tbody tr.athing.comtr a.togg.clicky:focus{outline: none}') let selected = -1
globalThis.document.styleSheets[0].insertRule('tbody tr.athing>td.title>span.titleline>a:focus,.athing.comtr a.togg.clicky:focus{outline: none}')
const requery = () => globalThis.document.querySelectorAll('tbody tr.athing') const requery = () => globalThis.document.querySelectorAll('tbody tr.athing')
let let query = requery()
query = requery(),
selected = -1,
lastCollapsed
const const
input = new Set()
.add('INPUT')
.add('TEXTAREA'),
// This is a Firefox only option. For some reason older versions of Firefox can't set outline: none. // This is a Firefox only option. For some reason older versions of Firefox can't set outline: none.
focusInvisible = { __proto__: null, focusVisible: false }, focusInvisible = { __proto__: null, focusVisible: false },
isComment = () => query[selected].classList.contains('comtr'),
highlightSelected = () => { highlightSelected = () => {
query[selected].style.boxShadow = '0px 0px 10px 4px rgba(0,0,0,0.73)' query[selected].style.boxShadow = '0px 0px 10px 4px rgba(0,0,0,0.73)'
isComment() ? globalThis.location.pathname === '/item' ?
// Drawing the boxShadow on this selector isn't visible enough. // Drawing the boxShadow on this selector isn't visible enough.
query[selected].querySelector('a.togg.clicky')?.focus(focusInvisible) query[selected].querySelector('a.togg.clicky')?.focus(focusInvisible)
: :
@ -38,74 +33,48 @@ const
query[selected].lastChild.firstChild.firstChild.focus(focusInvisible) query[selected].lastChild.firstChild.firstChild.focus(focusInvisible)
}, },
keydown = event => { keydown = event => {
if (!input.has(event.target.tagName)) switch (event.key) {
switch (event.key) { case 'j':
case 'j': if (selected + 1 === query.length)
const lastIndex = query.length - 1
if (selected === lastIndex)
return
if (selected !== -1)
query[selected].style.boxShadow = ''
let hiddenChain = 0
while (selected < lastIndex) {
const hidden = query[++selected].classList.contains('noshow')
hiddenChain = hiddenChain * hidden + hidden
if (!hidden) {
lastCollapsed = query[selected].classList.contains('coll') ? selected : undefined
break
}
}
selected -= hiddenChain * (selected - hiddenChain === lastCollapsed)
highlightSelected()
return return
case 'k':
if (selected <= 0)
return
if (selected !== -1)
query[selected].style.boxShadow = '' query[selected].style.boxShadow = ''
while (selected >= 0 && query[--selected].classList.contains('noshow')); while (selected < query.length && query[++selected].classList.contains('noshow'));
highlightSelected() highlightSelected()
return
case 'k':
if (selected <= 0)
return return
case 'h':
if (globalThis.location.pathname === '/hidden')
for (const selector of query[selected].nextSibling.querySelectorAll('a'))
if (selector.innerText === 'un-hide') {
selector.click()
break
}
else
query[selected].nextSibling.querySelector('a.clicky.hider')?.click()
if (selected + 1 <= query.length) query[selected].style.boxShadow = ''
highlightSelected() while (selected >= 0 && query[--selected].classList.contains('noshow'));
return highlightSelected()
case 'c': return
globalThis.open('https://news.ycombinator.com/item?id=' + query[selected].id, '_self') case 'h':
return if (globalThis.location.pathname === '/hidden')
case 'C': for (const selector of query[selected].nextSibling.querySelectorAll('a'))
globalThis.open('https://news.ycombinator.com/item?id=' + query[selected].id) if (selector.innerText === 'un-hide') {
return selector.click()
case 'r': break
if (!isComment()) }
return else
globalThis.open(query[selected].querySelector('div.reply a').href, '_self') query[selected].nextSibling.querySelector('a.clicky.hider')?.click()
return highlightSelected()
case 'R': return
if (!isComment()) case 'c':
return globalThis.open('https://news.ycombinator.com/item?id=' + query[selected].id, '_self')
globalThis.open(query[selected].querySelector('div.reply a').href) return
return case 'C':
case 'u': globalThis.open('https://news.ycombinator.com/item?id=' + query[selected].id)
globalThis.open((isComment() ? query[selected] : query[selected].nextSibling).querySelector('a.hnuser').href, '_self') return
return case 'u':
case 'U': globalThis.open(globalThis.location.pathname === '/item' ? query[selected].querySelector('a.hnuser').href : query[selected].nextSibling.querySelector('a.hnuser').href, '_self')
globalThis.open((isComment() ? query[selected] : query[selected].nextSibling).querySelector('a.hnuser').href) return
} case 'U':
globalThis.open(globalThis.location.pathname === '/item' ? query[selected].querySelector('a.hnuser').href : query[selected].nextSibling.querySelector('a.hnuser').href)
}
}, },
listen = () => globalThis.document.addEventListener('keydown', keydown) listen = () => globalThis.document.addEventListener('keydown', keydown)

View File

@ -12,7 +12,7 @@
"*://news.ycombinator.com/*" "*://news.ycombinator.com/*"
], ],
"js": [ "js": [
"keyboard-watcher.user.js" "keyboard-watcher.js"
] ]
} }
], ],