// rc-loader.js (function () { var scriptElement = document.currentScript; var baseUrl = scriptElement.src.replace(/\/scripts\/rc-loader\.js(\?.*)?$/, ''); var HEALTHCHECK_SCRIPT_URL = baseUrl + "/scripts/health-check.js"; var LOG_PREFIX = "[sdk][loader]"; var iframeObserverTimer = null; var DEBUG_ENABLED = scriptElement.getAttribute("data-debug") === "true"; function getAccessibleWindows(targetWindow) { var windows = []; function pushWindow(candidate) { if (!candidate) return; if (windows.indexOf(candidate) >= 0) return; try { void candidate.location.href; windows.push(candidate); } catch (e) { // Cross-origin window; ignore. } } pushWindow(targetWindow && targetWindow.top); pushWindow(targetWindow && targetWindow.parent); pushWindow(targetWindow); return windows; } function getStorageValue(targetWindow, type, key) { var windows = getAccessibleWindows(targetWindow); for (var i = 0; i < windows.length; i++) { try { var storage = windows[i][type]; if (!storage) continue; var value = storage.getItem(key); if (value !== null && value !== undefined) return value; } catch (e) { // Ignore inaccessible storage. } } return null; } function setStorageValue(targetWindow, type, key, value) { var windows = getAccessibleWindows(targetWindow); for (var i = 0; i < windows.length; i++) { try { var storage = windows[i][type]; if (!storage) continue; storage.setItem(key, value); return true; } catch (e) { // Ignore inaccessible storage. } } return false; } function removeStorageValue(targetWindow, type, key) { var windows = getAccessibleWindows(targetWindow); for (var i = 0; i < windows.length; i++) { try { var storage = windows[i][type]; if (!storage) continue; storage.removeItem(key); return true; } catch (e) { // Ignore inaccessible storage. } } return false; } function attachSharedRuntime(doc) { if (!doc || !doc.defaultView) return; doc.defaultView.__SDK_SHARED__ = { getStorageItem: function (type, key) { return getStorageValue(doc.defaultView, type, key); }, setStorageItem: function (type, key, value) { return setStorageValue(doc.defaultView, type, key, value); }, removeStorageItem: function (type, key) { return removeStorageValue(doc.defaultView, type, key); } }; } function loadScriptInto(doc, id, src, attrs) { if (!doc || !doc.head) return; var existing = doc.getElementById(id); if (existing) { var existingSrc = existing.getAttribute("src") || ""; if (existingSrc === src) return; existing.remove(); } var s = doc.createElement("script"); s.src = src; s.id = id; s.async = true; s.onload = function () { if (DEBUG_ENABLED) console.log(LOG_PREFIX, "loaded", id, src); }; s.onerror = function (e) { console.error(LOG_PREFIX, "error", id, src, e); }; if (attrs) { for (var key in attrs) { s.setAttribute(key, attrs[key]); } } doc.head.appendChild(s); } function loadScriptsForDoc(doc) { attachSharedRuntime(doc); loadScriptInto(doc, "healthcheck-sdk", HEALTHCHECK_SCRIPT_URL); } function tryLoadIntoSameOriginIframes() { var iframes = document.querySelectorAll("iframe"); for (var i = 0; i < iframes.length; i++) { try { var iframeDoc = iframes[i].contentDocument; if (!iframeDoc) continue; loadScriptsForDoc(iframeDoc); } catch (e) { // Cross-origin iframe; ignore. } } } function init() { loadScriptsForDoc(document); tryLoadIntoSameOriginIframes(); if (iframeObserverTimer) clearInterval(iframeObserverTimer); iframeObserverTimer = setInterval(tryLoadIntoSameOriginIframes, 3000); } if (document.readyState === "loading") { document.addEventListener("DOMContentLoaded", init); } else { init(); } })();