diff --git a/app/browser/reducers/dappReducer.js b/app/browser/reducers/dappReducer.js index daf88f1d937..daec46ce2af 100644 --- a/app/browser/reducers/dappReducer.js +++ b/app/browser/reducers/dappReducer.js @@ -15,13 +15,16 @@ const getSetting = require('../../../js/settings').getSetting const tabActions = require('../../common/actions/tabActions') const tabState = require('../../common/state/tabState') const config = require('../../../js/constants/config') +const {getOrigin} = require('../../../js/lib/urlutil') + +let notificationCallbacks = [] const dappReducer = (state, action, immutableAction) => { action = immutableAction || makeImmutable(action) switch (action.get('actionType')) { case appConstants.APP_DAPP_AVAILABLE: - if (!getSetting(settings.METAMASK_PROMPT_DISMISSED) && !getSetting(settings.METAMASK_ENABLED)) { - showDappNotification() + if (!getSetting(settings.METAMASK_ENABLED)) { + showDappNotification(action.get('location')) } break } @@ -31,20 +34,8 @@ const dappReducer = (state, action, immutableAction) => { const notifications = { text: { greeting: locale.translation('updateHello'), - message: locale.translation('dappDetected'), enable: locale.translation('dappEnableExtension'), dismiss: locale.translation('dappDismiss') - }, - onResponse: (message, buttonIndex) => { - // Index 0 is for dismiss - if (buttonIndex === 0) { - appActions.changeSetting(settings.METAMASK_PROMPT_DISMISSED, true) - } - // Index 1 is for enable - if (buttonIndex === 1) { - appActions.changeSetting(settings.METAMASK_ENABLED, true) - } - appActions.hideNotification(message) } } @@ -54,28 +45,52 @@ process.on('extension-ready', (installInfo) => { } }) -const showDappNotification = () => { +const showDappNotification = (location) => { + const origin = getOrigin(location) + if (!origin || getSetting(settings.METAMASK_PROMPT_DISMISSED)) { + return + } + + const message = locale.translation('dappDetected').replace(/{{\s*origin\s*}}/, origin) + appActions.showNotification({ greeting: notifications.text.greeting, - message: notifications.text.message, + message: message, buttons: [ {text: notifications.text.dismiss}, {text: notifications.text.enable} ], + frameOrigin: origin, options: { - persist: false + persist: true } }) -} -if (ipcMain) { - ipcMain.on(messages.NOTIFICATION_RESPONSE, (e, message, buttonIndex) => { - switch (message) { - case notifications.text.message: - notifications.onResponse(message, buttonIndex) - break + if (!notificationCallbacks[message]) { + notificationCallbacks[message] = (e, msg, buttonIndex, persist) => { + if (msg === message) { + appActions.hideNotification(message) + // Index 0 is for dismiss + if (buttonIndex === 0) { + if (persist) { + appActions.changeSetting(settings.METAMASK_PROMPT_DISMISSED, true) + } + } + + // Index 1 is for enable + if (buttonIndex === 1) { + appActions.changeSetting(settings.METAMASK_ENABLED, true) + } + + if (notificationCallbacks[message]) { + ipcMain.removeListener(messages.NOTIFICATION_RESPONSE, notificationCallbacks[message]) + delete notificationCallbacks[message] + } + } } - }) + } + + ipcMain.on(messages.NOTIFICATION_RESPONSE, notificationCallbacks[message]) } module.exports = dappReducer diff --git a/app/extensions/brave/locales/en-US/app.properties b/app/extensions/brave/locales/en-US/app.properties index 2f8be34ea91..3acb222222e 100644 --- a/app/extensions/brave/locales/en-US/app.properties +++ b/app/extensions/brave/locales/en-US/app.properties @@ -267,7 +267,7 @@ windowCaptionButtonClose=Close windowCaptionButtonMaximize=Maximize windowCaptionButtonMinimize=Minimize windowCaptionButtonRestore=Restore Down -dappDetected=This page contains a Dapp, would you like to enable the MetaMask extension? +dappDetected={{origin}} contains a Dapp, would you like to enable the MetaMask extension? dappDismiss=No thanks dappEnableExtension=Install MetaMask windowCaptionButtonMinimize=Minimize