Add beforeNavigate hook for Omnibox

This commit is contained in:
Folyd 2020-04-17 00:15:07 +08:00
parent 48fe6e45fa
commit 9a7a541c83
2 changed files with 9 additions and 1 deletions

View file

@ -29,6 +29,13 @@ omnibox.bootstrap({
description: `Search Rust docs ${c.match(query)} on ${settings.isOfflineMode ? "offline mode" : stdSearcher.rootPath}`,
}];
},
beforeNavigate: (content) => {
if (content && content.trim().startsWith("@")) {
return `https://docs.rs/${content.replace("@", "")}`;
} else {
return content;
}
},
onSelected: (query, result) => {
HistoryCommand.record(query, result);
}

View file

@ -43,7 +43,7 @@ Omnibox.prototype.parse = function(input) {
return {query: query.join(" "), page};
};
Omnibox.prototype.bootstrap = function({onSearch, onFormat, onAppend, onSelected}) {
Omnibox.prototype.bootstrap = function({onSearch, onFormat, onAppend, beforeNavigate, onSelected}) {
this.globalEvent = new QueryEvent({onSearch, onFormat, onAppend});
this.setDefaultSuggestion(this.defaultSuggestionDescription);
let results;
@ -80,6 +80,7 @@ Omnibox.prototype.bootstrap = function({onSearch, onFormat, onAppend, onSelected
chrome.omnibox.onInputEntered.addListener((content, disposition) => {
let result;
content = beforeNavigate(content);
if (/^(https?|file):\/\//i.test(content)) {
this.navigateToUrl(content, disposition);
result = results.find(item => item.content === content);