Receive messages from TronLink
The message is sent using "window.postMessage", and the content received by the Dapp is a MessageEvent.You can refer to MDN documentation of MessageEvent.
Account Change Message
Message ID: accountsChanged
Overview
This message is generated when:
-
Users log in
-
Users switch accounts
-
Users lock accounts
-
The wallet is automatically locked after timeout
Specification
Example
window.addEventListener('message', function (e) {
if (e.data.message && e.data.message.action === "accountsChanged") {
// handler logic
console.log('got accountsChanged event', e.data)
}
})
Returns
interface MessageEventAccountsChangedData {
isTronLink: boolean;
message: {
action: string;
data: {
address: string | boolean;
}
}
}
Return value example
- When users log in, the content of the message body is:
{
"data": {
"address": "TZ5XixnRyraxJJy996Q1sip85PHWuj4793" // Last selected account address
}
}
- When users switch accounts, the content of the message body is:
{
"data": {
"address": "TRKb2nAnCBfwxnLxgoKJro6VbyA6QmsuXq" // Newly selected account address
}
}
- When users lock accounts and the wallet is automatically locked due to timeout, the message body content is:
{
"data": {
"address": false
}
}
Network Change Message
Message ID: setNode
Overview
Developers can monitor this message to know network changes
This message is generated when:
- When the user changes the network
Specification
Example
window.addEventListener('message', function (e) {
if (e.data.message && e.data.message.action == "setNode") {
// handler logic
console.log('got setNode event', e.data)
}
})
Returns
{
"node": {
// Information about the current network
},
"connectNode": {
// Node information of DApp chain
}
}
Successful connection message
Message ID: connect
Overview
Developers can monitor this message for connection changes.
This message is generated when:
-
The DApp requests a connection, and the user confirms the connection in the pop-up window
-
Users connect to the website
Specification
Example
window.addEventListener('message', function (e) {
if (e.data.message && e.data.message.action == "connect") {
// handler logic
console.log('got connect event', e.data)
}
})
Disconnect website message
Message ID: disconnect
Overview
Developers can monitor this message for connection changes.
This message is generated when:
-
The DApp requests a connection, and the user rejects the connection in the pop-up window
-
Users disconnect from the website
Specification
Example
window.addEventListener('message', function (e) {
if (e.data.message && e.data.message.action == "disconnect") {
// handler logic
console.log('got connect event', e.data)
}
})
Messages to Be Deprecated
-
The user rejects connection: “rejectWeb”
-
The user disconnects from the website: “disconnectWeb”
-
The user accepts connection: “acceptWeb”
-
The user requests to connect to the website: “connectWeb”
User rejects connection
DEPRECATED
Message ID: rejectWeb
This message is generated when:
- The DApp requests a connection and the user rejects the connection in the pop-up window.
Developers can get the connection rejection message by listening to it:
window.addEventListener('message', function (e) {
if (e.data.message && e.data.message.action == "rejectWeb") {
// handler logic
console.log('got rejectWeb event', e.data)
}
})
User disconnects from the website
DEPRECATED
Message ID: disconnectWeb
This message is generated when:
- User actively disconnect from the website.
Developers can get the disconnection message by listening to it:
window.addEventListener('message', function (e) {
if (e.data.message && e.data.message.action == "disconnectWeb") {
// handler logic
console.log('got disconnectWeb event', e.data)
}
})
User accepts connection
DEPRECATED
Message ID: acceptWeb
This message is generated when:
- The user accepts connection.
Developers can get the connection acceptance message by listening to it:
window.addEventListener('message', function (e) {
if (e.data.message && e.data.message.action == "acceptWeb") {
// handler logic
console.log('got acceptWeb event', e.data)
}
})
User requests to connect to the website
DEPRECATED
Message ID: connectWeb
This message is generated when:
- The user requests to connect to the website.
Developers can get the connection request message by listening to it:
window.addEventListener('message', function (e) {
if (e.data.message && e.data.message.action == "connectWeb") {
// handler logic
console.log('got connectWeb event', e.data)
}
})