diff --git a/src/excmds.ts b/src/excmds.ts index 327847d2..ca3300d8 100644 --- a/src/excmds.ts +++ b/src/excmds.ts @@ -1027,6 +1027,7 @@ import * as hinting from './hinting_background' - -i view an image - -I view an image in a new tab - -; focus an element + - -# yank an element's anchor URL to clipboard */ //#background export function hint(option?: string) { @@ -1036,6 +1037,7 @@ export function hint(option?: string) { else if (option === "-i") hinting.hintImage(false) else if (option === "-I") hinting.hintImage(true) else if (option === "-;") hinting.hintFocus() + else if (option === "-#") hinting.hintPageAnchorYank() else hinting.hintPageSimple() } diff --git a/src/hinting.ts b/src/hinting.ts index f021b5e6..6415ada2 100644 --- a/src/hinting.ts +++ b/src/hinting.ts @@ -211,6 +211,14 @@ function hintableImages() { isVisible) } +/** Get arrat of "anchors": elements which have id or name and can be addressed + * with the hash/fragment in the URL + */ +function anchors() { + return Array.from(document.querySelectorAll(HINTTAGS_anchor_selectors)) + .filter(isVisible) +} + // CSS selectors. More readable for web developers. Not dead. Leaves browser to care about XML. const HINTTAGS_selectors = ` input:not([type=hidden]):not([disabled]), @@ -265,6 +273,11 @@ img, [src] ` +const HINTTAGS_anchor_selectors = ` +[id], +[name] +` + import {activeTab, browserBg, l, firefoxVersionAtLeast} from './lib/webext' async function openInBackground(url: string) { @@ -327,6 +340,20 @@ function hintPageYank() { }) } +/** Hint anchors and yank the URL on selection + */ +function hintPageAnchorYank() { + + hintPage(anchors(), hint=>{ + + let anchorUrl = new URL(window.location.href) + + anchorUrl.hash = hint.target.id || hint.target.name; + + messageActiveTab("commandline_frame", "setClipboard", [anchorUrl.href]) + }) +} + /** Hint images, opening in the same tab, or in a background tab * * @param inBackground opens the image source URL in a background tab, @@ -366,6 +393,7 @@ addListener('hinting_content', attributeCaller({ hintPageSimple, hintPageYank, hintPageTextYank, + hintPageAnchorYank, hintPageOpenInBackground, hintImage, hintFocus, diff --git a/src/hinting_background.ts b/src/hinting_background.ts index f3c0f8d1..ac05b8a9 100644 --- a/src/hinting_background.ts +++ b/src/hinting_background.ts @@ -19,6 +19,11 @@ export async function hintPageYank() { export async function hintPageTextYank() { return await messageActiveTab('hinting_content', 'hintPageTextYank') } + +export async function hintPageAnchorYank() { + return await messageActiveTab('hinting_content', 'hintPageAnchorYank') +} + export async function hintPageSimple() { return await messageActiveTab('hinting_content', 'hintPageSimple') } diff --git a/src/parsers/normalmode.ts b/src/parsers/normalmode.ts index 131fec96..5027ecb2 100755 --- a/src/parsers/normalmode.ts +++ b/src/parsers/normalmode.ts @@ -61,6 +61,7 @@ export const DEFAULTNMAPS = { ";y": "hint -y", ";p": "hint -p", ";;": "hint -;", + ";#": "hint -#", "I": "mode ignore", "a": "current_url bmark", "A": "bmark",