diff options
Diffstat (limited to 'app')
-rw-r--r-- | app/manifest.json | 8 | ||||
-rw-r--r-- | app/scripts/background.js | 8 | ||||
-rw-r--r-- | app/trezor-usb-permissions.html | 33 | ||||
-rw-r--r-- | app/vendor/trezor/content-script.js | 21 | ||||
-rw-r--r-- | app/vendor/trezor/usb-permissions.js | 50 |
5 files changed, 120 insertions, 0 deletions
diff --git a/app/manifest.json b/app/manifest.json index 8bf51cfd7..a95688789 100644 --- a/app/manifest.json +++ b/app/manifest.json @@ -52,6 +52,14 @@ ], "run_at": "document_start", "all_frames": true + }, + { + "matches": [ + "*://connect.trezor.io/5/popup.html" + ], + "js": [ + "vendor/trezor/content-script.js" + ] } ], "permissions": [ diff --git a/app/scripts/background.js b/app/scripts/background.js index a6fc5ed78..d577ead41 100644 --- a/app/scripts/background.js +++ b/app/scripts/background.js @@ -332,6 +332,10 @@ function setupController (initState, initLangCode) { [ENVIRONMENT_TYPE_FULLSCREEN]: true, } + const metamaskBlacklistedPorts = [ + 'trezor-connect', + ] + const isClientOpenStatus = () => { return popupIsOpen || Boolean(Object.keys(openMetamaskTabsIDs).length) || notificationIsOpen } @@ -352,6 +356,10 @@ function setupController (initState, initLangCode) { const processName = remotePort.name const isMetaMaskInternalProcess = metamaskInternalProcessHash[processName] + if (metamaskBlacklistedPorts.includes(remotePort.name)) { + return false + } + if (isMetaMaskInternalProcess) { const portStream = new PortStream(remotePort) // communication with popup diff --git a/app/trezor-usb-permissions.html b/app/trezor-usb-permissions.html new file mode 100644 index 000000000..16f28e5e1 --- /dev/null +++ b/app/trezor-usb-permissions.html @@ -0,0 +1,33 @@ +<!DOCTYPE html> +<html lang="en"> +<head> + <meta charset="UTF-8" /> + <meta http-equiv="X-UA-Compatible" content="IE=Edge" /> + <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0" /> + <title>TrezorConnect | Trezor</title> + <meta name="description" content="" /> + <meta name="keywords" content="" /> + <meta name="author" content="Trezor info@trezor.io" /> + <meta name="robots" content="noindex, nofollow" /> + <meta name="title" content="Trezor Connect" /> + <meta name="theme-color" content="#ffffff" /> + <meta http-equiv="Pragma" content="no-cache" /> + <meta http-equiv="Expires" content="-1" /> + <style> + * { + margin: 0; + padding: 0; + } + html, body { + position: relative; + width: 100%; + height: 100%; + min-height: 500px; + min-width: 328px; + } + </style> +</head> +<body> + <iframe id="trezor-usb-permissions" src="https://connect.trezor.io/5/extension-permissions.html" allow="usb" frameborder="0" width="100%" height="100%"></iframe> + <script type="text/javascript" src="./vendor/trezor/usb-permissions.js"></script> +</body>
\ No newline at end of file diff --git a/app/vendor/trezor/content-script.js b/app/vendor/trezor/content-script.js new file mode 100644 index 000000000..a21332f46 --- /dev/null +++ b/app/vendor/trezor/content-script.js @@ -0,0 +1,21 @@ +/* +Passing messages from background script to popup +*/ + +let port = chrome.runtime.connect({ name: 'trezor-connect' }); +port.onMessage.addListener(message => { + window.postMessage(message, window.location.origin); +}); +port.onDisconnect.addListener(d => { + port = null; +}); + +/* +Passing messages from popup to background script +*/ + +window.addEventListener('message', event => { + if (port && event.source === window && event.data) { + port.postMessage(event.data); + } +}); diff --git a/app/vendor/trezor/usb-permissions.js b/app/vendor/trezor/usb-permissions.js new file mode 100644 index 000000000..9de47e0a1 --- /dev/null +++ b/app/vendor/trezor/usb-permissions.js @@ -0,0 +1,50 @@ +/* +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'); + iframe.contentWindow.postMessage({ + type: 'usb-permissions-init', + extension: chrome.runtime.id, + }, '*'); + } else if (event.data === 'usb-permissions-close') { + switchToPopupTab(); + } +}); + +window.addEventListener('beforeunload', switchToPopupTab);
\ No newline at end of file |