Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions src/background/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,14 @@ browser.runtime.onInstalled.addListener(({ reason }) => {
}
});

browser.commands.onCommand.addListener((command, tab) => {
console.log(tab);
// should I still check the command?
browser.commands.onCommand.addListener((_command, _tab) => {
getCurrentTab().then((currentTab) => {
console.log(currentTab)
if (
currentTab?.id &&
currentTab.url &&
// chrome does not like content scripts acting on thier urls
!isBrowserURL(currentTab.url)
) {
console.log(tab?.id === currentTab.id);
const messagePayload: MessagePlayload = {
message: Message.TOGGLE_TAB_BUTLER_MODAL,
};
Expand Down Expand Up @@ -145,7 +141,6 @@ browser.runtime.onMessage.addListener(
break;

case Message.WEB_SEARCH: {
console.log("here", messagePayload);
const { query } = messagePayload as ActionPayload;
if (query) {
browser.search.query({ text: query, disposition: "NEW_TAB" });
Expand Down
2 changes: 1 addition & 1 deletion src/background/search/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export const actions: ActionData[] = [
{
name: "Open Twitter",
id: nanoid(),
message: Message.OPEN_FACEBOOK,
message: Message.OPEN_TWITTER,
type: DataType.ACTION,
},
];
40 changes: 11 additions & 29 deletions src/background/search/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,40 +122,22 @@ function scoreData<T extends Data>(
keys: keyof T | Array<keyof T>,
boostPercentage?: number
) {
const normalizedKeys = Array.isArray(keys) ? keys : [keys];
const dataLength = data.length;
let results: ScoredDataType[] = [];

if (!Array.isArray(keys)) {
for (let i = 0; i < dataLength; i++) {
const item = data[i];
let score = matchScore(query, item[keys] as string);
if (score < DEFAULT_SCORE_THRESHOLD) continue;
if (boostPercentage !== undefined) {
// think about this
score += score * boostPercentage; // get the percentage and add it to the score
}
results.push({ score, data: item });
for (let i = 0; i < dataLength; i++) {
const item = data[i];
let maxScore = 0;
for (const key of normalizedKeys) {
const score = matchScore(query, item[key] as string);
if (score > maxScore) maxScore = score;
}
} else {
for (let i = 0; i < dataLength; i++) {
const item = data[i];
const keyLength = keys.length;
let maxScore = 0;
for (let j = 0; j < keyLength; j++) {
const key = keys[j];
const score = matchScore(query, item[key] as string);
if (score < DEFAULT_SCORE_THRESHOLD) continue;
if (score > maxScore) {
maxScore = score;
}
}
if (maxScore === 0 || maxScore < DEFAULT_SCORE_THRESHOLD) continue;
if (boostPercentage !== undefined) {
// think about this
maxScore += maxScore * boostPercentage; // get the percentage and add it to the score
}
results.push({ score: maxScore, data: item });
if (maxScore < DEFAULT_SCORE_THRESHOLD) continue;
if (boostPercentage !== undefined) {
maxScore += maxScore * boostPercentage;
}
results.push({ score: maxScore, data: item });
}

if (results.length === 0) {
Expand Down
13 changes: 7 additions & 6 deletions src/background/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ const cleanTabUrl = (url: string) => {
// https://bobbyhadz.com/blog/javascript-remove-querystring-from-url
// this method removes all the query params but leaves the hash
// the hash is kept as in some cases, it can help users "match" with what they are looking for (eg: a section title in a website they are on)
if (url.includes("?")) {
return url.slice(0, url.indexOf("?")) + url.slice(url.indexOf("#"));
} else return url;
const queryIndex = url.indexOf("?");
if (queryIndex === -1) return url;
const hashIndex = url.indexOf("#");
return url.slice(0, queryIndex) + (hashIndex !== -1 ? url.slice(hashIndex) : "");
};

export async function fetchAllTabs() {
Expand Down Expand Up @@ -95,14 +96,14 @@ function normalizeBookmarks(bookmarks: browser.Bookmarks.BookmarkTreeNode[]) {
const bookmark = bookmarks[i];
if (bookmark.children) {
results.push(...normalizeBookmarks(bookmark.children));
} else {
} else if (bookmark.url) {
results.push({
type: DataType.BOOKMARK,
id: nanoid(),
title: bookmark.title,
url: bookmark.url!, // should be present if it since it is not a folder
url: bookmark.url,
});
}
}
}
return results;
}
Expand Down
3 changes: 2 additions & 1 deletion src/common/common.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
export const isFirefox = () => navigator.userAgent.includes("Firefox");
export const isChrome = () => navigator.userAgent.includes("Chrome");

export function isBrowserURL(url: string) {
export function isBrowserURL(url: string): boolean {
if (url === "about:blank") return true;
if (isChrome()) return isChromeURL(url);
if (isFirefox()) return isFirefoxURL(url);
return false;
}

export function isChromeURL(url: string) {
Expand Down
57 changes: 27 additions & 30 deletions src/content/ui/components/SearchModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const SearchModal = (props: Props) => {
);
return foundId === -1 ? null : foundId;
} else return null;
}, [selectedId]);
}, [selectedId, resultList]);

const inputRef = useRef<ElementRef<"input">>(null);

Expand Down Expand Up @@ -187,36 +187,33 @@ export const SearchModal = (props: Props) => {
}
};

const onInputChange = debounce((query: string) => {
if (query) {
search(query)
.then((result) => {
// if the the input is currently empty, dont try and and set the result/render an error
if (!inputRef.current?.value) return;
// console.log(result);
if (result.hasError) {
setError(true);
} else if (result.data !== null) {
// console.log(result.data);
const { sections, sortedResult } = result.data;
setResultSections(sections);
setResultList(sortedResult);
// select the first item
if (sortedResult.length === 0) {
setSelectedId(null);
} else {
setSelectedId(sortedResult[0].data.id);
const onInputChange = useRef(
debounce((query: string) => {
if (query) {
search(query)
.then((result) => {
// if the the input is currently empty, dont try and and set the result/render an error
if (!inputRef.current?.value) return;
if (result.hasError) {
setError(true);
} else if (result.data !== null) {
const { sections, sortedResult } = result.data;
setResultSections(sections);
setResultList(sortedResult);
// select the first item
if (sortedResult.length === 0) {
setSelectedId(null);
} else {
setSelectedId(sortedResult[0].data.id);
}
}
// result.data && setResultSections(result.data);
}
})
.catch(() => {
// console.log("here is the err", err);
// console.log("here in the catch");
setError(true);
});
}
}, 300);
})
.catch(() => {
setError(true);
});
}
}, 300),
).current;

const closeOnClick = <T,>(func: (data: T) => void) => {
return (data: T) => {
Expand Down