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.
Files changed (2) hide show
  1. package/accountswap.js +52 -48
  2. 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
- const pollForAccount = async function (i, isNav) {
6
- // Assume it's nav
7
- if (isNav === undefined) {
8
- isNav = true;
9
- }
10
- // Start the iterator
11
- if (!i) {
12
- i = 0;
13
- // Add a click event to signin
14
- if (isNav) {
15
- const signinButton = document.querySelector(".hnp-signin");
16
- if (signinButton) {
17
- console.log("Found signin button");
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
- e.preventDefault();
23
- e.stopPropagation();
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
- // Safecheck for treg since it might not be global yet
29
- if (window && window.treg && window.treg.identity) {
30
- // Now we have all the vars to know if we're logged in
31
- if (!window.treg.identity.id) {
32
- // If we don't have an identity, we're not logged in
33
- return false;
34
- }
35
- if (isNav) {
36
- // We got a valid entitlement! Let's see if the button exists and swap our new one in
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
- return await pollForAccount(i + 1, isNav);
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
- // Instead of having a true link, set click event to run treg.realm.iframeProfile.NavigateToIndex()
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
- subButton.onclick = function (e) {
51
- window.treg.realm.iframeProfile.NavigateToIndex();
52
- e.preventDefault();
53
- e.stopPropagation();
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 };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sfc-utils",
3
- "version": "1.4.170",
3
+ "version": "1.4.172",
4
4
  "author": "ewagstaff <evanjwagstaff@gmail.com>",
5
5
  "dependencies": {
6
6
  "archieml": "^0.4.2",