diff options
migrate to ProviderEngine zero-client
Diffstat (limited to 'app/scripts/contentscript.js')
-rw-r--r-- | app/scripts/contentscript.js | 28 |
1 files changed, 13 insertions, 15 deletions
diff --git a/app/scripts/contentscript.js b/app/scripts/contentscript.js index 7a180d9a9..cce5c1274 100644 --- a/app/scripts/contentscript.js +++ b/app/scripts/contentscript.js @@ -1,6 +1,7 @@ -const allowedMessageTarget = 'metamask' -const allowedMessageType = 'addUnsignedTx' +const LocalMessageDuplexStream = require('./lib/local-message-stream.js') +const PortStream = require('./lib/port-stream.js') +console.log('content script!') // inject in-page script var scriptTag = document.createElement('script') @@ -9,17 +10,14 @@ scriptTag.onload = function() { this.parentNode.removeChild(this) } var container = document.head || document.documentElement container.appendChild(scriptTag) -// setup connection with background -var metamaskPlugin = chrome.runtime.connect({name: 'metamask'}) +// setup communication to page and plugin +var pageStream = new LocalMessageDuplexStream({ + name: 'contentscript', + target: 'inpage', +}) +var pluginPort = chrome.runtime.connect({name: 'metamask'}) +var pluginStream = new PortStream(pluginPort) -// forward messages from inpage to background -window.addEventListener('message', receiveMessage, false); -function receiveMessage(event){ - var msg = event.data - // validate message type - if (typeof msg !== 'object') return - if (msg.to !== allowedMessageTarget) return - if (msg.type !== allowedMessageType) return - // forward message - metamaskPlugin.postMessage(msg) -}
\ No newline at end of file +// forward communication across +pageStream.pipe(pluginStream) +pluginStream.pipe(pageStream)
\ No newline at end of file |