2018-11-03 00:21:44 +00:00
|
|
|
const { categories } = require("./help")
|
|
|
|
|
|
|
|
const util = {}
|
|
|
|
|
2018-11-10 01:47:39 +00:00
|
|
|
util.getCurrentLocation = (prop = "href") => {
|
|
|
|
if (typeof window === "undefined") {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return window.location[prop]
|
|
|
|
}
|
|
|
|
|
2019-09-11 11:51:27 +00:00
|
|
|
util.escape = (str) => String(str).replace(/[&<>"'`=/]/g, (s) => ({
|
2018-11-03 00:21:44 +00:00
|
|
|
"&": "&",
|
|
|
|
"<": "<",
|
|
|
|
">": ">",
|
|
|
|
"\"": """,
|
|
|
|
"'": "'",
|
|
|
|
"/": "/",
|
|
|
|
"`": "`",
|
|
|
|
"=": "=",
|
|
|
|
}[s]))
|
|
|
|
|
|
|
|
util.createSuggestionItem = (html, props = {}) => {
|
|
|
|
const li = document.createElement("li")
|
|
|
|
li.innerHTML = html
|
|
|
|
return { html: li.outerHTML, props }
|
|
|
|
}
|
|
|
|
|
|
|
|
util.createURLItem = (title, url, sanitize = true) => {
|
|
|
|
let t = title
|
|
|
|
let u = url
|
|
|
|
if (sanitize) {
|
|
|
|
t = util.escape(t)
|
|
|
|
u = new URL(u).toString()
|
|
|
|
}
|
|
|
|
return util.createSuggestionItem(`
|
|
|
|
<div class="title">${t}</div>
|
|
|
|
<div class="url">${u}</div>
|
|
|
|
`, { url: u })
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine if the given rect is visible in the viewport
|
2019-09-11 11:51:27 +00:00
|
|
|
util.isRectVisibleInViewport = (rect) =>
|
2019-03-07 22:59:15 +00:00
|
|
|
rect.height > 0
|
|
|
|
&& rect.width > 0
|
|
|
|
&& rect.bottom >= 0
|
|
|
|
&& rect.right >= 0
|
|
|
|
&& rect.top <= (window.innerHeight || document.documentElement.clientHeight)
|
|
|
|
&& rect.left <= (window.innerWidth || document.documentElement.clientWidth)
|
2018-08-28 20:11:37 +00:00
|
|
|
|
2018-11-03 00:21:44 +00:00
|
|
|
// Determine if the given element is visible in the viewport
|
2019-09-11 11:51:27 +00:00
|
|
|
util.isElementInViewport = (e) =>
|
2019-05-30 06:49:07 +00:00
|
|
|
e.offsetHeight > 0 && e.offsetWidth > 0
|
|
|
|
&& !e.getAttribute("disabled")
|
|
|
|
&& util.isRectVisibleInViewport(e.getBoundingClientRect())
|
2018-11-03 00:21:44 +00:00
|
|
|
|
|
|
|
// Process Unmaps
|
2019-03-07 22:59:15 +00:00
|
|
|
util.rmMaps = (a) => {
|
2018-11-10 01:47:39 +00:00
|
|
|
if (typeof unmap === "undefined") {
|
|
|
|
return
|
|
|
|
}
|
2019-09-11 11:51:27 +00:00
|
|
|
a.forEach((u) => unmap(u))
|
2018-11-10 01:47:39 +00:00
|
|
|
}
|
2018-11-03 00:21:44 +00:00
|
|
|
|
2019-09-11 11:51:27 +00:00
|
|
|
util.rmSearchAliases = (a) => Object.entries(a).forEach(([leader, items]) => {
|
2019-03-07 22:59:15 +00:00
|
|
|
if (typeof removeSearchAliasX === "undefined") {
|
|
|
|
return
|
|
|
|
}
|
2019-09-11 11:51:27 +00:00
|
|
|
items.forEach((v) => removeSearchAliasX(v, leader))
|
2018-11-10 01:47:39 +00:00
|
|
|
})
|
2018-11-03 00:21:44 +00:00
|
|
|
|
|
|
|
// Process Mappings
|
2019-09-10 23:44:28 +00:00
|
|
|
util.processMaps = (maps, aliases, siteleader) => {
|
2018-11-10 01:47:39 +00:00
|
|
|
if (typeof map === "undefined" || typeof mapkey === "undefined") {
|
|
|
|
return
|
|
|
|
}
|
2019-09-10 23:44:28 +00:00
|
|
|
|
|
|
|
const hydratedAliases = Object.entries(aliases)
|
|
|
|
.flatMap(([baseDomain, aliasDomains]) =>
|
2019-09-11 11:51:27 +00:00
|
|
|
aliasDomains.flatMap((a) => ({ [a]: maps[baseDomain] })))
|
2019-09-10 23:44:28 +00:00
|
|
|
|
|
|
|
const mapsAndAliases = Object.assign({}, maps, ...hydratedAliases)
|
|
|
|
|
|
|
|
Object.entries(mapsAndAliases).forEach(([domain, domainMaps]) => domainMaps.forEach(((mapObj) => {
|
2019-03-07 22:59:15 +00:00
|
|
|
const {
|
|
|
|
alias,
|
|
|
|
callback,
|
|
|
|
leader = (domain === "global") ? "" : siteleader,
|
|
|
|
category = categories.misc,
|
|
|
|
description = "",
|
|
|
|
} = mapObj
|
|
|
|
const opts = {}
|
|
|
|
|
|
|
|
const key = `${leader}${alias}`
|
|
|
|
|
|
|
|
// Determine if it's a site-specific mapping
|
|
|
|
if (domain !== "global") {
|
|
|
|
const d = domain.replace(".", "\\.")
|
|
|
|
opts.domain = new RegExp(`^http(s)?://(([a-zA-Z0-9-_]+\\.)*)(${d})(/.*)?`)
|
|
|
|
}
|
|
|
|
|
|
|
|
const fullDescription = `#${category} ${description}`
|
|
|
|
|
|
|
|
if (mapObj.map !== undefined) {
|
|
|
|
map(alias, mapObj.map)
|
|
|
|
} else {
|
|
|
|
mapkey(key, fullDescription, callback, opts)
|
|
|
|
}
|
|
|
|
})))
|
2018-11-10 01:47:39 +00:00
|
|
|
}
|
2018-11-03 00:21:44 +00:00
|
|
|
|
|
|
|
// process completions
|
|
|
|
util.processCompletions = (completions, searchleader) => Object.values(completions).forEach((s) => {
|
2018-11-10 01:47:39 +00:00
|
|
|
if (typeof Front === "undefined" || typeof addSearchAliasX === "undefined" || typeof mapkey === "undefined") {
|
|
|
|
return
|
|
|
|
}
|
2018-11-03 00:21:44 +00:00
|
|
|
addSearchAliasX(s.alias, s.name, s.search, searchleader, s.compl, s.callback)
|
|
|
|
mapkey(`${searchleader}${s.alias}`, `#8Search ${s.name}`, () => Front.openOmnibar({ type: "SearchEngine", extra: s.alias }))
|
2019-03-07 22:59:15 +00:00
|
|
|
mapkey(`c${searchleader}${s.alias}`, `#8Search ${s.name} with clipboard contents`, () => {
|
|
|
|
Clipboard.read((c) => {
|
|
|
|
Front.openOmnibar({ type: "SearchEngine", pref: c.data, extra: s.alias })
|
|
|
|
})
|
|
|
|
})
|
2018-11-03 00:21:44 +00:00
|
|
|
})
|
2018-08-28 20:11:37 +00:00
|
|
|
|
2018-11-10 01:47:39 +00:00
|
|
|
util.addSettings = (s) => {
|
|
|
|
if (typeof settings === "undefined") {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
Object.assign(settings, s)
|
|
|
|
}
|
|
|
|
|
2018-11-03 00:21:44 +00:00
|
|
|
module.exports = util
|