1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-25 03:20:23 +01:00
metamask-extension/app/vendor/trezor/usb-permissions.js

52 lines
1.5 KiB
JavaScript
Raw Normal View History

2018-08-05 08:43:02 +02:00
/*
Handling messages from usb permissions iframe
*/
2018-08-10 17:44:32 +02:00
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;
}
// TODO: remove this query, or add `tabs` permission. This does not work.
2018-08-10 17:44:32 +02:00
// 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 });
});
}
2018-08-05 08:43:02 +02:00
window.addEventListener('message', event => {
if (event.data === 'usb-permissions-init') {
const iframe = document.getElementById('trezor-usb-permissions');
iframe.contentWindow.postMessage({
type: 'usb-permissions-init',
extension: chrome.runtime.id,
}, '*');
} else if (event.data === 'usb-permissions-close') {
2018-08-10 17:44:32 +02:00
switchToPopupTab();
2018-08-05 08:43:02 +02:00
}
2018-08-05 09:17:51 +02:00
});
2018-08-10 17:44:32 +02:00
window.addEventListener('beforeunload', switchToPopupTab);