From: Zhiting Lin Date: Thu, 9 May 2019 06:05:49 +0000 (+0800) Subject: update the account and netwrok change detection. X-Git-Tag: 2.0.0~8 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=e1678ba96fa3a0faef6999868f3c924a5ef643c9;p=bytom%2FByone.git update the account and netwrok change detection. --- diff --git a/src/background.js b/src/background.js index c685b58..54ebd8e 100644 --- a/src/background.js +++ b/src/background.js @@ -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) {}); + }); } } } diff --git a/src/content.js b/src/content.js index ccae4f4..d1bc101 100644 --- a/src/content.js +++ b/src/content.js @@ -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) diff --git a/src/inject.js b/src/inject.js index 2590a91..5902cf7 100644 --- a/src/inject.js +++ b/src/inject.js @@ -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 diff --git a/src/messages/types.js b/src/messages/types.js index 6319bfa..b81f669 100644 --- a/src/messages/types.js +++ b/src/messages/types.js @@ -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'