nifty-wallet/app/scripts/contentscript.js

27 lines
933 B
JavaScript
Raw Normal View History

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