From 8255147e5c649d6fd7fbd3d514358e01989f9388 Mon Sep 17 00:00:00 2001 From: Brian Clifton Date: Thu, 1 Dec 2016 02:37:12 -0700 Subject: [PATCH] Add Yandex.com as a search engine Fixes https://github.com/brave/browser-laptop/issues/2703 Also includes - small refactor in js/components/urlBar.js which should make future changes easier (and opens this up to unit testing) - removes permafail webdriver test that should have been removed with https://github.com/brave/browser-laptop/pull/5826 (I missed the fact CI failed on this when reviewing) Auditors: @bbondy, @cezaraugusto Test Plan: 1. Launch Brave and open Preferences 2. Set Yandex as your default search engine 3. Search in the omnibox, confirm it shows results on yandex.com 4. Check the URL that was created; client ID (clid=) should be: - 2274777 on Windows - 2274776 on macOS - 2274778 on linux 5. Try the search engine shortcut, :ya and do a search in the omnibox --- js/components/main.js | 3 +- js/components/urlBar.js | 64 ++++++++++++++++++++++++---- js/data/searchProviders.js | 12 ++++++ test/components/navigationBarTest.js | 25 ++++------- 4 files changed, 78 insertions(+), 26 deletions(-) diff --git a/js/components/main.js b/js/components/main.js index 13a3a60ab1b..65b97c5e637 100644 --- a/js/components/main.js +++ b/js/components/main.js @@ -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 diff --git a/js/components/urlBar.js b/js/components/urlBar.js index 9b4d306a5d3..e4abb175fb7 100644 --- a/js/components/urlBar.js +++ b/js/components/urlBar.js @@ -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) @@ -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) diff --git a/js/data/searchProviders.js b/js/data/searchProviders.js index 2b79a9df4dd..17ecd8d06dd 100644 --- a/js/data/searchProviders.js +++ b/js/data/searchProviders.js @@ -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 + } } ] } diff --git a/test/components/navigationBarTest.js b/test/components/navigationBarTest.js index 150c1e98af6..265f249ac27 100644 --- a/test/components/navigationBarTest.js +++ b/test/components/navigationBarTest.js @@ -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') + }) }) })