From d13f12413706b569d74f7ed83c3c65db4f5eb4fe Mon Sep 17 00:00:00 2001 From: Esteban Mino Date: Wed, 15 May 2019 12:47:39 -0400 Subject: [PATCH 1/7] if on title query for title when adding bookmark --- app/components/Views/BrowserTab/index.js | 69 ++++++++++++++++-------- 1 file changed, 47 insertions(+), 22 deletions(-) diff --git a/app/components/Views/BrowserTab/index.js b/app/components/Views/BrowserTab/index.js index 843a061ebaec..f70074f7f014 100644 --- a/app/components/Views/BrowserTab/index.js +++ b/app/components/Views/BrowserTab/index.js @@ -974,35 +974,60 @@ export class BrowserTab extends PureComponent { } }; - bookmark = () => { + addBookmark = async () => { this.toggleOptionsIfNeeded(); // Check it doesn't exist already if (this.props.bookmarks.filter(i => i.url === this.state.inputValue).length) { Alert.alert(strings('browser.error'), strings('browser.bookmark_already_exists')); return false; } - - this.props.navigation.push('AddBookmarkView', { - title: this.state.currentPageTitle || '', - url: this.state.inputValue, - onAddBookmark: async ({ name, url }) => { - this.props.addBookmark({ name, url }); - if (Platform.OS === 'ios') { - const item = { - uniqueIdentifier: url, - title: name || url, - contentDescription: `Launch ${name || url} on MetaMask`, - keywords: [name.split(' '), url, 'dapp'], - thumbnail: { uri: `https://api.faviconkit.com/${getHost(url)}/256` } - }; - try { - SearchApi.indexSpotlightItem(item); - } catch (e) { - Logger.error('Error adding to spotlight', e); + if (!this.state.currentPageTitle) { + // We need to get the title to add bookmark + const { current } = this.webview; + const js = ` + (function () { + const shortcutIcon = window.document.querySelector('head > link[rel="shortcut icon"]'); + const icon = shortcutIcon || Array.from(window.document.querySelectorAll('head > link[rel="icon"]')).find((icon) => Boolean(icon.href)); + + const siteName = document.querySelector('head > meta[property="og:site_name"]'); + const title = siteName || document.querySelector('head > meta[name="title"]'); + window.postMessageToNative( + { + type: 'GET_TITLE_FOR_BOOKMARK', + payload: { + title: title ? title.content : document.title, + url: location.href, + icon: icon && icon.href + } + } + ) + })(); + `; + Platform.OS === 'ios' ? current.evaluateJavaScript(js) : current.injectJavaScript(js); + } + await setTimeout(() => { + this.props.navigation.push('AddBookmarkView', { + title: this.state.currentPageTitle || '', + url: this.state.inputValue, + onAddBookmark: async ({ name, url }) => { + this.props.addBookmark({ name, url }); + if (Platform.OS === 'ios') { + const item = { + uniqueIdentifier: url, + title: name || url, + contentDescription: `Launch ${name || url} on MetaMask`, + keywords: [name.split(' '), url, 'dapp'], + thumbnail: { uri: `https://api.faviconkit.com/${getHost(url)}/256` } + }; + try { + SearchApi.indexSpotlightItem(item); + } catch (e) { + Logger.error('Error adding to spotlight', e); + } } } - } - }); + }); + }, 500); }; share = () => { @@ -1278,7 +1303,7 @@ export class BrowserTab extends PureComponent { {strings('browser.home')} -