Skip to content
This repository has been archived by the owner on Dec 27, 2022. It is now read-only.

[WIP]: start adding support for other search engines. ( issue #1033 ) #1052

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
37 changes: 37 additions & 0 deletions app/builtin-pages/views/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
// =

Expand Down Expand Up @@ -108,6 +110,7 @@ function renderGeneral () {
${renderAutoUpdater()}
${renderDefaultSyncPathSettings()}
${renderProtocolSettings()}
${renderSearchEngineSettings()}
${renderOnStartupSettings()}
${renderDefaultDatIgnoreSettings()}
${renderAnalyticsSettings()}
Expand Down Expand Up @@ -278,6 +281,40 @@ function renderProtocolSettings () {
</div>`
}

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`
<div class="section default-browser">
<h2 id="search-section" class="subtitle-heading">Default Search Engine Settings</h2>

<p>
Use the following search engine to search
</p>

<select id="search-engine-chooser" onchange=${(event) => changeSearchEngine(event.target.value)}>
${engines.map( engine => { return (engine == currentSearchEngine) ? yo`
<option selected>${engine}</option>
` :
yo `<option>${engine}</option>`
}
)}
</select>


</div>`
}

function renderAutoUpdater () {
if (!browserInfo.updater.isBrowserUpdatesSupported) {
return yo`
Expand Down
15 changes: 10 additions & 5 deletions app/builtin-pages/views/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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()
Expand Down
33 changes: 33 additions & 0 deletions app/lib/search-engines.js
Original file line number Diff line number Diff line change
@@ -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();
}
}
6 changes: 5 additions & 1 deletion app/shell-window/ui/navbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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}
}
Expand Down Expand Up @@ -878,4 +882,4 @@ function onContextMenu (e) {
page.navbarEl.querySelector('.nav-location-input').blur()
page.loadURL(url)
}
}
}