diff options
Diffstat (limited to 'app/vendor/trezor/usb-permissions.js')
-rw-r--r-- | app/vendor/trezor/usb-permissions.js | 42 |
1 files changed, 36 insertions, 6 deletions
diff --git a/app/vendor/trezor/usb-permissions.js b/app/vendor/trezor/usb-permissions.js index 9039c0094..9de47e0a1 100644 --- a/app/vendor/trezor/usb-permissions.js +++ b/app/vendor/trezor/usb-permissions.js @@ -2,6 +2,39 @@ Handling messages from usb permissions iframe */ +const switchToPopupTab = (event) => { + + window.removeEventListener('beforeunload', switchToPopupTab); + + if (!event) { + // triggered from 'usb-permissions-close' message + // switch tab to previous index and close current + chrome.tabs.query({ + currentWindow: true, + active: true, + }, (current) => { + if (current.length < 0) return; + chrome.tabs.query({ + index: current[0].index - 1 + }, popup => { + if (popup.length < 0) return; + chrome.tabs.update(popup[0].id, { active: true }); + }) + chrome.tabs.remove(current[0].id); + }); + return; + } + + // triggered from 'beforeunload' event + // find tab by popup pattern and switch to it + chrome.tabs.query({ + url: "*://connect.trezor.io/*/popup.html" + }, (tabs) => { + if (tabs.length < 0) return; + chrome.tabs.update(tabs[0].id, { active: true }); + }); +} + window.addEventListener('message', event => { if (event.data === 'usb-permissions-init') { const iframe = document.getElementById('trezor-usb-permissions'); @@ -10,11 +43,8 @@ window.addEventListener('message', event => { extension: chrome.runtime.id, }, '*'); } else if (event.data === 'usb-permissions-close') { - chrome.tabs.query({ - currentWindow: true, - active: true, - }, (tabs) => { - chrome.tabs.remove(tabs[0].id); - }); + switchToPopupTab(); } }); + +window.addEventListener('beforeunload', switchToPopupTab);
\ No newline at end of file |