Add yank anchor hint submode (;#)

THis yanks the page URL with the hinted elements id or title as a hash
fragment, which can be used to link to the page at that element's
location.

For example: https://en.wikipedia.org/wiki/Vim_(text_editor)#History

;# nmap added, from Vimperator
This commit is contained in:
John Beard 2017-11-28 22:51:53 +00:00
parent 2be3646b21
commit a3ca5cd2c1
4 changed files with 36 additions and 0 deletions

View file

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

View file

@ -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,

View file

@ -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')
}

View file

@ -61,6 +61,7 @@ export const DEFAULTNMAPS = {
";y": "hint -y",
";p": "hint -p",
";;": "hint -;",
";#": "hint -#",
"I": "mode ignore",
"a": "current_url bmark",
"A": "bmark",