Changeset View
Changeset View
Standalone View
Standalone View
web/cashtab/extension/src/background.js
const extension = require('extensionizer'); | const extension = require('extensionizer'); | ||||
const NOTIFICATION_HEIGHT = 600; | const NOTIFICATION_HEIGHT = 600; | ||||
const NOTIFICATION_WIDTH = 400; | const NOTIFICATION_WIDTH = 400; | ||||
// This starts listening to the port created with `extension.runtime.connect` in contentscript.js | // This starts listening to the port created with `extension.runtime.connect` in contentscript.js | ||||
extension.runtime.onConnect.addListener(function (port) { | extension.runtime.onConnect.addListener(function (port) { | ||||
console.assert(port.name == 'cashtabPort'); | console.assert(port.name == 'cashtabPort'); | ||||
port.onMessage.addListener(function (msg) { | port.onMessage.addListener(function (msg) { | ||||
// Handle a transaction creation request | // Handle a transaction creation request | ||||
if (msg.text == `Cashtab` && msg.txInfo) { | if (msg.text == `Cashtab` && msg.txInfo) { | ||||
console.log( | console.log( | ||||
`Received a transaction request, opening Cashtab extension`, | `Received a transaction request, opening Cashtab extension`, | ||||
); | ); | ||||
triggerUi(msg.txInfo); | openSendXec(msg.txInfo); | ||||
} | } | ||||
// Handle an address sharing request | // Handle an address sharing request | ||||
if (msg.text === `Cashtab` && msg.addressRequest) { | if (msg.text === `Cashtab` && msg.addressRequest) { | ||||
console.log(`Received request for ecash address`); | console.log(`Received request for ecash address`); | ||||
// get the tab this message came from | // get the tab this message came from | ||||
// Note that chrome extension does not support making this listener async | // Note that chrome extension does not support making this listener async | ||||
// so need to use this Promise.then() syntax | // so need to use this Promise.then() syntax | ||||
getCurrentActiveTab().then( | getCurrentActiveTab().then( | ||||
▲ Show 20 Lines • Show All 92 Lines • ▼ Show 20 Lines | await openWindow({ | ||||
type: 'popup', | type: 'popup', | ||||
width: NOTIFICATION_WIDTH, | width: NOTIFICATION_WIDTH, | ||||
height: NOTIFICATION_HEIGHT, | height: NOTIFICATION_HEIGHT, | ||||
left, | left, | ||||
top, | top, | ||||
}); | }); | ||||
} | } | ||||
/** | // Open Cashtab extension with transaction information in the query string | ||||
* Opens the browser popup for user confirmation | async function openSendXec(txInfo) { | ||||
*/ | |||||
/* | |||||
Breaking this function down | |||||
1) Get all active tabs in browser | |||||
2) Determine if the extension UI is currently open | |||||
3) If extension is not open AND no other UI triggered popups are open, then open one | |||||
Eventually will need similar model. Note that it actually goes much deeper than this in MetaMask. | |||||
To start, just open a popup | |||||
*/ | |||||
async function triggerUi(txInfo) { | |||||
// Open a pop-up | |||||
let left = 0; | let left = 0; | ||||
let top = 0; | let top = 0; | ||||
try { | try { | ||||
const lastFocused = await getLastFocusedWindow(); | const lastFocused = await getLastFocusedWindow(); | ||||
// Position window in top right corner of lastFocused window. | // Position window in top right corner of lastFocused window. | ||||
top = lastFocused.top; | top = lastFocused.top; | ||||
left = lastFocused.left + (lastFocused.width - NOTIFICATION_WIDTH); | left = lastFocused.left + (lastFocused.width - NOTIFICATION_WIDTH); | ||||
} catch (_) { | } catch (_) { | ||||
// The following properties are more than likely 0, due to being | // The following properties are more than likely 0, due to being | ||||
// opened from the background chrome process for the extension that | // opened from the background chrome process for the extension that | ||||
// has no physical dimensions | // has no physical dimensions | ||||
const { screenX, screenY, outerWidth } = window; | const { screenX, screenY, outerWidth } = window; | ||||
top = Math.max(screenY, 0); | top = Math.max(screenY, 0); | ||||
left = Math.max(screenX + (outerWidth - NOTIFICATION_WIDTH), 0); | left = Math.max(screenX + (outerWidth - NOTIFICATION_WIDTH), 0); | ||||
} | } | ||||
console.log(`txInfo`); | |||||
console.log(txInfo); | |||||
const queryString = Object.keys(txInfo) | const queryString = Object.keys(txInfo) | ||||
.map(key => key + '=' + txInfo[key]) | .map(key => key + '=' + txInfo[key]) | ||||
.join('&'); | .join('&'); | ||||
// create new notification popup | // create new notification popup | ||||
await openWindow({ | await openWindow({ | ||||
url: `index.html#/send?${queryString}`, | url: `index.html#/send?${queryString}`, | ||||
type: 'popup', | type: 'popup', | ||||
▲ Show 20 Lines • Show All 43 Lines • Show Last 20 Lines |