From 521f039953e06239f2642b8f6d1d9b1cc7411823 Mon Sep 17 00:00:00 2001 From: vec Date: Sun, 23 Jul 2023 16:07:43 +0800 Subject: [PATCH] [fix] #236 --- src/Background/modules/omnibox.ts | 23 +++++++++++++++++++---- src/utils/match-search-pattern.ts | 12 +----------- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/Background/modules/omnibox.ts b/src/Background/modules/omnibox.ts index 4be83a3..4a388b7 100644 --- a/src/Background/modules/omnibox.ts +++ b/src/Background/modules/omnibox.ts @@ -1,4 +1,5 @@ import { Atomic, Memo } from 'vait' +import { WindowID } from '../../core/layout/window' import { submitSearch } from '../../core/control-window' import { load as loadPreferences } from '../../preferences' import { toSearchURL } from '../../preferences/site-settings' @@ -17,6 +18,11 @@ function getURLSiteName(url_pattern: string): string { } } +const getCurrentTabByWindowId = (windowId: WindowID) => + chrome.tabs.query({ windowId }).then( + tabs => tabs.find(tab => tab.active) + ) + export default function OmniboxEvent() { // omnibox 提交 const [ applyOmniBoxInputEntered, cancelOmniBoxInputEntered ] = ChromeEvent( @@ -26,10 +32,19 @@ export default function OmniboxEvent() { if (ind_search && (ind_search.id === content)) { const { url_pattern, search_text } = ind_search - chrome.tabs.create({ - url: toSearchURL(url_pattern, search_text), - windowId: chrome.windows.WINDOW_ID_CURRENT - }) + + console.warn('search_text', `[${search_text}]`); + + + // 不知道为什么在这种情况下调用 chrome.tabs.getCurrent + // 会得到 undefined,于是只能使用 getCurrentTabByWindowId 这样一个迂回的办法 + getCurrentTabByWindowId(chrome.windows.WINDOW_ID_CURRENT).then( + current_tab => chrome.tabs.create({ + index: current_tab ? current_tab.index + 1 : undefined, + url: toSearchURL(url_pattern, search_text), + windowId: chrome.windows.WINDOW_ID_CURRENT + }) + ) } else { chrome.windows.getCurrent( ({ id }) => submitSearch(content, id) diff --git a/src/utils/match-search-pattern.ts b/src/utils/match-search-pattern.ts index 299a028..457d884 100644 --- a/src/utils/match-search-pattern.ts +++ b/src/utils/match-search-pattern.ts @@ -4,16 +4,6 @@ type MatchResult = Readonly<[true, string, string] | [false]> const blankCharExp = / | /g -function trimOne(str: string) { - const [ first, ...remain ] = str - - if (blankCharExp.test(first)) { - return remain.join('') - } else { - return str - } -} - export default function matchSearchPattern( input_str: string ): MatchResult { @@ -27,7 +17,7 @@ export default function matchSearchPattern( slice(0, index), pipe( slice(index, input_str.length), - trimOne + (s: string) => s.trimStart() ) ]