1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-27 12:56:01 +01:00
metamask-extension/app/scripts/contentscript.js

26 lines
884 B
JavaScript
Raw Normal View History

2015-12-19 07:05:16 +01:00
const LocalMessageDuplexStream = require('./lib/local-message-stream.js')
const PortStream = require('./lib/port-stream.js')
2015-08-02 01:33:31 +02:00
// inject in-page script
2015-08-01 03:38:02 +02:00
var scriptTag = document.createElement('script')
scriptTag.src = chrome.extension.getURL('scripts/inpage.js')
scriptTag.onload = function() { this.parentNode.removeChild(this) }
2015-08-02 01:33:31 +02:00
var container = document.head || document.documentElement
container.appendChild(scriptTag)
2015-12-19 07:05:16 +01:00
// setup communication to page and plugin
var pageStream = new LocalMessageDuplexStream({
name: 'contentscript',
target: 'inpage',
})
2016-01-15 11:03:42 +01:00
var pluginPort = chrome.runtime.connect({name: 'contentscript'})
2015-12-19 07:05:16 +01:00
var pluginStream = new PortStream(pluginPort)
2015-12-19 07:05:16 +01:00
// forward communication across
pageStream.pipe(pluginStream)
2016-02-10 20:46:13 +01:00
pluginStream.pipe(pageStream)
// log errors
pageStream.on('error', console.error.bind(console))
pluginStream.on('error', console.error.bind(console))