From 78a1cd3314455d6e4e08839a3eea3ec2868f4f59 Mon Sep 17 00:00:00 2001 From: brunobar79 Date: Sat, 11 Aug 2018 02:35:20 -0400 Subject: iframe communication working --- app/scripts/lib/setupLedgerIframe.js | 40 ++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 app/scripts/lib/setupLedgerIframe.js (limited to 'app/scripts/lib/setupLedgerIframe.js') diff --git a/app/scripts/lib/setupLedgerIframe.js b/app/scripts/lib/setupLedgerIframe.js new file mode 100644 index 000000000..2831d072e --- /dev/null +++ b/app/scripts/lib/setupLedgerIframe.js @@ -0,0 +1,40 @@ +const extension = require('extensionizer') +module.exports = setupLedgerIframe +/** + * Injects an iframe into the current document to + * enable the interaction with ledger devices + */ +function setupLedgerIframe () { + const ORIGIN = 'http://localhost:9000' + const ledgerIframe = document.createElement('iframe') + ledgerIframe.src = ORIGIN + console.log('Injecting ledger iframe') + document.head.appendChild(ledgerIframe) + + console.log('[LEDGER]: LEDGER BG LISTENER READY') + extension.runtime.onMessage.addListener(({action, params}) => { + console.log('[LEDGER]: GOT MSG FROM THE KEYRING', action, params) + if (action.search('ledger-') !== -1) { + //Forward messages from the keyring to the iframe + sendMessage({action, params}) + } + }) + + function sendMessage(msg) { + ledgerIframe.contentWindow.postMessage({...msg, target: 'LEDGER-IFRAME'}, '*') + } + + /* + Passing messages from iframe to background script + */ + console.log('[LEDGER]: LEDGER FROM-IFRAME LISTENER READY') + window.addEventListener('message', event => { + if(event.origin !== ORIGIN) return false + if (event.data && event.data.action && event.data.action.search('ledger-') !== -1) { + // Forward messages from the iframe to the keyring + console.log('[LEDGER] : forwarding msg', event.data) + extension.runtime.sendMessage(event.data) + } + }) + + } \ No newline at end of file -- cgit v1.2.3