From 19bf9ae56d36b2444a787a7c15c1e07b5c3cba29 Mon Sep 17 00:00:00 2001 From: Gordon Martin Date: Tue, 19 Jun 2018 22:59:28 +0100 Subject: [PATCH] [WIP]: start adding support for other search engines. --- app/builtin-pages/views/settings.js | 37 +++++++++++++++++++++++++++++ app/builtin-pages/views/start.js | 15 ++++++++---- app/lib/search-engines.js | 33 +++++++++++++++++++++++++ app/shell-window/ui/navbar.js | 6 ++++- 4 files changed, 85 insertions(+), 6 deletions(-) create mode 100644 app/lib/search-engines.js diff --git a/app/builtin-pages/views/settings.js b/app/builtin-pages/views/settings.js index 53507f5042..549c095519 100644 --- a/app/builtin-pages/views/settings.js +++ b/app/builtin-pages/views/settings.js @@ -5,6 +5,8 @@ import * as toast from '../com/toast' import DatNetworkActivity from '../com/dat-network-activity' import renderBuiltinPagesNav from '../com/builtin-pages-nav' +import {getAvailableSearchEngines} from '../../lib/search-engines' + // globals // = @@ -108,6 +110,7 @@ function renderGeneral () { ${renderAutoUpdater()} ${renderDefaultSyncPathSettings()} ${renderProtocolSettings()} + ${renderSearchEngineSettings()} ${renderOnStartupSettings()} ${renderDefaultDatIgnoreSettings()} ${renderAnalyticsSettings()} @@ -278,6 +281,40 @@ function renderProtocolSettings () { ` } +function renderSearchEngineSettings() { + + function changeSearchEngine(engineName) { + beaker.browser.setSetting('search_engine', engineName) + } + + // todo: default without hardcoding? + var currentSearchEngine = settings.search_engine || "DuckDuckGo"; + + var engines = getAvailableSearchEngines(); + + // todo: consider getting rid of that disgusting ternary :P + // also, style the select box? + return yo` +
+

Default Search Engine Settings

+ +

+ Use the following search engine to search +

+ + + + +
` +} + function renderAutoUpdater () { if (!browserInfo.updater.isBrowserUpdatesSupported) { return yo` diff --git a/app/builtin-pages/views/start.js b/app/builtin-pages/views/start.js index 61d59df488..b5b3d6054b 100644 --- a/app/builtin-pages/views/start.js +++ b/app/builtin-pages/views/start.js @@ -11,6 +11,8 @@ import * as toast from '../com/toast' import * as createArchivePopup from '../com/create-archive-popup' import {findParent, writeToClipboard} from '../../lib/fg/event-handlers' +import {getSearchEngineOrDefault, getDefaultSearchEngine} from '../../lib/search-engines' + const LATEST_VERSION = 7011 // semver where major*1mm and minor*1k; thus 3.2.1 = 3002001 const RELEASE_NOTES_URL = 'https://beakerbrowser.com/releases/0-7-10/?updated=true' @@ -142,15 +144,18 @@ async function onUpdateSearchQuery (q) { }) searchResults = searchResults.concat(historyResults) - // add a DuckDuckGo search to the results - const ddgRes = { + var configuredSearchEngine = settings.search_engine; + var engine = getSearchEngineOrDefault(configuredSearchEngine); + + // add a search result search to the results + const searchResult = { title: query, - targetUrl: `https://duckduckgo.com?q=${encodeURIComponent(query)}`, + targetUrl: engine.makeQueryUrl(query), icon: 'fa fa-search', - label: 'Search DuckDuckGo', + label: 'Search ' + engine.name, class: 'ddg' } - searchResults.push(ddgRes) + searchResults.push(searchResult) } update() diff --git a/app/lib/search-engines.js b/app/lib/search-engines.js new file mode 100644 index 0000000000..0c8d7b5dd0 --- /dev/null +++ b/app/lib/search-engines.js @@ -0,0 +1,33 @@ + +// TODO: autoCompleteCallback takes a string, calls the search engine autocomplete API, +// and gives back a result / some results so that we can show a result (or results) as +// the user types in the address bar? Maybe in a separate pull request... + +var searchEngines = [ + { + name: "DuckDuckGo", + makeQueryUrl: (text) => `https://duckduckgo.com?q=${encodeURIComponent(text)}`, + autoCompleteCallback: (searchStr, cb) => {} + }, + { + name: "Google", + makeQueryUrl: (text) => `https://google.com/search?q=${encodeURIComponent(text)}`, + autoCompleteCallback: (searchStr, cb) => {} + } +] + +export function getDefaultSearchEngine() { + return searchEngines[0]; +} + +export function getAvailableSearchEngines() { + return searchEngines.map(engine => engine.name) +} + +export function getSearchEngineOrDefault(name) { + if (!name) { + return getDefaultSearchEngine(); + } else { + return searchEngines.find( engine => engine.name === name ) || getDefaultSearchEngine(); + } +} diff --git a/app/shell-window/ui/navbar.js b/app/shell-window/ui/navbar.js index 640741859d..d6f482f368 100644 --- a/app/shell-window/ui/navbar.js +++ b/app/shell-window/ui/navbar.js @@ -469,6 +469,8 @@ async function handleAutocompleteSearch (results) { // set the top results accordingly var gotoResult = { url: vWithProtocol, title: 'Go to ' + v, isGuessingTheScheme } + + // todo: how do we get the search settings from here? var searchResult = { search: v, title: 'DuckDuckGo Search', @@ -518,6 +520,8 @@ function examineLocationInput (v) { isGuessingTheScheme = true // note that we're guessing so that, if this fails, we can try http:// } } + + // todo: how do we get the search settings from here? var vSearch = 'https://duckduckgo.com/?q=' + v.split(' ').map(encodeURIComponent).join('+') return {vWithProtocol, vSearch, isProbablyUrl, isGuessingTheScheme} } @@ -878,4 +882,4 @@ function onContextMenu (e) { page.navbarEl.querySelector('.nav-location-input').blur() page.loadURL(url) } -} \ No newline at end of file +}