feat: add Amazon view product keybinding

This commit is contained in:
Maddison Hellstrom 2019-05-29 23:48:37 -07:00
parent bf13013077
commit 4cab1366fc
2 changed files with 37 additions and 3 deletions

View File

@ -85,14 +85,43 @@ actions.togglePdfViewer = () => chrome.storage.local.get("noPdfViewer", (resp) =
}
})
// Site-specific actions
// =====================
// FakeSpot
// --------
actions.fakeSpot = (url = util.getCurrentLocation("href")) =>
actions.openLink(`http://fakespot.com/analyze?ra=true&url=${url}`, { newTab: true, active: false })()
// Site-specific actions
// =====================
// Amazon
// -----
actions.az = {}
actions.az.viewProduct = () => {
const reHost = /^([-\w]+[.])*amazon.\w+$/
const rePath = /^(?:.*\/)*(?:dp|gp\/product)(?:\/(?<asin>\w{10})).*/
const elements = {}
document.querySelectorAll("a[href]").forEach((a) => {
const u = new URL(a.href)
if (u.hash.length === 0 && reHost.test(u.hostname)) {
const rePathRes = rePath.exec(u.pathname)
if (rePathRes === null) {
return
}
if (!util.isElementInViewport(a)) {
return
}
if (elements[rePathRes.groups.asin] !== undefined) {
if (!(elements[rePathRes.groups.asin].text.trim().length === 0
&& a.text.trim().length > 0)) {
return
}
}
elements[rePathRes.groups.asin] = a
}
})
Hints.create(Object.values(elements), Hints.dispatchMouseClick)
}
// Godoc
// -----
actions.viewGodoc = () => actions.openLink(`https://godoc.org/${actions.getURLPath({ count: 2, domain: true })}`, { newTab: true })()

View File

@ -125,6 +125,11 @@ const maps = {
description: "Fakespot",
callback: actions.fakeSpot,
},
{
alias: "a",
description: "View product",
callback: actions.az.viewProduct,
},
],
"www.google.com": [