From f246b0e9baa80eb4ea222242d9075cbd354c1953 Mon Sep 17 00:00:00 2001 From: Maddison Hellstrom Date: Tue, 15 Dec 2020 21:54:11 -0800 Subject: [PATCH] [src:feat] scroll to URL hash --- actions.js | 9 +++++++++ keys.js | 6 ++++++ 2 files changed, 15 insertions(+) diff --git a/actions.js b/actions.js index a081d56..e76193a 100644 --- a/actions.js +++ b/actions.js @@ -93,6 +93,15 @@ actions.showSpeedReader = () => { document.body.appendChild(script) } +actions.scrollToHash = (hash = null) => { + const h = (hash || document.location.hash).replace("#", "") + const e = document.getElementById(h) || document.querySelector(`[name="${h}"]`) + if (!e) { + return + } + e.scrollIntoView({ behavior: "smooth" }) +} + // Surfingkeys-specific actions // ---------------------------- actions.createHint = (selector, action) => () => { diff --git a/keys.js b/keys.js index f6472cb..1b47424 100644 --- a/keys.js +++ b/keys.js @@ -183,6 +183,12 @@ const maps = { category: categories.tabs, description: "Close tab to right", }, + { + alias: "gh", + category: categories.clipboard, + description: "Scroll to element targeted by URL hash", + callback: actions.scrollToHash, + }, ], "amazon.com": [