sfc-utils 1.4.171 → 1.4.173

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 +42 -54
  2. package/package.json +1 -1
package/accountswap.js CHANGED
@@ -2,76 +2,64 @@
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) {
11
+ tries++;
19
12
  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;
13
+ if (signinButton && !signinButton.dataset.realmAttached) {
14
+ signinButton.dataset.realmAttached = "true";
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
+ window.treg.realm.core.login();
18
+ }
28
19
  e.preventDefault();
29
20
  e.stopPropagation();
30
21
  };
22
+ attachedRealm = true;
31
23
  } else {
32
- console.log("Didn't find signin button this time");
24
+ // Wait and try again
25
+ await new Promise((resolve) => setTimeout(resolve, 500));
33
26
  }
34
27
  }
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
28
+ };
29
+
30
+ // Swap the Subscribe button for the Account button if logged in
31
+ const swapSubscribeForAccount = async function () {
32
+ let swapped = false;
33
+ let tries = 0;
34
+ const maxTries = 20; // 20 * 500ms = 10 seconds
35
+ while (!swapped && tries < maxTries) {
36
+ tries++;
37
+ if (window?.treg?.identity?.id) {
44
38
  const rightBlock = document.querySelector(".nav2-right");
45
- if (rightBlock) {
46
- if (!rightBlock.innerText) {
47
- // If there's no innerText, keep waiting
48
- await new Promise((resolve) => setTimeout(resolve, 1000));
49
- return await pollForAccount(i + 1, isNav);
50
- }
51
- // Change the inner HTML
39
+ if (rightBlock && rightBlock.innerText) {
52
40
  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
41
+ if (window?.treg?.realm?.iframeProfile) {
56
42
  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
- };
43
+ if (subButton) {
44
+ subButton.onclick = function (e) {
45
+ window.treg.realm.iframeProfile.NavigateToIndex();
46
+ e.preventDefault();
47
+ e.stopPropagation();
48
+ };
49
+ swapped = true;
50
+ }
62
51
  }
63
52
  }
64
53
  }
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
54
  await new Promise((resolve) => setTimeout(resolve, 500));
73
- return await pollForAccount(i + 1, isNav, attachedRealm);
74
55
  }
75
56
  };
76
57
 
58
+ // Launch both processes
59
+ const pollForAccount = async function () {
60
+ // Launch both async processes, but don't wait for them to finish (they are self-terminating)
61
+ attachSigninHandler();
62
+ swapSubscribeForAccount();
63
+ };
64
+
77
65
  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.173",
4
4
  "author": "ewagstaff <evanjwagstaff@gmail.com>",
5
5
  "dependencies": {
6
6
  "archieml": "^0.4.2",