diff --git a/app/scripts/lib/enums.js b/app/scripts/lib/enums.js index c6d57a1bc..32c0947a3 100644 --- a/app/scripts/lib/enums.js +++ b/app/scripts/lib/enums.js @@ -1,6 +1,7 @@ const ENVIRONMENT_TYPE_POPUP = 'popup' const ENVIRONMENT_TYPE_NOTIFICATION = 'notification' const ENVIRONMENT_TYPE_FULLSCREEN = 'fullscreen' +const ENVIRONMENT_TYPE_BACKGROUND = 'background' const PLATFORM_BRAVE = 'Brave' const PLATFORM_CHROME = 'Chrome' @@ -12,6 +13,7 @@ module.exports = { ENVIRONMENT_TYPE_POPUP, ENVIRONMENT_TYPE_NOTIFICATION, ENVIRONMENT_TYPE_FULLSCREEN, + ENVIRONMENT_TYPE_BACKGROUND, PLATFORM_BRAVE, PLATFORM_CHROME, PLATFORM_EDGE, diff --git a/app/scripts/lib/util.js b/app/scripts/lib/util.js index ea13b26be..2eb71c0a0 100644 --- a/app/scripts/lib/util.js +++ b/app/scripts/lib/util.js @@ -5,6 +5,7 @@ const { ENVIRONMENT_TYPE_POPUP, ENVIRONMENT_TYPE_NOTIFICATION, ENVIRONMENT_TYPE_FULLSCREEN, + ENVIRONMENT_TYPE_BACKGROUND, PLATFORM_FIREFOX, PLATFORM_OPERA, PLATFORM_CHROME, @@ -28,17 +29,21 @@ function getStack () { * - 'popup' refers to the extension opened through the browser app icon (in top right corner in chrome and firefox) * - 'responsive' refers to the main browser window * - 'notification' refers to the popup that appears in its own window when taking action outside of metamask + * - 'background' refers to the background page * * @returns {string} A single word label that represents the type of window through which the app is being viewed * */ const getEnvironmentType = (url = window.location.href) => { - if (url.match(/popup.html(?:#.*)*$/)) { + const parsedUrl = new URL(url) + if (parsedUrl.pathname === '/popup.html') { return ENVIRONMENT_TYPE_POPUP - } else if (url.match(/home.html(?:\?.+)*$/) || url.match(/home.html(?:#.*)*$/)) { + } else if (parsedUrl.pathname === '/home.html') { return ENVIRONMENT_TYPE_FULLSCREEN - } else { + } else if (parsedUrl.pathname === '/notification.html') { return ENVIRONMENT_TYPE_NOTIFICATION + } else { + return ENVIRONMENT_TYPE_BACKGROUND } } diff --git a/app/scripts/platforms/extension.js b/app/scripts/platforms/extension.js index 43820515d..d54a8a7b3 100644 --- a/app/scripts/platforms/extension.js +++ b/app/scripts/platforms/extension.js @@ -1,6 +1,9 @@ const extension = require('extensionizer') const {createExplorerLink: explorerLink} = require('etherscan-link') +const {getEnvironmentType} = require('../lib/util') +const {ENVIRONMENT_TYPE_BACKGROUND} = require('../lib/enums') + class ExtensionPlatform { // @@ -35,6 +38,9 @@ class ExtensionPlatform { extensionURL += `#${route}` } this.openWindow({ url: extensionURL }) + if (getEnvironmentType() !== ENVIRONMENT_TYPE_BACKGROUND) { + window.close() + } } getPlatformInfo (cb) { diff --git a/app/vendor/trezor/usb-permissions.js b/app/vendor/trezor/usb-permissions.js index 9de47e0a1..18f70f94f 100644 --- a/app/vendor/trezor/usb-permissions.js +++ b/app/vendor/trezor/usb-permissions.js @@ -25,6 +25,7 @@ const switchToPopupTab = (event) => { return; } + // TODO: remove this query, or add `tabs` permission. This does not work. // triggered from 'beforeunload' event // find tab by popup pattern and switch to it chrome.tabs.query({ @@ -47,4 +48,4 @@ window.addEventListener('message', event => { } }); -window.addEventListener('beforeunload', switchToPopupTab); \ No newline at end of file +window.addEventListener('beforeunload', switchToPopupTab); diff --git a/test/unit/app/util-test.js b/test/unit/app/util-test.js index 656b22d92..259bd708b 100644 --- a/test/unit/app/util-test.js +++ b/test/unit/app/util-test.js @@ -1,6 +1,48 @@ const assert = require('assert') -const { sufficientBalance } = require('../../../app/scripts/lib/util') +const { getEnvironmentType, sufficientBalance } = require('../../../app/scripts/lib/util') +const { + ENVIRONMENT_TYPE_POPUP, + ENVIRONMENT_TYPE_NOTIFICATION, + ENVIRONMENT_TYPE_FULLSCREEN, + ENVIRONMENT_TYPE_BACKGROUND, +} = require('../../../app/scripts/lib/enums') +describe('getEnvironmentType', function () { + it('should return popup type', function () { + const environmentType = getEnvironmentType('http://extension-id/popup.html') + assert.equal(environmentType, ENVIRONMENT_TYPE_POPUP) + }) + + it('should return notification type', function () { + const environmentType = getEnvironmentType('http://extension-id/notification.html') + assert.equal(environmentType, ENVIRONMENT_TYPE_NOTIFICATION) + }) + + it('should return fullscreen type', function () { + const environmentType = getEnvironmentType('http://extension-id/home.html') + assert.equal(environmentType, ENVIRONMENT_TYPE_FULLSCREEN) + }) + + it('should return background type', function () { + const environmentType = getEnvironmentType('http://extension-id/_generated_background_page.html') + assert.equal(environmentType, ENVIRONMENT_TYPE_BACKGROUND) + }) + + it('should return the correct type for a URL with a hash fragment', function () { + const environmentType = getEnvironmentType('http://extension-id/popup.html#hash') + assert.equal(environmentType, ENVIRONMENT_TYPE_POPUP) + }) + + it('should return the correct type for a URL with query parameters', function () { + const environmentType = getEnvironmentType('http://extension-id/popup.html?param=foo') + assert.equal(environmentType, ENVIRONMENT_TYPE_POPUP) + }) + + it('should return the correct type for a URL with query parameters and a hash fragment', function () { + const environmentType = getEnvironmentType('http://extension-id/popup.html?param=foo#hash') + assert.equal(environmentType, ENVIRONMENT_TYPE_POPUP) + }) +}) describe('SufficientBalance', function () { it('returns true if max tx cost is equal to balance.', function () {