Skip to content

Commit

Permalink
[fix] #236
Browse files Browse the repository at this point in the history
  • Loading branch information
VecHK committed Jul 23, 2023
1 parent a6e1539 commit 521f039
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
23 changes: 19 additions & 4 deletions src/Background/modules/omnibox.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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(
Expand All @@ -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)
Expand Down
12 changes: 1 addition & 11 deletions src/utils/match-search-pattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -27,7 +17,7 @@ export default function matchSearchPattern(
slice(0, index),
pipe(
slice(index, input_str.length),
trimOne
(s: string) => s.trimStart()
)
]

Expand Down

0 comments on commit 521f039

Please sign in to comment.