Skip to content
This repository was archived by the owner on Dec 11, 2019. It is now read-only.

Add Yandex.com as a search engine #5964

Merged
merged 1 commit into from
Dec 1, 2016
Merged
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
3 changes: 2 additions & 1 deletion js/components/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,8 @@ class Main extends ImmutableComponent {
if (entry.name === engine) {
windowActions.setSearchDetail(Immutable.fromJS({
searchURL: entry.search,
autocompleteURL: entry.autocomplete
autocompleteURL: entry.autocomplete,
platformClientId: entry.platformClientId
}))
this.lastLoadedSearchProviders = engine
return false
Expand Down
64 changes: 56 additions & 8 deletions js/components/urlBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,59 @@ class UrlBar extends ImmutableComponent {
windowActions.setRenderUrlBarSuggestions(false)
}

/**
* Assign client ID based on user's os/platform.
* `platformClientId` must be populated for this entry in `searchProviders.js`.
*/
getPlatformClientId (provider) {
try {
if (provider.platformClientId) {
const platformUtil = require('../../app/common/lib/platformUtil')
if (platformUtil.isWindows()) {
return provider.platformClientId.win32 || ''
} else if (platformUtil.isDarwin()) {
return provider.platformClientId.darwin || ''
}
return provider.platformClientId.linux || ''
}
} catch (e) { }
return ''
}

/**
* Build a search URL considering:
* - user's default search engine provider
* - search engine shortcut keywords
*
* Future considerations could include criteria such as:
* - user's country / locale (amazon.com vs amazon.ca)
* - http verb
*/
buildSearchUrl (searchTerms) {
let provider = this.props.searchDetail.toJS()
let url = provider.searchURL

// remove shortcut from the search terms
if (this.activateSearchEngine && this.searchSelectEntry !== null) {
provider = this.searchSelectEntry
const shortcut = new RegExp('^' + provider.shortcut + ' ', 'g')
searchTerms = searchTerms.replace(shortcut, '')
url = provider.search
}

// required: populate the search terms (URL encoded)
url = url.replace('{searchTerms}',
encodeURIComponent(searchTerms))

// optional: populate the client id
// some search engines have a different clientId depending on the platform
if (url.indexOf('{platformClientId}') > -1) {
url = url.replace('{platformClientId}',
this.getPlatformClientId(provider))
}
return url
}

onKeyDown (e) {
if (!this.isActive) {
windowActions.setUrlBarActive(true)
Expand Down Expand Up @@ -121,14 +174,9 @@ class UrlBar extends ImmutableComponent {
// load the selected suggestion
this.urlBarSuggestions.clickSelected(e)
} else {
let searchUrl = this.props.searchDetail.get('searchURL').replace('{searchTerms}', encodeURIComponent(location))
if (this.activateSearchEngine && this.searchSelectEntry !== null && !isLocationUrl) {
const replaceRE = new RegExp('^' + this.searchSelectEntry.shortcut + ' ', 'g')
location = location.replace(replaceRE, '')
searchUrl = this.searchSelectEntry.search.replace('{searchTerms}', encodeURIComponent(location))
}

location = isLocationUrl ? location : searchUrl
location = isLocationUrl
? location
: this.buildSearchUrl(location)
// do search.
if (e.altKey) {
windowActions.newFrame({ location }, true)
Expand Down
12 changes: 12 additions & 0 deletions js/data/searchProviders.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,18 @@ module.exports = { "providers" :
"search" : "https://www.qwant.com/?q={searchTerms}&client=brave",
"autocomplete": "https://api.qwant.com/api/suggest/?q={searchTerms}&client=brave",
"shortcut" : ":q"
},
{
"name" : "Yandex",
"base" : "https://yandex.com",
"image" : "https://yastatic.net/islands-icons/_/6jyHGXR8-HAc8oJ1bU8qMUQQz_g.ico",
"search" : "https://yandex.com/search/?text={searchTerms}&clid={platformClientId}",
"shortcut" : ":ya",
"platformClientId": {
"win32": 2274777,
"darwin": 2274776,
"linux": 2274778
}
}
]
}
25 changes: 8 additions & 17 deletions test/components/navigationBarTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,31 +381,22 @@ describe('navigationBar tests', function () {
yield setup(this.app.client)
})

it('Uses the default favicon when one is not specified', function * () {
const page1Url = Brave.server.url('page1.html')
yield this.app.client.tabByUrl(Brave.newTabUrl).url(page1Url).waitForUrl(page1Url).windowParentByUrl(page1Url)
yield this.app.client.waitUntil(() =>
this.app.client.getCssProperty(activeTabFavicon, 'background-image').then((backgroundImage) =>
backgroundImage.value === `url("${Brave.server.url('favicon.ico')}")`
))
})

it('Parses favicon when one is present', function * () {
const pageWithFavicon = Brave.server.url('favicon.html')
yield this.app.client.tabByUrl(Brave.newTabUrl).url(pageWithFavicon).waitForUrl(pageWithFavicon).windowParentByUrl(pageWithFavicon)
yield this.app.client.waitUntil(() =>
this.app.client.getCssProperty(activeTabFavicon, 'background-image').then((backgroundImage) =>
backgroundImage.value === `url("${Brave.server.url('img/test.ico')}")`
))
yield this.app.client.waitUntil(function () {
return this.getCssProperty(activeTabFavicon, 'background-image').then((backgroundImage) =>
backgroundImage.value === `url("${Brave.server.url('img/test.ico')}")`)
})
})

it('Fallback to default icon when no one is specified', function * () {
const pageWithNoFavicon = Brave.server.url('page_no_favicon.html')
yield this.app.client.tabByUrl(Brave.newTabUrl).url(pageWithNoFavicon).waitForUrl(pageWithNoFavicon).windowParentByUrl(pageWithNoFavicon)
yield this.app.client.waitUntil(() =>
this.app.client.getAttribute(activeTabFavicon, 'class').then((className) =>
className === 'tabIcon bookmarkFile fa fa-file-o'
))
yield this.app.client.waitUntil(function () {
return this.getAttribute(activeTabFavicon, 'class').then((className) =>
className === 'tabIcon bookmarkFile fa fa-file-o')
})
})
})

Expand Down