sfc-utils 1.4.170 → 1.4.172
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/accountswap.js +52 -48
- package/package.json +1 -1
package/accountswap.js
CHANGED
|
@@ -2,69 +2,73 @@
|
|
|
2
2
|
// Any domain + /realm/ should work for an account link
|
|
3
3
|
const accountURL = "/realm/";
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
// Add event listener to signin button
|
|
19
|
-
signinButton.onclick = function (e) {
|
|
20
|
-
console.log("Clicked signin button");
|
|
5
|
+
// Attach the sign-in handler to the .hnp-signin button if not already attached
|
|
6
|
+
const attachSigninHandler = async function () {
|
|
7
|
+
let attachedRealm = false;
|
|
8
|
+
let tries = 0;
|
|
9
|
+
const maxTries = 20; // 20 * 500ms = 10 seconds
|
|
10
|
+
while (!attachedRealm && tries < maxTries) {
|
|
11
|
+
const signinButton = document.querySelector(".hnp-signin");
|
|
12
|
+
if (signinButton && !signinButton.dataset.realmAttached) {
|
|
13
|
+
signinButton.dataset.realmAttached = "true";
|
|
14
|
+
console.log("Attaching signin handler");
|
|
15
|
+
signinButton.onclick = function (e) {
|
|
16
|
+
if (window.treg?.realm?.core) {
|
|
17
|
+
console.log("Logging in!");
|
|
21
18
|
window.treg.realm.core.login();
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
}
|
|
19
|
+
}
|
|
20
|
+
e.preventDefault();
|
|
21
|
+
e.stopPropagation();
|
|
22
|
+
};
|
|
23
|
+
attachedRealm = true;
|
|
24
|
+
} else {
|
|
25
|
+
// Wait and try again
|
|
26
|
+
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
27
|
+
tries++;
|
|
26
28
|
}
|
|
27
29
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
// Swap the Subscribe button for the Account button if logged in
|
|
33
|
+
const swapSubscribeForAccount = async function () {
|
|
34
|
+
let swapped = false;
|
|
35
|
+
let tries = 0;
|
|
36
|
+
const maxTries = 20; // 20 * 500ms = 10 seconds
|
|
37
|
+
while (!swapped && tries < maxTries) {
|
|
38
|
+
if (window?.treg?.identity?.id) {
|
|
37
39
|
const rightBlock = document.querySelector(".nav2-right");
|
|
38
40
|
if (rightBlock) {
|
|
39
41
|
if (!rightBlock.innerText) {
|
|
40
42
|
// If there's no innerText, keep waiting
|
|
41
43
|
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
42
|
-
|
|
44
|
+
continue;
|
|
43
45
|
}
|
|
44
|
-
// Change the inner HTML
|
|
45
46
|
rightBlock.innerHTML = `<a id="nav2-sub-box" href="${accountURL}"><div>Account</div></a>`;
|
|
46
|
-
|
|
47
|
-
if (window.treg.realm.iframeProfile) {
|
|
48
|
-
// Find the newly swapped button
|
|
47
|
+
if (window.treg?.realm?.iframeProfile) {
|
|
49
48
|
const subButton = document.querySelector("#nav2-sub-box");
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
49
|
+
if (subButton) {
|
|
50
|
+
subButton.onclick = function (e) {
|
|
51
|
+
window.treg.realm.iframeProfile.NavigateToIndex();
|
|
52
|
+
e.preventDefault();
|
|
53
|
+
e.stopPropagation();
|
|
54
|
+
};
|
|
55
|
+
}
|
|
55
56
|
}
|
|
57
|
+
swapped = true;
|
|
56
58
|
}
|
|
59
|
+
} else {
|
|
60
|
+
// Wait and try again
|
|
61
|
+
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
62
|
+
tries++;
|
|
57
63
|
}
|
|
58
|
-
return true;
|
|
59
|
-
} else {
|
|
60
|
-
if (i > 10) {
|
|
61
|
-
// If we've waited 5 seconds and there's still no entitlement, assume we aren't getting one
|
|
62
|
-
return false;
|
|
63
|
-
}
|
|
64
|
-
// Check again after 0.5 sec using a Promise with async/await
|
|
65
|
-
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
66
|
-
return await pollForAccount(i + 1, isNav);
|
|
67
64
|
}
|
|
68
65
|
};
|
|
69
66
|
|
|
67
|
+
// Launch both processes
|
|
68
|
+
const pollForAccount = async function () {
|
|
69
|
+
// Launch both async processes, but don't wait for them to finish (they are self-terminating)
|
|
70
|
+
attachSigninHandler();
|
|
71
|
+
swapSubscribeForAccount();
|
|
72
|
+
};
|
|
73
|
+
|
|
70
74
|
module.exports = { pollForAccount };
|