2016-07-21 19:45:32 +02:00
|
|
|
const extension = require('./extension')
|
2016-10-04 22:10:28 +02:00
|
|
|
const height = 520
|
2016-09-13 21:14:32 +02:00
|
|
|
const width = 360
|
2016-03-12 02:13:48 +01:00
|
|
|
|
2016-07-12 05:53:26 +02:00
|
|
|
const notifications = {
|
2016-08-24 00:44:50 +02:00
|
|
|
show,
|
2016-08-23 20:40:08 +02:00
|
|
|
getPopup,
|
2016-08-24 00:44:50 +02:00
|
|
|
closePopup,
|
2016-04-28 23:16:24 +02:00
|
|
|
}
|
2016-07-12 00:45:57 +02:00
|
|
|
module.exports = notifications
|
|
|
|
window.METAMASK_NOTIFIER = notifications
|
2016-03-12 02:13:48 +01:00
|
|
|
|
2016-08-24 00:44:50 +02:00
|
|
|
function show () {
|
2016-09-02 22:29:47 +02:00
|
|
|
getPopup((err, popup) => {
|
|
|
|
if (err) throw err
|
2016-08-23 02:18:14 +02:00
|
|
|
|
2016-09-02 22:29:47 +02:00
|
|
|
if (popup) {
|
|
|
|
// bring focus to existing popup
|
|
|
|
extension.windows.update(popup.id, { focused: true })
|
|
|
|
} else {
|
|
|
|
// create new popup
|
|
|
|
extension.windows.create({
|
|
|
|
url: 'notification.html',
|
|
|
|
type: 'popup',
|
|
|
|
focused: true,
|
2016-09-13 21:14:32 +02:00
|
|
|
width,
|
|
|
|
height,
|
2016-09-02 22:29:47 +02:00
|
|
|
})
|
|
|
|
}
|
2016-04-28 23:16:24 +02:00
|
|
|
})
|
2016-06-25 02:22:27 +02:00
|
|
|
}
|
2016-06-23 04:28:11 +02:00
|
|
|
|
2016-11-11 19:26:12 +01:00
|
|
|
function getWindows (cb) {
|
2016-08-23 20:48:46 +02:00
|
|
|
// Ignore in test environment
|
|
|
|
if (!extension.windows) {
|
2016-09-01 20:22:29 +02:00
|
|
|
return cb()
|
2016-08-23 20:48:46 +02:00
|
|
|
}
|
|
|
|
|
2016-08-23 20:40:08 +02:00
|
|
|
extension.windows.getAll({}, (windows) => {
|
2016-09-01 20:22:29 +02:00
|
|
|
cb(null, windows)
|
|
|
|
})
|
|
|
|
}
|
2016-08-23 20:40:08 +02:00
|
|
|
|
2016-11-11 19:26:12 +01:00
|
|
|
function getPopup (cb) {
|
2016-09-02 22:29:47 +02:00
|
|
|
getWindows((err, windows) => {
|
|
|
|
if (err) throw err
|
|
|
|
cb(null, getPopupIn(windows))
|
2016-08-23 20:40:08 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-11-11 19:26:12 +01:00
|
|
|
function getPopupIn (windows) {
|
2016-09-13 21:14:32 +02:00
|
|
|
return windows ? windows.find((win) => {
|
|
|
|
return (win && win.type === 'popup' &&
|
|
|
|
win.height === height &&
|
|
|
|
win.width === width)
|
|
|
|
}) : null
|
2016-09-01 20:22:29 +02:00
|
|
|
}
|
|
|
|
|
2016-11-11 19:26:12 +01:00
|
|
|
function closePopup () {
|
2016-09-02 22:29:47 +02:00
|
|
|
getPopup((err, popup) => {
|
|
|
|
if (err) throw err
|
2016-08-24 00:44:50 +02:00
|
|
|
if (!popup) return
|
|
|
|
extension.windows.remove(popup.id, console.error)
|
|
|
|
})
|
|
|
|
}
|