Browse Source

parse urls to allow for localhost links and icon grabbing from hostname

main
familyfriendlymikey 2 years ago
parent
commit
ac8b26f856
  1. 54
      app/client.imba
  2. 11
      app/db.imba
  3. 11
      app/utils.imba
  4. 2
      package.json
  5. 1
      readme.md

54
app/client.imba

@ -6,6 +6,7 @@ import db from './db'
import fzi from 'fzi' import fzi from 'fzi'
import download from 'downloadjs' import download from 'downloadjs'
import { nanoid } from 'nanoid' import { nanoid } from 'nanoid'
import { parse_url } from './utils'
let state = { let state = {
query: '' query: ''
@ -48,13 +49,14 @@ tag app
await load_config! await load_config!
def add_initial_links def add_initial_links
add_link { name: "github", url: "https://github.com/familyfriendlymikey" } add_link { name: "click here for help", url: "github.com/familyfriendlymikey/fuzzyhome", frequency: 1 }
add_link { name: "google", url: "google.com" } add_link { name: "google", url: "google.com" }
add_link { name: "youtube", url: "youtube.com" } add_link { name: "youtube", url: "youtube.com" }
add_link { name: "photopea", url: "photopea.com" } add_link { name: "photopea", url: "photopea.com" }
add_link { name: "twitch", url: "twitch.tv" } add_link { name: "twitch", url: "twitch.tv" }
add_link { name: "messenger", url: "messenger.com" } add_link { name: "messenger", url: "messenger.com" }
add_link { name: "instagram", url: "instagram.com" } add_link { name: "instagram", url: "instagram.com" }
add_link { name: "localhost 3000", url: "http://localhost:3000" }
def validate_config def validate_config
throw 'config error' unless config..search_engine.hasOwnProperty 'url' throw 'config error' unless config..search_engine.hasOwnProperty 'url'
@ -63,7 +65,7 @@ tag app
def reset_config def reset_config
p "resetting config" p "resetting config"
let url = 'www.google.com/search?q=' let url = 'https://www.google.com/search?q='
let frequency = 0 let frequency = 0
let icon = await fetch_image_as_base_64 'google.com' let icon = await fetch_image_as_base_64 'google.com'
config.search_engine = { url, icon, frequency } config.search_engine = { url, icon, frequency }
@ -123,14 +125,14 @@ tag app
def use_search_engine def use_search_engine
increment_search_engine_frequency! increment_search_engine_frequency!
window.location.href = "//{encoded_search_query}" window.location.href = encoded_search_query
def fetch_image_as_base_64 url def fetch_image_as_base_64 host
let fallback = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAAH0lEQVR42mO8seXffwYqAsZRA0cNHDVw1MBRA0eqgQCDRkbJSQHxEQAAAABJRU5ErkJggg==' let fallback = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAAH0lEQVR42mO8seXffwYqAsZRA0cNHDVw1MBRA0eqgQCDRkbJSQHxEQAAAABJRU5ErkJggg=='
return new Promise! do |resolve| return new Promise! do |resolve|
let res let res
try try
res = await global.fetch("https://icon.horse/icon/{url}") res = await global.fetch("https://icon.horse/icon/{host}")
catch catch
p "Failed to get icon from icon horse." p "Failed to get icon from icon horse."
resolve fallback resolve fallback
@ -154,13 +156,14 @@ tag app
let name = split_text.join(" ") let name = split_text.join(" ")
{ name, url } { name, url }
def strip_url url
url.trim!.replace(/(^\w+:|^)\/\//, '')
def add_link { url, name, frequency=0 } def add_link { url, name, frequency=0 }
name = name.trim! name = name.trim!
url = strip_url url let host
let img = await fetch_image_as_base_64(url) try
{ href:url, host } = parse_url url
catch e
return err "parsing url", e
let img = await fetch_image_as_base_64 host
let id = nanoid! let id = nanoid!
let link = { id, name, url, frequency, img } let link = { id, name, url, frequency, img }
try try
@ -172,7 +175,7 @@ tag app
def handle_click_link link def handle_click_link link
await increment_link_frequency link await increment_link_frequency link
window.location.href = "//{link.url}" window.location.href = link.url
def handle_click_search def handle_click_search
increment_search_engine_frequency! increment_search_engine_frequency!
@ -183,7 +186,7 @@ tag app
else else
let link = state.scored_links[selection_index] let link = state.scored_links[selection_index]
await increment_link_frequency link await increment_link_frequency link
window.location.href = "//{link.url}" window.location.href = link.url
def handle_click_add def handle_click_add
loading = yes loading = yes
@ -240,20 +243,19 @@ tag app
settings_active = no settings_active = no
loading = no loading = no
def set_search_engine url
let icon = await fetch_image_as_base_64 url
config.search_engine = { url, icon }
save_config!
def handle_click_config def handle_click_config
loading = yes loading = yes
let input = window.prompt("Please enter the URL of your search engine.") let set_search_engine = do
return unless input let input = window.prompt "Please enter the URL of your search engine."
let url = input.trim!.replace(/(^\w+:|^)\/\//, '') return if input === null
unless url try
err "changing search engine", "Invalid URL." var { href:url, host } = parse_url input
return catch e
await set_search_engine url return err "changing search engine", e
let icon = await fetch_image_as_base_64 host
config.search_engine = { url, icon }
save_config!
await set_search_engine!
settings_active = no settings_active = no
loading = no loading = no
@ -414,7 +416,7 @@ tag app
if state.scored_links.length > 0 if state.scored_links.length > 0
for link, index in state.scored_links for link, index in state.scored_links
<a.link <a.link
href="//{link.url}" href=link.url
@pointerover=(selection_index = index) @pointerover=(selection_index = index)
@click.prevent=handle_click_link(link) @click.prevent=handle_click_link(link)
.selected=(index == selection_index) .selected=(index == selection_index)
@ -427,7 +429,7 @@ tag app
<.frequency> link.frequency <.frequency> link.frequency
else else
<a.link.selected <a.link.selected
href="//{encoded_search_query}" href=encoded_search_query
@click=handle_click_search @click=handle_click_search
> >
<.link-left> <.link-left>

11
app/db.imba

@ -1,3 +1,4 @@
let p = console.log
import Dexie from 'dexie' import Dexie from 'dexie'
import 'dexie-export-import' import 'dexie-export-import'
import { nanoid } from 'nanoid' import { nanoid } from 'nanoid'
@ -11,6 +12,7 @@ db.version(1).stores({
db.version(2).stores({ db.version(2).stores({
links: "++id,name,url,frequency,img" links: "++id,name,url,frequency,img"
}).upgrade! do |trans| }).upgrade! do |trans|
p "upgrading to fuzzyhome db version 2"
trans.links.toCollection!.modify! do |link| trans.links.toCollection!.modify! do |link|
let id = nanoid! let id = nanoid!
let name = link.name let name = link.name
@ -19,4 +21,13 @@ db.version(2).stores({
let img = link.img let img = link.img
this.value = { id, name, url, frequency, img } this.value = { id, name, url, frequency, img }
import { parse_url } from './utils'
db.version(3).stores({
links: "++id,name,url,frequency,img"
}).upgrade! do |trans|
p "upgrading to fuzzyhome db version 3"
trans.links.toCollection!.modify! do |link|
try
link.url = parse_url(link.url).href
export default db export default db

11
app/utils.imba

@ -0,0 +1,11 @@
export def parse_url url
throw "invalid url" if url === null
let get_url = do |s|
let url = new URL s
throw _ unless (url.host and url.href)
url
try
return get_url url
try
return get_url "https://{url}"
throw "invalid url"

2
package.json

@ -1,6 +1,6 @@
{ {
"name": "fuzzyhome", "name": "fuzzyhome",
"version": "0.1.32", "version": "0.1.33",
"scripts": { "scripts": {
"start": "imba run -SMH --baseurl . -w server.imba", "start": "imba run -SMH --baseurl . -w server.imba",
"build-app": "rm -rf dist && imba build -SMH --baseurl . server.imba", "build-app": "rm -rf dist && imba build -SMH --baseurl . server.imba",

1
readme.md

@ -22,6 +22,7 @@ For example:
imba home page https://imba.io/ imba home page https://imba.io/
``` ```
The last space-separated string will be used as the url for your link. The last space-separated string will be used as the url for your link.
In most cases if you do not specify a protocol, `https` will be used.
### Fuzzy Find ### Fuzzy Find
Search for a link by typing. Search for a link by typing.

Loading…
Cancel
Save