sfc-utils 1.4.171 → 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 +47 -50
  2. package/package.json +1 -1
package/accountswap.js CHANGED
@@ -2,76 +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, attachedRealm) {
6
- // Assume it's nav
7
- if (isNav === undefined) {
8
- isNav = true;
9
- }
10
- if (attachedRealm === undefined) {
11
- attachedRealm = false;
12
- }
13
- // Start the iterator
14
- if (!i) {
15
- i = 0;
16
- }
17
- // Add a click event to signin
18
- if (isNav && !attachedRealm) {
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) {
19
11
  const signinButton = document.querySelector(".hnp-signin");
20
- console.log("Signin button, looking for .hnp-signin");
21
- if (signinButton) {
22
- console.log("Found signin button");
23
- // Add event listener to signin button
24
- attachedRealm = true;
12
+ if (signinButton && !signinButton.dataset.realmAttached) {
13
+ signinButton.dataset.realmAttached = "true";
14
+ console.log("Attaching signin handler");
25
15
  signinButton.onclick = function (e) {
26
- console.log("Clicked signin button");
27
- window.treg.realm.core.login();
16
+ if (window.treg?.realm?.core) {
17
+ console.log("Logging in!");
18
+ window.treg.realm.core.login();
19
+ }
28
20
  e.preventDefault();
29
21
  e.stopPropagation();
30
22
  };
23
+ attachedRealm = true;
31
24
  } else {
32
- console.log("Didn't find signin button this time");
25
+ // Wait and try again
26
+ await new Promise((resolve) => setTimeout(resolve, 500));
27
+ tries++;
33
28
  }
34
29
  }
35
- // Safecheck for treg since it might not be global yet
36
- if (window && window.treg && window.treg.identity) {
37
- // Now we have all the vars to know if we're logged in
38
- if (!window.treg.identity.id) {
39
- // If we don't have an identity, we're not logged in
40
- return false;
41
- }
42
- if (isNav) {
43
- // 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) {
44
39
  const rightBlock = document.querySelector(".nav2-right");
45
40
  if (rightBlock) {
46
41
  if (!rightBlock.innerText) {
47
42
  // If there's no innerText, keep waiting
48
43
  await new Promise((resolve) => setTimeout(resolve, 1000));
49
- return await pollForAccount(i + 1, isNav);
44
+ continue;
50
45
  }
51
- // Change the inner HTML
52
46
  rightBlock.innerHTML = `<a id="nav2-sub-box" href="${accountURL}"><div>Account</div></a>`;
53
- // Instead of having a true link, set click event to run treg.realm.iframeProfile.NavigateToIndex()
54
- if (window.treg.realm.iframeProfile) {
55
- // Find the newly swapped button
47
+ if (window.treg?.realm?.iframeProfile) {
56
48
  const subButton = document.querySelector("#nav2-sub-box");
57
- subButton.onclick = function (e) {
58
- window.treg.realm.iframeProfile.NavigateToIndex();
59
- e.preventDefault();
60
- e.stopPropagation();
61
- };
49
+ if (subButton) {
50
+ subButton.onclick = function (e) {
51
+ window.treg.realm.iframeProfile.NavigateToIndex();
52
+ e.preventDefault();
53
+ e.stopPropagation();
54
+ };
55
+ }
62
56
  }
57
+ swapped = true;
63
58
  }
59
+ } else {
60
+ // Wait and try again
61
+ await new Promise((resolve) => setTimeout(resolve, 500));
62
+ tries++;
64
63
  }
65
- return true;
66
- } else {
67
- if (i > 10) {
68
- // If we've waited 5 seconds and there's still no entitlement, assume we aren't getting one
69
- return false;
70
- }
71
- // Check again after 0.5 sec using a Promise with async/await
72
- await new Promise((resolve) => setTimeout(resolve, 500));
73
- return await pollForAccount(i + 1, isNav, attachedRealm);
74
64
  }
75
65
  };
76
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
+
77
74
  module.exports = { pollForAccount };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sfc-utils",
3
- "version": "1.4.171",
3
+ "version": "1.4.172",
4
4
  "author": "ewagstaff <evanjwagstaff@gmail.com>",
5
5
  "dependencies": {
6
6
  "archieml": "^0.4.2",