A fast, local-first "redirection engine" for !bang users with a few extra features ^-^
5
fork

Configure Feed

Select the types of activity you want to include in your feed.

at main 60 lines 1.3 kB view raw
1import { CONSTANTS } from "./main"; 2 3const createAudio = (src: string) => { 4 const audio = new Audio(); 5 audio.src = src; 6 return audio; 7}; 8 9const storage = { 10 get: (key: string) => localStorage.getItem(key), 11 set: (key: string, value: string) => localStorage.setItem(key, value), 12 remove: (key: string) => localStorage.removeItem(key), 13}; 14 15function addToSearchHistory( 16 query: string, 17 bang: { bang: string; name: string; url: string }, 18) { 19 const history: Array<{ query: string; bang: string; name: string; timestamp: number }> = 20 JSON.parse(storage.get(CONSTANTS.LOCAL_STORAGE_KEYS.SEARCH_HISTORY) || "[]"); 21 22 history.unshift({ 23 query, 24 bang: bang.bang, 25 name: bang.name, 26 timestamp: Date.now(), 27 }); 28 history.splice(CONSTANTS.MAX_HISTORY); 29 storage.set( 30 CONSTANTS.LOCAL_STORAGE_KEYS.SEARCH_HISTORY, 31 JSON.stringify(history), 32 ); 33} 34 35function getSearchHistory(): Array<{ 36 query: string; 37 bang: string; 38 name: string; 39 timestamp: number; 40}> { 41 try { 42 return JSON.parse( 43 storage.get(CONSTANTS.LOCAL_STORAGE_KEYS.SEARCH_HISTORY) || "[]", 44 ); 45 } catch { 46 return []; 47 } 48} 49 50function clearSearchHistory() { 51 storage.set(CONSTANTS.LOCAL_STORAGE_KEYS.SEARCH_HISTORY, "[]"); 52} 53 54export { 55 createAudio, 56 storage, 57 addToSearchHistory, 58 getSearchHistory, 59 clearSearchHistory, 60};