Martechsol commited on
Commit Β·
c7920aa
1
Parent(s): 9d9919f
fix(addon): implement infinite zero-overhead name resolution with storage and focus events
Browse files- static/addon.html +19 -17
static/addon.html
CHANGED
|
@@ -345,8 +345,6 @@
|
|
| 345 |
}
|
| 346 |
|
| 347 |
// ββ Shared helper: resolve user name from all 3 options βββββββββββββββββ
|
| 348 |
-
let _lastFailedToken = null;
|
| 349 |
-
|
| 350 |
async function _resolveUserName() {
|
| 351 |
// ββ OPTION 0: Window injection by HR portal (fastest, zero cost) ββ
|
| 352 |
// The HR portal page sets window.HR_USER_NAME = "John Doe" before this script loads
|
|
@@ -388,10 +386,6 @@
|
|
| 388 |
const tv = localStorage.getItem(tk) || sessionStorage.getItem(tk);
|
| 389 |
if (tv && tv.length > 20) { authToken = tv.replace(/^Bearer\s+/i, ''); break; }
|
| 390 |
}
|
| 391 |
-
|
| 392 |
-
// Prevent API spam if we already tried this specific token and it failed
|
| 393 |
-
if (authToken && authToken === _lastFailedToken) return null;
|
| 394 |
-
|
| 395 |
const headers = { 'Content-Type': 'application/json' };
|
| 396 |
if (authToken) headers['Authorization'] = 'Bearer ' + authToken;
|
| 397 |
|
|
@@ -399,17 +393,12 @@
|
|
| 399 |
headers: headers,
|
| 400 |
credentials: 'include' // also sends cookies (session-based auth)
|
| 401 |
});
|
| 402 |
-
|
| 403 |
if (pResp.ok) {
|
| 404 |
const pData = await pResp.json();
|
| 405 |
// API top-level 'name' field is confirmed; personalDetails.fullName is fallback
|
| 406 |
const found = pData.name || (pData.personalDetails && pData.personalDetails.fullName);
|
| 407 |
if (found && found.trim().length > 1) return found.trim();
|
| 408 |
}
|
| 409 |
-
|
| 410 |
-
// If we reached here without returning, the token is invalid/expired
|
| 411 |
-
if (authToken) _lastFailedToken = authToken;
|
| 412 |
-
|
| 413 |
} catch (e) {}
|
| 414 |
|
| 415 |
return null; // all options exhausted
|
|
@@ -467,14 +456,17 @@
|
|
| 467 |
}
|
| 468 |
} catch (e) {}
|
| 469 |
|
| 470 |
-
//
|
| 471 |
-
// Checks local storage (0 cost). Will only hit the API if a *new* auth token appears.
|
| 472 |
if (!userName) {
|
| 473 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 474 |
const resolved = await _resolveUserName();
|
| 475 |
-
|
| 476 |
if (resolved) {
|
| 477 |
-
clearInterval(retryInterval);
|
| 478 |
localStorage.setItem('martech_last_user_name', resolved);
|
| 479 |
try {
|
| 480 |
const rData = await fetch(BASE_URL + "/api/session/update-name", {
|
|
@@ -488,7 +480,17 @@
|
|
| 488 |
}
|
| 489 |
} catch (e) {}
|
| 490 |
}
|
| 491 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 492 |
}
|
| 493 |
}
|
| 494 |
|
|
|
|
| 345 |
}
|
| 346 |
|
| 347 |
// ββ Shared helper: resolve user name from all 3 options βββββββββββββββββ
|
|
|
|
|
|
|
| 348 |
async function _resolveUserName() {
|
| 349 |
// ββ OPTION 0: Window injection by HR portal (fastest, zero cost) ββ
|
| 350 |
// The HR portal page sets window.HR_USER_NAME = "John Doe" before this script loads
|
|
|
|
| 386 |
const tv = localStorage.getItem(tk) || sessionStorage.getItem(tk);
|
| 387 |
if (tv && tv.length > 20) { authToken = tv.replace(/^Bearer\s+/i, ''); break; }
|
| 388 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 389 |
const headers = { 'Content-Type': 'application/json' };
|
| 390 |
if (authToken) headers['Authorization'] = 'Bearer ' + authToken;
|
| 391 |
|
|
|
|
| 393 |
headers: headers,
|
| 394 |
credentials: 'include' // also sends cookies (session-based auth)
|
| 395 |
});
|
|
|
|
| 396 |
if (pResp.ok) {
|
| 397 |
const pData = await pResp.json();
|
| 398 |
// API top-level 'name' field is confirmed; personalDetails.fullName is fallback
|
| 399 |
const found = pData.name || (pData.personalDetails && pData.personalDetails.fullName);
|
| 400 |
if (found && found.trim().length > 1) return found.trim();
|
| 401 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 402 |
} catch (e) {}
|
| 403 |
|
| 404 |
return null; // all options exhausted
|
|
|
|
| 456 |
}
|
| 457 |
} catch (e) {}
|
| 458 |
|
| 459 |
+
// If name still not found, set up continuous lightweight background resolution
|
|
|
|
| 460 |
if (!userName) {
|
| 461 |
+
let _isResolving = false;
|
| 462 |
+
|
| 463 |
+
const _attemptSync = async () => {
|
| 464 |
+
// Stop checking if we already got a real name, or if a check is currently running
|
| 465 |
+
if (!_isGenericId(sessionId) || _isResolving) return;
|
| 466 |
+
|
| 467 |
+
_isResolving = true;
|
| 468 |
const resolved = await _resolveUserName();
|
|
|
|
| 469 |
if (resolved) {
|
|
|
|
| 470 |
localStorage.setItem('martech_last_user_name', resolved);
|
| 471 |
try {
|
| 472 |
const rData = await fetch(BASE_URL + "/api/session/update-name", {
|
|
|
|
| 480 |
}
|
| 481 |
} catch (e) {}
|
| 482 |
}
|
| 483 |
+
_isResolving = false;
|
| 484 |
+
};
|
| 485 |
+
|
| 486 |
+
// Event 1: Instantly trigger when the main app writes a token to storage
|
| 487 |
+
window.addEventListener('storage', _attemptSync);
|
| 488 |
+
|
| 489 |
+
// Event 2: Instantly trigger when user refocuses the tab (e.g. after auth redirect)
|
| 490 |
+
window.addEventListener('focus', _attemptSync);
|
| 491 |
+
|
| 492 |
+
// Fallback: Check every 15 seconds (virtually zero cost once session is named)
|
| 493 |
+
setInterval(_attemptSync, 15000);
|
| 494 |
}
|
| 495 |
}
|
| 496 |
|