OSDN Git Service

add the interaction functionality between the chrome-extension and Dapp.
authorZhiting Lin <zlin035@uottawa.ca>
Sun, 3 Feb 2019 02:44:16 +0000 (10:44 +0800)
committerZhiting Lin <zlin035@uottawa.ca>
Sun, 3 Feb 2019 02:44:16 +0000 (10:44 +0800)
src/background.js
src/content.js
src/messages/types.js
src/views/advancedTransfer.vue

index 9f99837..337f2c1 100644 (file)
@@ -2,13 +2,28 @@ import { LocalStream } from 'extension-streams'
 import InternalMessage from '@/messages/internal'
 import * as MsgTypes from './messages/types'
 
+import accountAction from "@/models/account";
+import bytom from "@/models/bytom";
+
 export default class Background {
   constructor() {
     this.setupInternalMessaging()
+    this.setupBytom()
+  }
+
+  setupBytom(){
+    const network = localStorage.bytomNet||'mainnet'
+    bytom.setupNet(network)
+
+    window.addEventListener('storage', storageEventHandler, false);
+    function storageEventHandler(evt){
+      if(evt.key === 'bytomNet'){
+        bytom.setupNet( evt.newValue )
+      }
+    }
   }
 
   setupInternalMessaging() {
-    console.log('messaging')
     LocalStream.watch((request, sendResponse) => {
       console.log(request)
       const message = InternalMessage.fromJson(request)
@@ -24,6 +39,9 @@ export default class Background {
       case MsgTypes.ADVTRANSFER:
         this.advancedTransfer(sendResponse, message.payload)
         break
+      case MsgTypes.SEND:
+        this.send(sendResponse, message.payload)
+        break
     }
   }
 
@@ -59,11 +77,47 @@ export default class Background {
         top: 0,
         left: 0
       },
-      () => {
-        sendResponse(true)
+      (window) => {
+        chrome.runtime.onMessage.addListener(function(request, sender) {
+          if(sender.tab.windowId === window.id){
+            switch (request.method){
+              case 'advanced-transfer':
+                sendResponse(request);
+                break
+            }
+          }
+        });
       }
     )
   }
+
+  send(sendResponse, payload) {
+    const action = payload.action
+    const body = payload.body
+    if(action){
+      let promise
+      switch (action){
+        case 'listAllAccount':
+          promise = accountAction.list()
+          break
+        case 'currentAccount':
+          let account = JSON.parse(localStorage.currentAccount)
+          sendResponse(account)
+          break
+        case 'balance':
+          const id = body.id
+          const guid = body.guid
+          promise = accountAction.balance(guid, id)
+          break
+      }
+      if(promise){
+        promise.then(resp =>
+        {
+          sendResponse(resp)
+        })
+      }
+    }
+  }
 }
 
 new Background()
index b2b5634..62ffe61 100644 (file)
@@ -59,6 +59,7 @@ class Content {
         break
       case MsgTypes.TRANSFER:
       case MsgTypes.ADVTRANSFER:
+      case MsgTypes.SEND:
         this.transfer(msg.type, networkMessage)
         break
       default:
index afa22c9..30ccd3f 100644 (file)
@@ -4,3 +4,4 @@ export const PUSH_BYTOM = 'pushBytom'
 export const AUTHENTICATE = 'authenticate'
 export const TRANSFER = 'transfer'
 export const ADVTRANSFER = 'advTransfer'
+export const SEND = 'send'
index a94545a..5eab6a7 100644 (file)
@@ -60,7 +60,8 @@
                 </div>
             </div>
             <br>
-            <div style="width: 200px; margin: 0 auto;">
+            <div class="btn-inline" style="width: 200px;">
+              <div class="btn bg-gray" @click="close">{{ $t('transfer.cancel') }}</div>
               <div class="btn bg-green" @click="$refs.modalPasswd.open()">{{ $t('transfer.confirm') }}</div>
             </div>
         </section>
@@ -75,6 +76,7 @@ import account from "@/models/account";
 import transaction from "@/models/transaction";
 import getLang from "@/assets/language/sdk";
 import modalPasswd from "@/components/modal-passwd";
+import { LocalStream } from 'extension-streams'
 
 export default {
     components: {
@@ -124,7 +126,8 @@ export default {
     },
     methods: {
         close: function () {
-            this.$router.go(-1)
+            LocalStream.send({method:'advanced-transfer',action:'reject'});
+            window.close();
         },
         send: function (passwd) {
             let loader = this.$loading.show({
@@ -135,43 +138,47 @@ export default {
             });
             const inout = JSON.parse(this.$route.query.object)
 
-            transaction.buildTransaction(this.account.guid,  inout.input, inout.output).then(ret => {
+            transaction.buildTransaction(this.account.guid,  inout.input, inout.output, inout.gas).then(ret => {
                 const arrayData = ret.result.data.signing_instructions[0].data
                 return transaction.advancedTransfer(this.account.guid, ret.result.data, passwd, arrayData)
-                  .then(() => {
+                  .then((resp) => {
                     loader.hide();
+                    LocalStream.send({method:'advanced-transfer',action:'success', message:resp});
                     this.$dialog.show({
                       body: this.$t("transfer.success")
                     });
-                    this.$router.push('/')
+                    this.$router.push('/');
                   })
                   .catch(error => {
                     throw error
                   });
             }).catch(error => {
                 loader.hide();
+
                 this.$dialog.show({
                     body: getLang(error.message)
                 });
             });
         }
     }, mounted() {
-        if (this.$route.params.account != undefined) {
-            this.account = this.$route.params.account;
-        }
+      const inout = JSON.parse(this.$route.query.object);
 
-        account.setupNet(localStorage.bytomNet);
-        account.list().then(accounts => {
-            this.accounts = accounts;
-            this.accounts.forEach(function (ele) {
-                ele.label = ele.alias
-                ele.value = ele.guid
-            });
+      if (inout.account != undefined) {
+              this.account = inout.account;
+          }
 
-            if (Object.keys(this.account).length == 0) {
-                this.account = accounts[0]
-            }
-        });
-    }
+          account.setupNet(localStorage.bytomNet);
+          account.list().then(accounts => {
+              this.accounts = accounts;
+              this.accounts.forEach(function (ele) {
+                  ele.label = ele.alias
+                  ele.value = ele.guid
+              });
+
+              if (Object.keys(this.account).length == 0) {
+                  this.account = accounts[0]
+              }
+          });
+      }
 };
 </script>