← 全部示例

JavaScript 效率 producthunt.com

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); }
  });
})();

如何使用此示例

  1. 用上方按钮复制代码。
  2. 安装 JustZix(2 分钟),在目标页面打开扩展。
  3. 添加一条匹配该页面的新规则。
  4. 将代码粘贴到规则的 JavaScript 面板并保存 — 每次访问页面时都会运行。

为此示例评分

暂无评分 — 成为第一个。

此示例有效吗?

没有可粘贴的地方,代码片段毫无用处。

JustZix 安装只需 2 分钟,并在每个匹配的页面上运行你的代码。无需账户、无需付费。

免费下载 查看应用场景