Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (() => {
- const COOLDOWN_PERIOD = 5000; // Prevents multiple triggers within 5s
- const BANNER_CLICK_COOLDOWN = 100; // Prevents spam clicking
- let hasTriggeredFix = false;
- let lastTriggerTime = 0;
- let lastBannerClickTime = 0;
- let bannerMonitorActive = false;
- function getHass() {
- return document.querySelector("home-assistant")?.hass || null;
- }
- function fetchLovelaceConfig() {
- if (hasTriggeredFix) {
- return;
- }
- hasTriggeredFix = true;
- const hass = getHass();
- if (!hass?.connection) {
- return;
- }
- hass.connection.sendMessagePromise({ type: "lovelace/config" })
- .then(saveLovelaceConfig)
- .catch(err => console.error("❌ Failed to fetch Lovelace config:", err));
- }
- function saveLovelaceConfig(lovelaceConfig) {
- const hass = getHass();
- if (!hass?.connection) {
- return;
- }
- hass.connection.sendMessagePromise({
- type: "lovelace/config/save",
- config: { views: lovelaceConfig.views },
- url_path: null
- }).then(() => {
- console.log("✅ Successfully saved Lovelace config!");
- monitorForBanner(); // Start monitoring for refresh banner
- }).catch(err => console.error("❌ Failed to save Lovelace config!", err));
- }
- function findButtonInShadowRoots(text) {
- const queue = [{ root: document }];
- while (queue.length) {
- const { root } = queue.shift();
- const button = [...root.querySelectorAll("button")].find(
- btn => btn.textContent.trim().toUpperCase() === text
- );
- if (button) {
- return button;
- }
- queue.push(...[...root.querySelectorAll("*")]
- .filter(el => el.shadowRoot)
- .map(el => ({ root: el.shadowRoot })));
- }
- return null;
- }
- function monitorForBanner() {
- if (bannerMonitorActive) {
- return;
- }
- console.log("🔎 Monitoring for Lovelace Refresh banner...");
- bannerMonitorActive = true;
- function checkBanner() {
- const now = Date.now();
- const refreshButton = findButtonInShadowRoots("REFRESH");
- if (refreshButton && now - lastBannerClickTime > BANNER_CLICK_COOLDOWN) {
- console.log("✅ Found Lovelace Refresh button! Clicking...");
- refreshButton.click();
- lastBannerClickTime = now;
- }
- requestAnimationFrame(checkBanner); // Keep monitoring even after clicking
- }
- requestAnimationFrame(checkBanner);
- }
- function monitorConsoleForError() {
- const TARGET_ERROR = "Cannot read properties of undefined (reading 'style')";
- function triggerFix() {
- const now = Date.now();
- if (now - lastTriggerTime < COOLDOWN_PERIOD) {
- return;
- }
- lastTriggerTime = now;
- if (!hasTriggeredFix) {
- console.warn("⚠️ Lovelace error detected! Running fix...");
- fetchLovelaceConfig();
- }
- }
- const originalConsoleError = console.error;
- console.error = function (...args) {
- originalConsoleError.apply(console, args);
- if (args.some(arg => arg?.toString().includes(TARGET_ERROR))) {
- triggerFix();
- }
- };
- window.onerror = function (message, source, lineno, colno, error) {
- if (message.includes(TARGET_ERROR)) {
- triggerFix();
- }
- };
- }
- function isHomeAssistantApp() {
- return navigator.userAgent.includes("HomeAssistant");
- }
- if (!window.__lovelaceConfigFetched) {
- window.__lovelaceConfigFetched = true;
- monitorConsoleForError();
- if (isHomeAssistantApp()) {
- console.warn("📱 Running inside Home Assistant app - fetching Lovelace config...");
- fetchLovelaceConfig();
- }
- }
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement