Skip to content

Commit

Permalink
fix: networkChanged event listener not working
Browse files Browse the repository at this point in the history
  • Loading branch information
AricRedemption committed Mar 15, 2024
1 parent 9d85813 commit c272630
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 9 deletions.
20 changes: 14 additions & 6 deletions src/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ browser.runtime.onMessage.addListener(async (msg, sender) => {
return await exActions[msg.fn](...msg.args)
}

const actionName = msg.action.replace('authorize-', '').replace('query-', '').replace('event-', '').replace('inscribe-', '')
const actionName = msg.action
.replace('authorize-', '')
.replace('query-', '')
.replace('event-', '')
.replace('inscribe-', '')
if (msg.action?.startsWith('query') && actionName === 'Ping') {
const response = {
nonce: msg.nonce,
Expand All @@ -35,7 +39,10 @@ browser.runtime.onMessage.addListener(async (msg, sender) => {
failedStatus = 'locked'
} else if (!account || !account.id) {
failedStatus = 'not-logged-in'
} else if (!(await connector.isConnected(account.id, msg.host)) && !['Connect', 'IsConnected', 'ConnectBTC'].includes(actionName)) {
} else if (
!(await connector.isConnected(account.id, msg.host)) &&
!['Connect', 'IsConnected', 'ConnectBTC'].includes(actionName)
) {
failedStatus = 'not-connected'
}

Expand Down Expand Up @@ -96,13 +103,14 @@ browser.runtime.onMessage.addListener(async (msg, sender) => {
let popupUrl = browser.runtime.getURL(rawUrl)
popupUrl += `?${params.toString()}`


// To avoid repeating pop-ups of 'sign btc message'
let isClose = false
const tabs = await browser.tabs.query({ active: true })
tabs.forEach(tab => {
isClose = (tab.url?.includes('actionName=SignBTCMessage') || false) && (tab.url?.includes(`params=%22${msg.params}%22`) || false)
});
tabs.forEach((tab) => {
isClose =
(tab.url?.includes('actionName=SignBTCMessage') || false) &&
(tab.url?.includes(`params=%22${msg.params}%22`) || false)
})
if (isClose) {
return
}
Expand Down
2 changes: 1 addition & 1 deletion src/content-script/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export async function createAction(
callback(event.data)
}
}
return true
// return true
}
window.addEventListener('message', actionListener)
}
Expand Down
2 changes: 1 addition & 1 deletion src/content-script/inject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const listenToMetalet = () => {
window.postMessage(msg, '*')
}

return true
// return true
})
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ export type Network = 'mainnet' | 'testnet'
export const network = ref<Network>(await storage.get('network', { defaultValue: 'mainnet' }))

export async function setNetwork(_network: Network) {
await storage.set('network', _network)
network.value = _network
notifyContent('networkChanged')(_network)
await storage.set('network', _network)
}

export async function getNetwork(): Promise<Network> {
Expand Down
3 changes: 3 additions & 0 deletions src/pages/settings/components/SelectNetwork.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ref, Ref } from 'vue'
import { Listbox, ListboxButton, ListboxOptions, ListboxOption } from '@headlessui/vue'
import { ChevronRightIcon } from '@heroicons/vue/20/solid'
import { getNetwork, setNetwork } from '@/lib/network'
import { sleep } from '@/lib/helpers';
type Network = {
id: number
Expand All @@ -24,6 +25,8 @@ const select = async (network: Network) => {
// 存入存储
await setNetwork(network.type)
await sleep(200)
// force a reload
window.location.reload()
}
Expand Down

0 comments on commit c272630

Please sign in to comment.