X: прокрутка твитов клавишами J/K
Позволяет плавно переходить между твитами клавишами J (вниз) и K (вверх) без мыши.
Код для копирования
// Smoothly scroll tweet-by-tweet with the J and K keys
function tweets() {
return Array.prototype.slice.call(document.querySelectorAll('[data-testid="tweet"]'));
}
function current(list) {
for (let i = 0; i < list.length; i++) {
if (list[i].getBoundingClientRect().top > 60) { return i; }
}
return list.length - 1;
}
document.addEventListener('keydown', function (e) {
const tag = (e.target.tagName || '').toLowerCase();
if (tag === 'input' || tag === 'textarea' || e.target.isContentEditable) { return; }
if (e.key !== 'j' && e.key !== 'k') { return; }
const list = tweets();
if (!list.length) { return; }
let idx = current(list);
idx += (e.key === 'j') ? 1 : -1;
idx = Math.max(0, Math.min(list.length - 1, idx));
list[idx].scrollIntoView({ behavior: 'smooth', block: 'start' });
});
Как использовать этот пример
- Скопируй код кнопкой выше.
- Установи JustZix (2 минуты) и открой расширение на нужной странице.
- Добавь новое правило, совпадающее с этой страницей.
- Вставь код в панель JavaScript правила и сохрани — он запускается при каждом заходе на страницу.
Оцени этот пример
Оценок пока нет — оцени первым.