Changeset View
Changeset View
Standalone View
Standalone View
web/cashtab/extension/src/background.js
const extension = require('extensionizer'); | |||||
const NOTIFICATION_HEIGHT = 600; | const NOTIFICATION_HEIGHT = 600; | ||||
const NOTIFICATION_WIDTH = 400; | const NOTIFICATION_WIDTH = 400; | ||||
let popupIsOpen = false; | let popupIsOpen = false; | ||||
let notificationIsOpen = false; | let notificationIsOpen = false; | ||||
const openMetamaskTabsIDs = {}; | const openMetamaskTabsIDs = {}; | ||||
const requestAccountTabIds = {}; | const requestAccountTabIds = {}; | ||||
// This starts listening to the port created with `chrome.runtime.connect` in contentscript.js | // This starts listening to the port created with `extension.runtime.connect` in contentscript.js | ||||
chrome.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) { | ||||
console.log('msg received in background.js'); | console.log('msg received in background.js'); | ||||
console.log(msg.text); | console.log(msg.text); | ||||
if (msg.text == `CashTab` && msg.txInfo) { | if (msg.text == `CashTab` && msg.txInfo) { | ||||
console.log(`Caught, opening popup`); | console.log(`Caught, opening popup`); | ||||
triggerUi(msg.txInfo); | triggerUi(msg.txInfo); | ||||
} | } | ||||
Show All 10 Lines | |||||
3) If extension is not open AND no other UI triggered popups are open, then open one | 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. | Eventually will need similar model. Note that it actually goes much deeper than this in MetaMask. | ||||
To start, just open a popup | To start, just open a popup | ||||
*/ | */ | ||||
async function triggerUi(txInfo) { | async function triggerUi(txInfo) { | ||||
/* | /* | ||||
const tabs = await chrome.getActiveTabs(); | const tabs = await extension.getActiveTabs(); | ||||
const currentlyActiveCashtabTab = Boolean(tabs.find(tab => openMetamaskTabsIDs[tab.id])); | const currentlyActiveCashtabTab = Boolean(tabs.find(tab => openMetamaskTabsIDs[tab.id])); | ||||
if (!popupIsOpen && !currentlyActiveCashtabTab) { | if (!popupIsOpen && !currentlyActiveCashtabTab) { | ||||
await notificationManager.showPopup(); | await notificationManager.showPopup(); | ||||
} | } | ||||
*/ | */ | ||||
// Open a pop-up | // Open a pop-up | ||||
let left = 0; | let left = 0; | ||||
let top = 0; | let top = 0; | ||||
Show All 26 Lines | const popupWindow = await openWindow({ | ||||
height: NOTIFICATION_HEIGHT, | height: NOTIFICATION_HEIGHT, | ||||
left, | left, | ||||
top, | top, | ||||
}); | }); | ||||
} | } | ||||
async function openWindow(options) { | async function openWindow(options) { | ||||
return new Promise((resolve, reject) => { | return new Promise((resolve, reject) => { | ||||
chrome.windows.create(options, newWindow => { | extension.windows.create(options, newWindow => { | ||||
const error = checkForError(); | const error = checkForError(); | ||||
if (error) { | if (error) { | ||||
return reject(error); | return reject(error); | ||||
} | } | ||||
return resolve(newWindow); | return resolve(newWindow); | ||||
}); | }); | ||||
}); | }); | ||||
} | } | ||||
function checkForError() { | function checkForError() { | ||||
const { lastError } = chrome.runtime; | const { lastError } = extension.runtime; | ||||
if (!lastError) { | if (!lastError) { | ||||
return undefined; | return undefined; | ||||
} | } | ||||
// if it quacks like an Error, its an Error | // if it quacks like an Error, its an Error | ||||
if (lastError.stack && lastError.message) { | if (lastError.stack && lastError.message) { | ||||
return lastError; | return lastError; | ||||
} | } | ||||
// repair incomplete error object (eg chromium v77) | // repair incomplete error object (eg chromium v77) | ||||
return new Error(lastError.message); | return new Error(lastError.message); | ||||
} | } | ||||
async function getLastFocusedWindow() { | async function getLastFocusedWindow() { | ||||
return new Promise((resolve, reject) => { | return new Promise((resolve, reject) => { | ||||
chrome.windows.getLastFocused(windowObject => { | extension.windows.getLastFocused(windowObject => { | ||||
const error = checkForError(); | const error = checkForError(); | ||||
if (error) { | if (error) { | ||||
return reject(error); | return reject(error); | ||||
} | } | ||||
return resolve(windowObject); | return resolve(windowObject); | ||||
}); | }); | ||||
}); | }); | ||||
} | } |