@patch-adams/core 1.5.19 → 1.5.21

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/dist/index.cjs CHANGED
@@ -1600,6 +1600,129 @@ function generateLrsBridgeCode(options) {
1600
1600
  return hasValidMbox || hasValidAccount;
1601
1601
  }
1602
1602
 
1603
+ // ========================================================================
1604
+ // CROSS-FRAME ACTOR SHARING
1605
+ // PA-Patcher injects the bridge into ALL HTML files in a SCORM package.
1606
+ // Rise courses have multiple HTML files (index.html, scormcontent/index.html),
1607
+ // each getting their own bridge instance with its own LRS.actor.
1608
+ // The skin overlay (email gate) only runs in one frame and sets the actor there.
1609
+ // Without sharing, other bridge instances keep the Anonymous Learner actor.
1610
+ // Solution: persist actor to localStorage so all bridge instances can use it.
1611
+ // ========================================================================
1612
+ var ACTOR_STORAGE_KEY = 'pa_lrs_shared_actor';
1613
+
1614
+ function isAnonymousActor(actor) {
1615
+ if (!actor) return true;
1616
+ if (!actor.name) return true;
1617
+ var n = actor.name.toLowerCase();
1618
+ return n === 'anonymous learner' || n === 'unknown learner' || n === 'anonymous' || n === 'unknown';
1619
+ }
1620
+
1621
+ /**
1622
+ * Persist actor to localStorage for cross-frame sharing.
1623
+ * Only stores non-anonymous actors.
1624
+ */
1625
+ function persistActor(actor) {
1626
+ if (!actor || isAnonymousActor(actor)) return;
1627
+ try {
1628
+ localStorage.setItem(ACTOR_STORAGE_KEY, JSON.stringify(actor));
1629
+ log('Actor persisted to localStorage:', actor.name);
1630
+ } catch (e) { /* localStorage unavailable */ }
1631
+ }
1632
+
1633
+ /**
1634
+ * Load shared actor from localStorage.
1635
+ * Returns the stored actor or null.
1636
+ */
1637
+ function loadSharedActor() {
1638
+ try {
1639
+ var stored = localStorage.getItem(ACTOR_STORAGE_KEY);
1640
+ if (stored) {
1641
+ var actor = JSON.parse(stored);
1642
+ if (actor && !isAnonymousActor(actor)) {
1643
+ return actor;
1644
+ }
1645
+ }
1646
+ } catch (e) { /* localStorage unavailable or parse error */ }
1647
+ return null;
1648
+ }
1649
+
1650
+ /**
1651
+ * Get the best available actor for statement building.
1652
+ * ALWAYS checks localStorage because the skin overlay in another frame
1653
+ * may have updated the actor AFTER this bridge instance initialized.
1654
+ * (e.g., user enters email in skin overlay \u2192 actor persisted to localStorage,
1655
+ * but the bridge in scormcontent/index.html already loaded a stale actor at init)
1656
+ */
1657
+ function getActor() {
1658
+ // Always check localStorage for the freshest actor \u2014 it may have been
1659
+ // updated by the skin overlay in another frame since our init
1660
+ var shared = loadSharedActor();
1661
+ if (shared) {
1662
+ // Only update if different from current (avoid unnecessary log spam)
1663
+ if (!LRS.actor || LRS.actor.name !== shared.name ||
1664
+ (LRS.actor.account && shared.account && LRS.actor.account.name !== shared.account.name)) {
1665
+ log('Actor updated from cross-frame storage:', shared.name);
1666
+ }
1667
+ LRS.actor = shared;
1668
+ return shared;
1669
+ }
1670
+ // Fallback to current actor or re-extract
1671
+ if (LRS.actor && !isAnonymousActor(LRS.actor)) {
1672
+ return LRS.actor;
1673
+ }
1674
+ return LRS.actor || extractActor();
1675
+ }
1676
+
1677
+ /**
1678
+ * Set actor on the bridge with cross-frame persistence.
1679
+ * Called by skins and external code via window.pa_patcher.lrs.setActor(actor)
1680
+ */
1681
+ LRS.setActor = function(actor) {
1682
+ LRS.actor = actor;
1683
+ persistActor(actor);
1684
+
1685
+ // Propagate to ALL frames: walk UP parent chain AND DOWN into child iframes
1686
+ // UP: parent frames (e.g., if skin is in a child iframe)
1687
+ try {
1688
+ var w = window;
1689
+ for (var i = 0; i < 10; i++) {
1690
+ try {
1691
+ if (w !== window && w.pa_patcher && w.pa_patcher.lrs) {
1692
+ w.pa_patcher.lrs.actor = actor;
1693
+ }
1694
+ } catch (e) { /* cross-origin */ }
1695
+ if (w === w.parent) break;
1696
+ w = w.parent;
1697
+ }
1698
+ } catch (e) {}
1699
+
1700
+ // DOWN: child iframes (e.g., scormcontent/index.html has its own bridge)
1701
+ function setActorInChildren(win) {
1702
+ try {
1703
+ var frames = win.frames;
1704
+ for (var j = 0; j < frames.length; j++) {
1705
+ try {
1706
+ if (frames[j].pa_patcher && frames[j].pa_patcher.lrs) {
1707
+ frames[j].pa_patcher.lrs.actor = actor;
1708
+ log('Actor propagated to child frame', j);
1709
+ }
1710
+ // Recurse into nested iframes
1711
+ setActorInChildren(frames[j]);
1712
+ } catch (e) { /* cross-origin child */ }
1713
+ }
1714
+ } catch (e) {}
1715
+ }
1716
+ setActorInChildren(window);
1717
+
1718
+ if (window.console && window.console.info) {
1719
+ console.info('[PA-LRS] Actor set:', actor.name,
1720
+ actor.mbox ? '(' + actor.mbox + ')' : '',
1721
+ actor.account ? '(account: ' + actor.account.name + ')' : '',
1722
+ '\u2014 shared across frames');
1723
+ }
1724
+ };
1725
+
1603
1726
  /**
1604
1727
  * Async version that fetches Bravais session if needed
1605
1728
  * Also re-checks all sources in case they became available
@@ -1611,6 +1734,7 @@ function generateLrsBridgeCode(options) {
1611
1734
  // Try to enhance actor with email if missing
1612
1735
  enhanceActorWithEmail(actor, function(enhancedActor) {
1613
1736
  LRS.actor = enhancedActor;
1737
+ persistActor(enhancedActor);
1614
1738
  callback(enhancedActor);
1615
1739
  });
1616
1740
  }
@@ -2617,7 +2741,7 @@ function generateLrsBridgeCode(options) {
2617
2741
 
2618
2742
  var statement = {
2619
2743
  id: generateUUID(),
2620
- actor: LRS.actor || extractActor(),
2744
+ actor: getActor(),
2621
2745
  verb: typeof verb === 'string' ? (VERBS[verb] || { id: verb, display: { 'en-US': verb } }) : verb,
2622
2746
  object: courseObj,
2623
2747
  timestamp: new Date().toISOString()
@@ -3039,7 +3163,7 @@ function generateLrsBridgeCode(options) {
3039
3163
  function buildStatementXyleme(verb, activityObject, result, additionalContext) {
3040
3164
  var statement = {
3041
3165
  id: generateUUID(),
3042
- actor: LRS.actor || extractActor(),
3166
+ actor: getActor(),
3043
3167
  verb: typeof verb === 'string' ? (VERBS[verb] || { id: verb, display: { 'en-US': verb } }) : verb,
3044
3168
  object: activityObject,
3045
3169
  timestamp: new Date().toISOString()
@@ -4768,6 +4892,15 @@ function generateLrsBridgeCode(options) {
4768
4892
  // Extract actor (sync first for immediate use)
4769
4893
  extractActor();
4770
4894
 
4895
+ // Check localStorage for actor set by another frame (e.g., skin overlay in a different HTML file)
4896
+ if (isAnonymousActor(LRS.actor)) {
4897
+ var sharedActor = loadSharedActor();
4898
+ if (sharedActor) {
4899
+ LRS.actor = sharedActor;
4900
+ log('Loaded actor from cross-frame storage at init:', sharedActor.name);
4901
+ }
4902
+ }
4903
+
4771
4904
  log('Bridge initialized in mode:', LRS.mode);
4772
4905
  log('Actor:', LRS.actor);
4773
4906
  log('Course:', LRS.courseInfo);