1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-23 10:30:04 +01:00
metamask-extension/app/scripts/platforms/extension.js
Mark Stacey 12e055a37c
Close window after opening fullscreen (#6966)
* Add background environment type

The `getEnvironmentType` method now checks for the background
environment as well, instead of returning 'notification' for that case.

Instead of adding another regex for the background path, the regexes
for each environment have been replaced with the URL constructor[0].
This is the standard method of parsing URLs, and is available in all
supported browsers.

[0]: https://developer.mozilla.org/en-US/docs/Web/API/URL

* Add note regarding a missing manifest permission

The `url` parameter to `tabs.query(...)` requires the `tabs` permission,
and will be ignored otherwise. We are missing this permission, so that
call does not work.

* Close window after opening full screen

The browser behaviour when opening a new tab differs between Chrome and
Firefox. In the case of a popup, Chrome will close the popup whereas
Firefox will leave it open. In the case of the notification window,
Chrome will move the new tab to the foreground, whereas Firefox will
leave the notification window in the foreground when opening a new tab.

We always want to close the current UI (popup or notification) when
switching to a full-screen view. The only exception to this is when the
switch is triggered from the background, which has no UI.

Closes #6513, #6685
2019-08-08 11:50:32 -03:00

114 lines
2.8 KiB
JavaScript

const extension = require('extensionizer')
const {createExplorerLink: explorerLink} = require('etherscan-link')
const {getEnvironmentType} = require('../lib/util')
const {ENVIRONMENT_TYPE_BACKGROUND} = require('../lib/enums')
class ExtensionPlatform {
//
// Public
//
reload () {
extension.runtime.reload()
}
openWindow ({ url }) {
extension.tabs.create({ url })
}
closeCurrentWindow () {
return extension.windows.getCurrent((windowDetails) => {
return extension.windows.remove(windowDetails.id)
})
}
getVersion () {
return extension.runtime.getManifest().version
}
openExtensionInBrowser (route = null, queryString = null) {
let extensionURL = extension.runtime.getURL('home.html')
if (queryString) {
extensionURL += `?${queryString}`
}
if (route) {
extensionURL += `#${route}`
}
this.openWindow({ url: extensionURL })
if (getEnvironmentType() !== ENVIRONMENT_TYPE_BACKGROUND) {
window.close()
}
}
getPlatformInfo (cb) {
try {
extension.runtime.getPlatformInfo((platform) => {
cb(null, platform)
})
} catch (e) {
cb(e)
}
}
showTransactionNotification (txMeta) {
const { status, txReceipt: { status: receiptStatus } = {} } = txMeta
if (status === 'confirmed') {
// There was an on-chain failure
receiptStatus === '0x0'
? this._showFailedTransaction(txMeta, 'Transaction encountered an error.')
: this._showConfirmedTransaction(txMeta)
} else if (status === 'failed') {
this._showFailedTransaction(txMeta)
}
}
_showConfirmedTransaction (txMeta) {
this._subscribeToNotificationClicked()
const url = explorerLink(txMeta.hash, parseInt(txMeta.metamaskNetworkId))
const nonce = parseInt(txMeta.txParams.nonce, 16)
const title = 'Confirmed transaction'
const message = `Transaction ${nonce} confirmed! View on Etherscan`
this._showNotification(title, message, url)
}
_showFailedTransaction (txMeta, errorMessage) {
const nonce = parseInt(txMeta.txParams.nonce, 16)
const title = 'Failed transaction'
const message = `Transaction ${nonce} failed! ${errorMessage || txMeta.err.message}`
this._showNotification(title, message)
}
_showNotification (title, message, url) {
extension.notifications.create(
url,
{
'type': 'basic',
'title': title,
'iconUrl': extension.extension.getURL('../../images/icon-64.png'),
'message': message,
})
}
_subscribeToNotificationClicked () {
if (!extension.notifications.onClicked.hasListener(this._viewOnEtherscan)) {
extension.notifications.onClicked.addListener(this._viewOnEtherscan)
}
}
_viewOnEtherscan (txId) {
if (txId.startsWith('http://')) {
extension.tabs.create({ url: txId })
}
}
}
module.exports = ExtensionPlatform