OSDN Git Service

update the account and netwrok change detection.
authorZhiting Lin <zlin035@uottawa.ca>
Thu, 9 May 2019 06:05:49 +0000 (14:05 +0800)
committerZhiting Lin <zlin035@uottawa.ca>
Thu, 9 May 2019 06:05:49 +0000 (14:05 +0800)
src/background.js
src/content.js
src/inject.js
src/messages/types.js

index c685b58..54ebd8e 100644 (file)
@@ -20,6 +20,13 @@ export default class Background {
     function storageEventHandler(evt){
       if(evt.key === 'bytomNet'){
         bytom.setupNet( evt.newValue )
+        chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
+          chrome.tabs.sendMessage(tabs[0].id, {action: "updateNetAndAccounts"}, function(response) {});
+        });
+      }else if(evt.key === 'currentAccount'){
+        chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
+          chrome.tabs.sendMessage(tabs[0].id, {action: "updateAccount"}, function(response) {});
+        });
       }
     }
   }
index ccae4f4..d1bc101 100644 (file)
@@ -44,8 +44,36 @@ class Content {
 
       document.dispatchEvent(new CustomEvent('chromeBytomLoaded'))
     })
+
+    chrome.runtime.onMessage.addListener(async (obj) => {
+      switch (obj.action) {
+        case 'updateAccount':
+          const defaultAccount = await this.getDefaultAccount();
+          if(defaultAccount){
+            stream.send(
+              NetworkMessage.payload(MsgTypes.UPDATE_BYTOM, {type:'default_account', value: defaultAccount}),
+              EventNames.INJECT
+            )
+          }
+          break
+        case 'updateNetAndAccounts':
+          const net = await this.getDefaultNetwork();
+          const accountList = await this.getAccountList();
+          stream.send(
+            NetworkMessage.payload(MsgTypes.UPDATE_BYTOM, {type:'net', value: net}),
+            EventNames.INJECT
+          )
+          stream.send(
+            NetworkMessage.payload(MsgTypes.UPDATE_BYTOM, {type:'accounts', value: accountList}),
+            EventNames.INJECT
+          )
+          break
+      }
+      return true;
+    })
   }
 
+
   contentListener(msg) {
     console.log('content.stream.listen:', msg, stream.key)
 
index 2590a91..5902cf7 100644 (file)
@@ -24,6 +24,14 @@ class Inject {
       ) {
         window.bytom = new Bytomdapp(stream, msg.payload)
       }
+
+      if (
+        msg &&
+        msg.hasOwnProperty('type') &&
+        msg.type === MsgTypes.UPDATE_BYTOM
+      ) {
+        window.bytom[msg.payload.type] = msg.payload.value
+      }
     })
 
     // Syncing the streams between the extension and the web application
index 6319bfa..b81f669 100644 (file)
@@ -1,6 +1,7 @@
 export const ERROR = 'error'
 
 export const PUSH_BYTOM = 'pushBytom'
+export const UPDATE_BYTOM = 'updateBytom'
 export const AUTHENTICATE = 'authenticate'
 export const TRANSFER = 'transfer'
 export const ADVTRANSFER = 'advTransfer'