OSDN Git Service

update vote confirm page.
[bytom/Byone.git] / src / inject.js
1 import IdGenerator from '@/utils/IdGenerator'
2 import { EncryptedStream } from 'extension-streams'
3 import * as MsgTypes from './messages/types'
4 import * as EventNames from '@/messages/event'
5 import Bytomdapp from './dapp'
6
7 /***
8  * This is the javascript which gets injected into
9  * the application and facilitates communication between
10  * bytom chrome extension and the web application.
11  */
12 class Inject {
13   constructor() {
14     // Injecting an encrypted stream into the web application.
15     const stream = new EncryptedStream(EventNames.INJECT, IdGenerator.text(64))
16
17     // Waiting for bytomExtension to push itself onto the application
18     stream.listenWith(msg => {
19       console.log('inject.stream.listen:', msg)
20       if (
21         msg &&
22         msg.hasOwnProperty('type') &&
23         msg.type === MsgTypes.PUSH_BYTOM
24       ) {
25         window.bytom = new Bytomdapp(stream, msg.payload)
26       }
27
28       if (
29         msg &&
30         msg.hasOwnProperty('type') &&
31         msg.type === MsgTypes.UPDATE_BYTOM
32       ) {
33         window.bytom[msg.payload.type] = msg.payload.value
34       }
35     })
36
37     // Syncing the streams between the extension and the web application
38     stream.sync(EventNames.BYTOM, stream.key)
39   }
40 }
41
42 new Inject()