[source:refactor] DRY up github link selectors

This commit is contained in:
Maddison Hellstrom 2019-11-09 05:36:16 -08:00
parent 78d307101c
commit f8369a8405
1 changed files with 71 additions and 85 deletions

View File

@ -189,10 +189,8 @@ actions.gh.star = ({ toggle = false } = {}) => async () => {
Front.showBanner(`${star} Repository ${repo} ${verb} ${statusMsg}!`) Front.showBanner(`${star} Repository ${repo} ${verb} ${statusMsg}!`)
} }
actions.gh.openRepo = () => { actions.gh.isRepo = (href) => {
const elements = [...document.querySelectorAll("a[href]")] const u = new URL(href)
.filter((a) => {
const u = new URL(a.href)
const [user, repo, ...rest] = u.pathname.split("/").filter((s) => s !== "") const [user, repo, ...rest] = u.pathname.split("/").filter((s) => s !== "")
return ( return (
u.origin === util.getCurrentLocation("origin") u.origin === util.getCurrentLocation("origin")
@ -204,14 +202,10 @@ actions.gh.openRepo = () => {
&& /^([a-zA-Z0-9]+-?)+$/.test(user) && /^([a-zA-Z0-9]+-?)+$/.test(user)
&& !ghReservedNames.check(user) && !ghReservedNames.check(user)
) )
})
Hints.create(elements, Hints.dispatchMouseClick)
} }
actions.gh.openUser = () => { actions.gh.isUser = (href) => {
const elements = [...document.querySelectorAll("a[href]")] const u = new URL(href)
.filter((a) => {
const u = new URL(a.href)
const [user, ...rest] = u.pathname.split("/").filter((s) => s !== "") const [user, ...rest] = u.pathname.split("/").filter((s) => s !== "")
return ( return (
u.origin === util.getCurrentLocation("origin") u.origin === util.getCurrentLocation("origin")
@ -221,14 +215,10 @@ actions.gh.openUser = () => {
&& /^([a-zA-Z0-9]+-?)+$/.test(user) && /^([a-zA-Z0-9]+-?)+$/.test(user)
&& !ghReservedNames.check(user) && !ghReservedNames.check(user)
) )
})
Hints.create(elements, Hints.dispatchMouseClick)
} }
actions.gh.openFile = () => { actions.gh.isFile = (href) => {
const elements = [...document.querySelectorAll("a[href]")] const u = new URL(href)
.filter((a) => {
const u = new URL(a.href)
const [user, repo, maybeBlob, ...rest] = u.pathname.split("/").filter((s) => s !== "") const [user, repo, maybeBlob, ...rest] = u.pathname.split("/").filter((s) => s !== "")
return ( return (
u.origin === util.getCurrentLocation("origin") u.origin === util.getCurrentLocation("origin")
@ -242,14 +232,10 @@ actions.gh.openFile = () => {
&& /^([a-zA-Z0-9]+-?)+$/.test(user) && /^([a-zA-Z0-9]+-?)+$/.test(user)
&& !ghReservedNames.check(user) && !ghReservedNames.check(user)
) )
})
Hints.create(elements, Hints.dispatchMouseClick)
} }
actions.gh.openIssue = () => { actions.gh.isIssue = (href) => {
const elements = [...document.querySelectorAll("a[href]")] const u = new URL(href)
.filter((a) => {
const u = new URL(a.href)
const [user, repo, maybeIssues] = u.pathname.split("/").filter((s) => s !== "") const [user, repo, maybeIssues] = u.pathname.split("/").filter((s) => s !== "")
return ( return (
u.origin === util.getCurrentLocation("origin") u.origin === util.getCurrentLocation("origin")
@ -261,14 +247,10 @@ actions.gh.openIssue = () => {
&& /^([a-zA-Z0-9]+-?)+$/.test(user) && /^([a-zA-Z0-9]+-?)+$/.test(user)
&& !ghReservedNames.check(user) && !ghReservedNames.check(user)
) )
})
Hints.create(elements, Hints.dispatchMouseClick)
} }
actions.gh.openPull = () => { actions.gh.isPull = (href) => {
const elements = [...document.querySelectorAll("a[href]")] const u = new URL(href)
.filter((a) => {
const u = new URL(a.href)
const [user, repo, maybePulls] = u.pathname.split("/").filter((s) => s !== "") const [user, repo, maybePulls] = u.pathname.split("/").filter((s) => s !== "")
return ( return (
u.origin === util.getCurrentLocation("origin") u.origin === util.getCurrentLocation("origin")
@ -280,10 +262,14 @@ actions.gh.openPull = () => {
&& /^([a-zA-Z0-9]+-?)+$/.test(user) && /^([a-zA-Z0-9]+-?)+$/.test(user)
&& !ghReservedNames.check(user) && !ghReservedNames.check(user)
) )
})
Hints.create(elements, Hints.dispatchMouseClick)
} }
actions.gh.openRepo = () => util.createHintsFiltered((a) => actions.gh.isRepo(a.href))
actions.gh.openUser = () => util.createHintsFiltered((a) => actions.gh.isUser(a.href))
actions.gh.openFile = () => util.createHintsFiltered((a) => actions.gh.isFile(a.href))
actions.gh.openIssue = () => util.createHintsFiltered((a) => actions.gh.isIssue(a.href))
actions.gh.openPull = () => util.createHintsFiltered((a) => actions.gh.isPull(a.href))
actions.gh.toggleLangStats = () => actions.gh.toggleLangStats = () =>
document.querySelector("summary[title='Click for language details']").click() document.querySelector("summary[title='Click for language details']").click()