Skip to content

Commit fc17338

Browse files
feat: history suggestions (#36)
1 parent f757e2f commit fc17338

File tree

6 files changed

+100
-11
lines changed

6 files changed

+100
-11
lines changed

extension/background.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { main } from './main.js';
22

33
main().then(()=>{
4-
console.log("Finished");
4+
console.log("Fly extension loaded");
55
});

extension/helpers.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,25 @@ async function requestJson(url: string): Promise<string> {
7373
});
7474
}
7575

76+
export async function setHistory(history: string[]){
77+
await setLocalStorage("history", JSON.stringify(history))
78+
}
79+
80+
export async function getHistory(query: string[]): Promise<string[]> {
81+
let history;
82+
83+
try {
84+
history = await getLocalStorage("history");
85+
history = [...JSON.parse(history)];
86+
history.push(query.join(" "));
87+
history = [...new Set(history)]
88+
} catch (e) {
89+
history = [query.join(" ")];
90+
}
91+
92+
return history;
93+
}
94+
7695
export async function repoManagement(query: string[]): Promise<string> {
7796
query.shift();
7897
const action = query[0];

extension/localstorage.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
export async function getLocalStorage(key: string): Promise<string>{
22
return new Promise((resolve, reject) => {
33
console.log("get", key);
4-
// @ts-ignore
54
chrome.storage.local.get([key], async function (items: { [x: string]: string; }) {
5+
if(chrome.runtime.lastError) {
6+
console.warn("Whoops.. " + chrome.runtime.lastError.message);
7+
}
68
if(key in items){
79
resolve(items[key]);
810
} else {
@@ -17,8 +19,10 @@ export async function setLocalStorage(key: string, value: string){
1719
console.log("set", key, value);
1820
let obj: any = {};
1921
obj[key] = value;
20-
// @ts-ignore
2122
chrome.storage.local.set(obj, function () {
23+
if(chrome.runtime.lastError) {
24+
console.warn("Whoops.. " + chrome.runtime.lastError.message);
25+
}
2226
resolve(undefined);
2327
});
2428
});
@@ -27,8 +31,10 @@ export async function setLocalStorage(key: string, value: string){
2731
export async function unsetLocalStorage(key: string){
2832
return new Promise((resolve, reject) => {
2933
console.log("unset", key);
30-
// @ts-ignore
3134
chrome.storage.local.remove(key, function () {
35+
if(chrome.runtime.lastError) {
36+
console.warn("Whoops.. " + chrome.runtime.lastError.message);
37+
}
3238
resolve(undefined);
3339
});
3440
});

extension/main.ts

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,20 @@ import {
1212
repoManagement,
1313
makeType,
1414
getValue,
15-
getType
15+
getType,
16+
getHistory,
17+
setHistory
1618
} from "./helpers.js";
1719

1820
import {getLocalStorage } from "./localstorage.js";
21+
import SuggestResult = chrome.omnibox.SuggestResult;
1922

2023
export async function main() {
21-
// @ts-ignore
2224
chrome.omnibox.onInputEntered.addListener(async (text, OnInputEnteredDisposition) => {
25+
if(chrome.runtime.lastError) {
26+
console.warn("Whoops.. " + chrome.runtime.lastError.message);
27+
}
28+
2329
const query = text.split(" ");
2430
if(query[0] === "set"){
2531
await setCalled(query);
@@ -29,9 +35,37 @@ export async function main() {
2935
let resp = await repoManagement(query);
3036
console.log(resp);
3137
} else {
32-
const flightPlans = await getFlightPlans();
33-
const flight = await getFlightFromQuery(query, flightPlans);
34-
await handleFlight(flight, flightPlans);
38+
const history = await getHistory(query);
39+
setHistory(history).then(async () => {
40+
const flightPlans = await getFlightPlans();
41+
const flight = await getFlightFromQuery(query, flightPlans);
42+
await handleFlight(flight, flightPlans);
43+
});
44+
}
45+
});
46+
47+
chrome.omnibox.onInputChanged.addListener(async (text, suggest) => {
48+
if(chrome.runtime.lastError) {
49+
console.warn("Whoops.. " + chrome.runtime.lastError.message);
50+
}
51+
const query = text.split(" ");
52+
if(!["set", "unset", "repo"].includes(query[0])){
53+
const history = await getHistory([]);
54+
const initialSuggestions = history.map((item: string) => {
55+
if (item !== "" && item.toLowerCase().includes(text.toLowerCase())) {
56+
return {
57+
content: item,
58+
description: item
59+
};
60+
}
61+
});
62+
const filtered = initialSuggestions.filter(function (x: any) {
63+
return x !== undefined;
64+
}) as SuggestResult[];
65+
66+
if(filtered.length > 0){
67+
suggest(filtered);
68+
}
3569
}
3670
});
3771
}

package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"author": "Apollorion",
1111
"license": "MIT",
1212
"devDependencies": {
13+
"@types/chrome": "^0.0.188",
1314
"@types/jest": "^27.4.0",
1415
"jest": "^27.5.1",
1516
"ts-jest": "^27.1.3",
@@ -18,8 +19,12 @@
1819
"dependencies": {},
1920
"jest": {
2021
"preset": "ts-jest",
21-
"modulePathIgnorePatterns": ["dist"],
22-
"extensionsToTreatAsEsm": [".ts"],
22+
"modulePathIgnorePatterns": [
23+
"dist"
24+
],
25+
"extensionsToTreatAsEsm": [
26+
".ts"
27+
],
2328
"globals": {
2429
"ts-jest": {
2530
"useESM": true

yarn.lock

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,13 +548,38 @@
548548
dependencies:
549549
"@babel/types" "^7.3.0"
550550

551+
"@types/chrome@^0.0.188":
552+
version "0.0.188"
553+
resolved "https://registry.yarnpkg.com/@types/chrome/-/chrome-0.0.188.tgz#ed73feca28a6d84f796ec50a2dbe7e41510175b3"
554+
integrity sha512-fVxbcnSrQqCaaTFfaP9QooRr0Lf47Ni+QVGpd4SQafe6x8RlLrlp+AAgc4QKNUVK7W6xEoOePMAu5sBXrc2qhA==
555+
dependencies:
556+
"@types/filesystem" "*"
557+
"@types/har-format" "*"
558+
559+
"@types/filesystem@*":
560+
version "0.0.32"
561+
resolved "https://registry.yarnpkg.com/@types/filesystem/-/filesystem-0.0.32.tgz#307df7cc084a2293c3c1a31151b178063e0a8edf"
562+
integrity sha512-Yuf4jR5YYMR2DVgwuCiP11s0xuVRyPKmz8vo6HBY3CGdeMj8af93CFZX+T82+VD1+UqHOxTq31lO7MI7lepBtQ==
563+
dependencies:
564+
"@types/filewriter" "*"
565+
566+
"@types/filewriter@*":
567+
version "0.0.29"
568+
resolved "https://registry.yarnpkg.com/@types/filewriter/-/filewriter-0.0.29.tgz#a48795ecadf957f6c0d10e0c34af86c098fa5bee"
569+
integrity sha512-BsPXH/irW0ht0Ji6iw/jJaK8Lj3FJemon2gvEqHKpCdDCeemHa+rI3WBGq5z7cDMZgoLjY40oninGxqk+8NzNQ==
570+
551571
"@types/graceful-fs@^4.1.2":
552572
version "4.1.5"
553573
resolved "https://registry.yarnpkg.com/@types/graceful-fs/-/graceful-fs-4.1.5.tgz#21ffba0d98da4350db64891f92a9e5db3cdb4e15"
554574
integrity sha512-anKkLmZZ+xm4p8JWBf4hElkM4XR+EZeA2M9BAkkTldmcyDY4mbdIJnRghDJH3Ov5ooY7/UAoENtmdMSkaAd7Cw==
555575
dependencies:
556576
"@types/node" "*"
557577

578+
"@types/har-format@*":
579+
version "1.2.8"
580+
resolved "https://registry.yarnpkg.com/@types/har-format/-/har-format-1.2.8.tgz#e6908b76d4c88be3db642846bb8b455f0bfb1c4e"
581+
integrity sha512-OP6L9VuZNdskgNN3zFQQ54ceYD8OLq5IbqO4VK91ORLfOm7WdT/CiT/pHEBSQEqCInJ2y3O6iCm/zGtPElpgJQ==
582+
558583
"@types/istanbul-lib-coverage@*", "@types/istanbul-lib-coverage@^2.0.0", "@types/istanbul-lib-coverage@^2.0.1":
559584
version "2.0.4"
560585
resolved "https://registry.yarnpkg.com/@types/istanbul-lib-coverage/-/istanbul-lib-coverage-2.0.4.tgz#8467d4b3c087805d63580480890791277ce35c44"

0 commit comments

Comments
 (0)