diff --git a/app/components/Views/BrowserTab/index.js b/app/components/Views/BrowserTab/index.js index df196191220..cdb0f645aa6 100644 --- a/app/components/Views/BrowserTab/index.js +++ b/app/components/Views/BrowserTab/index.js @@ -61,7 +61,7 @@ import downloadFile from '../../../util/browser/downloadFile'; import { createBrowserUrlModalNavDetails } from '../BrowserUrlModal/BrowserUrlModal'; const { HOMEPAGE_URL, USER_AGENT, NOTIFICATION_NAMES } = AppConstants; -const HOMEPAGE_HOST = 'home.metamask.io'; +const HOMEPAGE_HOST = new URL(HOMEPAGE_URL)?.hostname; const MM_MIXPANEL_TOKEN = process.env.MM_MIXPANEL_TOKEN; const createStyles = (colors) => @@ -425,25 +425,6 @@ export const BrowserTab = (props) => { return bookmarks.some(({ url: bookmark }) => bookmark === maskedUrl); }; - /** - * Inject home page scripts to get the favourites and set analytics key - */ - const injectHomePageScripts = async () => { - const { current } = webviewRef; - if (!current) return; - const analyticsEnabled = Analytics.getEnabled(); - const disctinctId = await Analytics.getDistinctId(); - const homepageScripts = ` - window.__mmFavorites = ${JSON.stringify(props.bookmarks)}; - window.__mmSearchEngine = "${props.searchEngine}"; - window.__mmMetametrics = ${analyticsEnabled}; - window.__mmDistinctId = "${disctinctId}"; - window.__mmMixpanelToken = "${MM_MIXPANEL_TOKEN}"; - `; - - current.injectJavaScript(homepageScripts); - }; - /** * Show a phishing modal when a url is not allowed */ @@ -667,6 +648,31 @@ export const BrowserTab = (props) => { }; }, [goBack, isTabActive, props.navigation]); + /** + * Inject home page scripts to get the favourites and set analytics key + */ + const injectHomePageScripts = async (bookmarks) => { + const { current } = webviewRef; + const analyticsEnabled = Analytics.getEnabled(); + const disctinctId = await Analytics.getDistinctId(); + const homepageScripts = ` + window.__mmFavorites = ${JSON.stringify(bookmarks || props.bookmarks)}; + window.__mmSearchEngine = "${props.searchEngine}"; + window.__mmMetametrics = ${analyticsEnabled}; + window.__mmDistinctId = "${disctinctId}"; + window.__mmMixpanelToken = "${MM_MIXPANEL_TOKEN}"; + (function () { + try { + window.dispatchEvent(new Event('metamask_onHomepageScriptsInjected')); + } catch (e) { + //Nothing to do + } + })() + `; + + current.injectJavaScript(homepageScripts); + }; + /** * Handles state changes for when the url changes */ diff --git a/app/core/RPCMethods/RPCMethodMiddleware.ts b/app/core/RPCMethods/RPCMethodMiddleware.ts index 34d5e80651f..31135df0aeb 100644 --- a/app/core/RPCMethods/RPCMethodMiddleware.ts +++ b/app/core/RPCMethods/RPCMethodMiddleware.ts @@ -52,6 +52,7 @@ interface RPCMethodsMiddleParameters { tabId: string; // For WalletConnect isWalletConnect: boolean; + injectHomePageScripts: (bookmarks?: []) => void; } export const checkActiveAccountAndChainId = ({ @@ -132,6 +133,7 @@ export const getRpcMethodMiddleware = ({ tabId, // For WalletConnect isWalletConnect, + injectHomePageScripts, }: RPCMethodsMiddleParameters) => // all user facing RPC calls not implemented by the provider createAsyncMiddleware(async (req: any, res: any, next: any) => { @@ -545,6 +547,12 @@ export const getRpcMethodMiddleware = ({ store.dispatch(removeBookmark(bookmark)); + const { bookmarks: updatedBookmarks } = store.getState(); + + if (isHomepage()) { + injectHomePageScripts(updatedBookmarks); + } + res.result = { favorites: bookmarks, }; @@ -583,6 +591,13 @@ export const getRpcMethodMiddleware = ({ res.result = true; }, + metamask_injectHomepageScripts: async () => { + if (isHomepage()) { + injectHomePageScripts(); + } + res.result = true; + }, + /** * This method is used by the inpage provider to get its state on * initialization. diff --git a/locales/languages/en.json b/locales/languages/en.json index eb11c3c0bed..1acce63876d 100644 --- a/locales/languages/en.json +++ b/locales/languages/en.json @@ -1902,7 +1902,7 @@ "or": "or" }, "download_files": { - "error": "Failed to download attachment.", - "unknownError": "Unknown error occurred" + "error": "Failed to download attachment.", + "unknownError": "Unknown error occurred" } }