-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
156 lines (140 loc) · 5.25 KB
/
popup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
document.addEventListener('DOMContentLoaded', async () => {
const loginBtn = document.getElementById('loginBtn');
const logoutBtn = document.getElementById('logoutBtn');
const alertBox = document.getElementById('alert');
const alertMessage = document.getElementById('alertMessage');
const loginSection = document.getElementById('loginSection');
const logoutSection = document.getElementById('logoutSection');
const userDataSection = document.getElementById('userDataSection');
const displayNameElement = document.getElementById('displayName');
const userNameElement = document.getElementById('userName');
const mineAddress = document.getElementById('mineAddress');
const mineAmount = document.getElementById('mineAmount');
const totalGENY = document.getElementById('totalGENY');
const displayAvatar = document.getElementById('displayAvatar');
const userBio = document.getElementById('userBio');
const followersCount = document.getElementById('followersCount');
const followingCount = document.getElementById('followingCount');
const userNameDisplayLogout = document.getElementById('userNameDisplay');
const spinner = document.getElementById('spinner');
const browserAPI = window.chrome || window.browser;
/**
* Updates UI sections based on user login state
*/
const updateUI = (isLoggedIn) => {
loginSection.style.display = isLoggedIn ? 'none' : 'block';
logoutSection.style.display = isLoggedIn ? 'block' : 'none';
userDataSection.style.display = isLoggedIn ? 'block' : 'none';
};
/**
* Shortens a wallet address
*/
const shortenAddress = (address) => {
return address.length < 10 ? address : `${address.slice(0, 6)}....${address.slice(-4)}`;
};
/**
* Displays an alert message
*/
const showAlert = (message, type) => {
alertMessage.textContent = message;
alertBox.classList.remove('d-none', 'alert-success', 'alert-danger', 'alert-warning');
alertBox.classList.add(`alert-${type}`);
alertBox.style.display = 'block';
setTimeout(() => {
alertBox.classList.add('d-none');
alertBox.style.display = 'none';
}, 5000);
};
/**
* Loads user data into UI
*/
const loadUserData = (data) => {
if (data.isTokenHolder && data.walletAddress) {
displayNameElement.textContent = data.displayName;
userNameElement.textContent = data.userName;
mineAddress.textContent = shortenAddress(data.walletAddress);
mineAmount.textContent = data.tokenAmount;
totalGENY.textContent = '100B $GENY';
displayAvatar.src = data.displayAvatar;
userBio.textContent = data.userBio;
followersCount.textContent = data.followersCount;
followingCount.textContent = data.followingCount;
if (userNameDisplayLogout) userNameDisplayLogout.textContent = data.displayName;
updateUI(true);
} else {
updateUI(false);
}
};
/**
* Listen for storage update event
*/
browserAPI.runtime.onMessage.addListener((message) => {
if (message.type === 'userDataUpdated') {
console.log("User data updated, reloading UI...");
browserAPI.storage.sync.get(null, (data) => {
loadUserData(data);
});
}
});
/**
* Initialize UI: Load from storage
*/
browserAPI.storage.sync.get(null, (data) => {
if (data.walletAddress) {
loadUserData(data);
} else {
updateUI(false);
}
});
/**
* Handles wallet connection and validation.
*/
window.addEventListener('message', (event) => {
if (event.data?.type === 'WALLET_CONNECTED') {
const { walletAddress } = event.data;
console.log('Wallet Address received:', walletAddress);
if (walletAddress) {
spinner.classList.remove('d-none');
loginBtn.disabled = true;
loginBtn.innerHTML = `<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span> Processing...`;
browserAPI.runtime.sendMessage({ type: 'checkTokenHolder', walletAddress });
} else {
showAlert('Invalid wallet address.', 'danger');
}
}
});
/**
* Login button: Opens wallet connection
*/
loginBtn.addEventListener('click', () => {
spinner.classList.remove('d-none');
loginBtn.disabled = true;
loginBtn.innerHTML = `<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span> Processing...`;
const returnUrl = window.location.href;
const form = document.createElement('form');
form.method = 'POST';
form.action = 'https://genyframe.xyz/wallet-redirection';
form.target = 'WalletRedirectionWindow';
const input = document.createElement('input');
input.type = 'hidden';
input.name = 'returnUrl';
input.value = returnUrl;
form.appendChild(input);
document.body.appendChild(form);
window.open('', 'WalletRedirectionWindow', 'width=600,height=600');
form.submit();
document.body.removeChild(form);
});
/**
* Logout button: Clears storage & resets UI
*/
logoutBtn.addEventListener('click', () => {
browserAPI.storage.sync.remove([
'userName', 'displayName', 'isTokenHolder', 'walletAddress',
'tokenAmount', 'displayAvatar', 'userBio', 'followersCount', 'followingCount'
], () => {
showAlert('Logged out successfully.', 'warning');
updateUI(false);
});
});
});