Product Hunt:键盘浏览信息流
用 J 和 K 键在信息流帖子间跳转,并高亮当前选中的产品。
可复制的代码
// Navigate feed posts with the J and K keys
(function () {
function posts() {
return Array.from(document.querySelectorAll('[data-test^="post-item"]'));
}
let idx = -1;
function go(step) {
const list = posts();
if (!list.length) { return; }
idx = Math.max(0, Math.min(list.length - 1, idx + step));
const el = list[idx];
if (el) {
el.scrollIntoView({ behavior: 'smooth', block: 'center' });
el.style.outline = '2px solid #da552f';
setTimeout(function () { el.style.outline = ''; }, 800);
}
}
document.addEventListener('keydown', function (e) {
if (e.target && /^(INPUT|TEXTAREA)$/.test(e.target.tagName)) { return; }
if (e.key === 'j') { go(1); }
else if (e.key === 'k') { go(-1); }
});
})();
如何使用此示例
- 用上方按钮复制代码。
- 安装 JustZix(2 分钟),在目标页面打开扩展。
- 添加一条匹配该页面的新规则。
- 将代码粘贴到规则的 JavaScript 面板并保存 — 每次访问页面时都会运行。
为此示例评分
暂无评分 — 成为第一个。