diff --git a/app/client.imba b/app/client.imba index 17933d9..9a676ee 100644 --- a/app/client.imba +++ b/app/client.imba @@ -6,6 +6,7 @@ import db from './db' import fzi from 'fzi' import download from 'downloadjs' import { nanoid } from 'nanoid' +import { parse_url } from './utils' let state = { query: '' @@ -48,13 +49,14 @@ tag app await load_config! 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: "youtube", url: "youtube.com" } add_link { name: "photopea", url: "photopea.com" } add_link { name: "twitch", url: "twitch.tv" } add_link { name: "messenger", url: "messenger.com" } add_link { name: "instagram", url: "instagram.com" } + add_link { name: "localhost 3000", url: "http://localhost:3000" } def validate_config throw 'config error' unless config..search_engine.hasOwnProperty 'url' @@ -63,7 +65,7 @@ tag app def reset_config p "resetting config" - let url = 'www.google.com/search?q=' + let url = 'https://www.google.com/search?q=' let frequency = 0 let icon = await fetch_image_as_base_64 'google.com' config.search_engine = { url, icon, frequency } @@ -123,14 +125,14 @@ tag app def use_search_engine 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==' return new Promise! do |resolve| let res try - res = await global.fetch("https://icon.horse/icon/{url}") + res = await global.fetch("https://icon.horse/icon/{host}") catch p "Failed to get icon from icon horse." resolve fallback @@ -154,13 +156,14 @@ tag app let name = split_text.join(" ") { name, url } - def strip_url url - url.trim!.replace(/(^\w+:|^)\/\//, '') - def add_link { url, name, frequency=0 } name = name.trim! - url = strip_url url - let img = await fetch_image_as_base_64(url) + let host + 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 link = { id, name, url, frequency, img } try @@ -172,7 +175,7 @@ tag app def handle_click_link link await increment_link_frequency link - window.location.href = "//{link.url}" + window.location.href = link.url def handle_click_search increment_search_engine_frequency! @@ -183,7 +186,7 @@ tag app else let link = state.scored_links[selection_index] await increment_link_frequency link - window.location.href = "//{link.url}" + window.location.href = link.url def handle_click_add loading = yes @@ -240,20 +243,19 @@ tag app settings_active = 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 loading = yes - let input = window.prompt("Please enter the URL of your search engine.") - return unless input - let url = input.trim!.replace(/(^\w+:|^)\/\//, '') - unless url - err "changing search engine", "Invalid URL." - return - await set_search_engine url + let set_search_engine = do + let input = window.prompt "Please enter the URL of your search engine." + return if input === null + try + var { href:url, host } = parse_url input + catch e + 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 loading = no @@ -414,7 +416,7 @@ tag app if state.scored_links.length > 0 for link, index in state.scored_links link.frequency else <.link-left> diff --git a/app/db.imba b/app/db.imba index d6a6a39..ff4217f 100644 --- a/app/db.imba +++ b/app/db.imba @@ -1,3 +1,4 @@ +let p = console.log import Dexie from 'dexie' import 'dexie-export-import' import { nanoid } from 'nanoid' @@ -11,6 +12,7 @@ db.version(1).stores({ db.version(2).stores({ links: "++id,name,url,frequency,img" }).upgrade! do |trans| + p "upgrading to fuzzyhome db version 2" trans.links.toCollection!.modify! do |link| let id = nanoid! let name = link.name @@ -19,4 +21,13 @@ db.version(2).stores({ let img = link.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 diff --git a/app/utils.imba b/app/utils.imba new file mode 100644 index 0000000..e0e3174 --- /dev/null +++ b/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" diff --git a/package.json b/package.json index e188b0d..b388108 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "fuzzyhome", - "version": "0.1.32", + "version": "0.1.33", "scripts": { "start": "imba run -SMH --baseurl . -w server.imba", "build-app": "rm -rf dist && imba build -SMH --baseurl . server.imba", diff --git a/readme.md b/readme.md index 5f76dcb..b4543a9 100644 --- a/readme.md +++ b/readme.md @@ -22,6 +22,7 @@ For example: imba home page https://imba.io/ ``` 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 Search for a link by typing.