Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.2][Fix] Favourites not showing when home button is pressed in browser tab menu #4175

Merged
merged 14 commits into from
May 13, 2022
Merged
48 changes: 26 additions & 22 deletions app/components/Views/BrowserTab/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,31 @@ export const BrowserTab = (props) => {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [notifyAllConnections, props.approvedHosts, props.selectedAddress]);

/**
* Inject home page scripts to get the favourites and set analytics key
*/
const injectHomePageScripts = async () => {
const { current } = webviewRef;
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}";
(function () {
try {
window.dispatchEvent(new Event('metamask_onHomepageScriptsInjected'));
} catch (e) {
//Nothing to do
}
})()
`;

current.injectJavaScript(homepageScripts);
};

const initializeBackgroundBridge = (urlBridge, isMainFrame) => {
const newBridge = new BackgroundBridge({
webview: webviewRef,
Expand All @@ -381,6 +406,7 @@ export const BrowserTab = (props) => {
// Wizard
wizardScrollAdjusted,
tabId: props.id,
injectHomePageScripts,
}),
isMainFrame,
});
Expand Down Expand Up @@ -467,25 +493,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
*/
Expand Down Expand Up @@ -871,9 +878,6 @@ export const BrowserTab = (props) => {
}

icon.current = null;
if (isHomepage(nativeEvent.url)) {
injectHomePageScripts();
}

// Reset the previous bridges
backgroundBridges.current.length && backgroundBridges.current.forEach((bridge) => bridge.onDisconnect());
Expand Down
9 changes: 9 additions & 0 deletions app/core/RPCMethods/RPCMethodMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ interface RPCMethodsMiddleParameters {
tabId: string;
// For WalletConnect
isWalletConnect: boolean;
injectHomePageScripts: () => void;
}

export const checkActiveAccountAndChainId = ({ address, chainId, activeAccounts }: any) => {
Expand Down Expand Up @@ -118,6 +119,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) => {
Expand Down Expand Up @@ -510,6 +512,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.
Expand Down