parent
29a602a89b
commit
8d49d519c0
@ -1,27 +0,0 @@ |
|||||||
|
|
||||||
/* |
|
||||||
IFRAME |
|
||||||
var pageStream = new LocalMessageDuplexStream({ |
|
||||||
name: 'contentscript', |
|
||||||
target: 'inpage', |
|
||||||
}) |
|
||||||
SERVICEWORKER |
|
||||||
pageStream.on('error', console.error) |
|
||||||
var pluginPort = extension.runtime.connect({name: 'contentscript'}) |
|
||||||
var pluginStream = new PortStream(pluginPort) |
|
||||||
pluginStream.on('error', console.error) |
|
||||||
IFRAME --> SW |
|
||||||
// forward communication plugin->inpage
|
|
||||||
pageStream.pipe(pluginStream).pipe(pageStream) |
|
||||||
*/ |
|
||||||
|
|
||||||
module.exports = SetupUntrustedComunicationWithSW |
|
||||||
|
|
||||||
function SetupUntrustedComunicationWithSW (connectionStream, readySwStream) { |
|
||||||
pageStream.on('error', console.error) |
|
||||||
var pluginPort = extension.runtime.connect({name: 'contentscript'}) |
|
||||||
var pluginStream = new PortStream(pluginPort) |
|
||||||
pluginStream.on('error', console.error) |
|
||||||
// forward communication plugin->inpage
|
|
||||||
pageStream.pipe(pluginStream).pipe(pageStream) |
|
||||||
} |
|
@ -1,14 +1,14 @@ |
|||||||
const ParentStream = require('iframe-stream').ParentStream |
const ParentStream = require('iframe-stream').ParentStream |
||||||
const SWcontroller = require('./sw-controller') |
const SWcontroller = require('client-sw-ready-event/lib/sw-client.js') |
||||||
const SwStream = require('sw-stream/lib/sw-stream.js') |
const SwStream = require('sw-stream/lib/sw-stream.js') |
||||||
const SetupUntrustedComunication = ('./lib/setup-untrusted-connection.js') |
const SetupUntrustedComunication = ('./lib/setup-untrusted-connection.js') |
||||||
|
|
||||||
const background = new SWcontroller({ |
const background = new SWcontroller({ |
||||||
fileName: '/popup/sw-build.js', |
fileName: '/popup/sw-build.js', |
||||||
}) |
}) |
||||||
|
|
||||||
const pageStream = new ParentStream() |
const pageStream = new ParentStream() |
||||||
background.on('ready', (_) => { |
background.on('ready', (_) => { |
||||||
// var inpageProvider = new MetamaskInpageProvider(SwStream(background.controller))
|
|
||||||
let swStream = SwStream({ |
let swStream = SwStream({ |
||||||
serviceWorker: background.controller, |
serviceWorker: background.controller, |
||||||
context: 'dapp', |
context: 'dapp', |
@ -1,5 +1,5 @@ |
|||||||
const setupIframe = require('./setup-iframe.js') |
const setupIframe = require('./setup-iframe.js') |
||||||
const MetamaskInpageProvider = require('../../app/scripts/lib/inpage-provider.js') |
const MetamaskInpageProvider = require('../../../app/scripts/lib/inpage-provider.js') |
||||||
|
|
||||||
module.exports = getProvider |
module.exports = getProvider |
||||||
|
|
@ -1,10 +1,10 @@ |
|||||||
const injectCss = require('inject-css') |
const injectCss = require('inject-css') |
||||||
const MetaMaskUiCss = require('../ui/css') |
const MetaMaskUiCss = require('../../ui/css') |
||||||
const setupIframe = require('./lib/setup-iframe.js') |
const setupIframe = require('./lib/setup-iframe.js') |
||||||
const MetamaskInpageProvider = require('../app/scripts/lib/inpage-provider.js') |
const MetamaskInpageProvider = require('../../app/scripts/lib/inpage-provider.js') |
||||||
const SWcontroller = require('./sw-controller') |
const SWcontroller = require('client-sw-ready-event/lib/sw-client.js') |
||||||
const SwStream = require('sw-stream/lib/sw-stream.js') |
const SwStream = require('sw-stream/lib/sw-stream.js') |
||||||
const startPopup = require('../app/scripts/popup-core') |
const startPopup = require('../../app/scripts/popup-core') |
||||||
|
|
||||||
|
|
||||||
var css = MetaMaskUiCss() |
var css = MetaMaskUiCss() |
@ -1,64 +0,0 @@ |
|||||||
const EventEmitter = require('events') |
|
||||||
|
|
||||||
module.exports = class ClientSideServiceWorker extends EventEmitter{ |
|
||||||
constructor (opts) { |
|
||||||
super() |
|
||||||
this.fileName = opts.fileName |
|
||||||
this.startDelay = opts.startDelay |
|
||||||
|
|
||||||
this.serviceWorkerApi = navigator.serviceWorker |
|
||||||
this.serviceWorkerApi.onmessage = (messageEvent) => this.emit('message', messageEvent) |
|
||||||
this.serviceWorkerApi.onerror = (err) => this.emit('error', err) |
|
||||||
this.on('message', (messageEvent) => {debugger}) |
|
||||||
if (opts.initStart) this.startWorker() |
|
||||||
} |
|
||||||
|
|
||||||
get controller () { |
|
||||||
return this.sw || this.serviceWorkerApi.controller |
|
||||||
} |
|
||||||
|
|
||||||
|
|
||||||
startWorker () { |
|
||||||
return this.registerWorker() |
|
||||||
.then((sw) => { |
|
||||||
this.sw = sw |
|
||||||
this.sw.onerror = (err) => this.emit('error', err) |
|
||||||
this.sw = sw |
|
||||||
this.emit('ready', this.sw) |
|
||||||
}) |
|
||||||
.catch((err) => this.emit('error', err)) |
|
||||||
} |
|
||||||
|
|
||||||
registerWorker () { |
|
||||||
return this.serviceWorkerApi.register(this.fileName) |
|
||||||
.then((registerdWorker) => { |
|
||||||
return new Promise((resolve, reject) => { |
|
||||||
let timeOutId = setTimeout(() => { |
|
||||||
if (this.serviceWorkerApi.controller) return resolve(this.serviceWorkerApi.controller) |
|
||||||
if (registerdWorker.active) return resolve(registerdWorker.active) |
|
||||||
return reject(new Error('ClientSideServiceWorker: No controller found and onupdatefound timed out')) |
|
||||||
}, this.startDelay || 1000 ) |
|
||||||
|
|
||||||
registerdWorker.onupdatefound = (event) => { |
|
||||||
this.emit('updatefound') |
|
||||||
registerdWorker.update() |
|
||||||
} |
|
||||||
}) |
|
||||||
}) |
|
||||||
} |
|
||||||
|
|
||||||
sendMessage (message) { |
|
||||||
const self = this |
|
||||||
return new Promise((resolve, reject) => { |
|
||||||
var messageChannel = new MessageChannel() |
|
||||||
messageChannel.port1.onmessage = (event) => { |
|
||||||
if (event.data.err) { |
|
||||||
reject(event.data.error) |
|
||||||
} else { |
|
||||||
resolve(event.data.data) |
|
||||||
} |
|
||||||
} |
|
||||||
this.controller.postMessage(message, [messageChannel.port2]) |
|
||||||
}) |
|
||||||
} |
|
||||||
} |
|
Loading…
Reference in new issue